# vim:ts=2:sw=2:et:si:ai: # The API sends back messages via the p4.messages attribute if there was an # error with the last command. We will often convert that to this exception, # which will be formatted into JSON. require 'P4' # Any error we get from the Perforce server is generally encapsulated in # a P4Error, which allows us to get some basic diagnostic information from # the Perforce server out to the user. class P4Error < RuntimeError # All error codes must be greater than this number. ERROR_CODE_BASE = 15_360 # Note: We use 15_361 for 'unhandled' ruby errors attr_accessor :message_code, :message_severity, :message_text # Class method to create a valid P4Error object from a simple # string. Used when we get errors for which no P4::Message object # is available. def self.default_error(message_text) P4Error.new(ERROR_CODE_BASE, P4::E_FAILED, message_text) end # Returns true if there's an error def self.error?(p4, severity: P4::E_FAILED) return p4.messages && p4.messages.any? {|m| m.severity >= severity} end def initialize(code, severity, text) @message_code = code @message_severity = severity @message_text = text end # Returns true if the exception should actually be considered a user error # instead of a system exception. def user_error? return @message_severity == P4::E_FAILED end # Returns true if the exception is truly a system problem. def system_error? return @message_severity == P4::E_FATAL end def to_s @message_text end end