Exception: Core::Exceptions::Error
- Defined in:
- lib/svcbase/exceptions.rb
Overview
errors
Direct Known Subclasses
BadRequest, Conflict, Forbidden, Gone, NotFound, NotModified, OpaqueError, TooManyRequests, Unprocessable, UnsupportedMediaType
Constant Summary collapse
- MESSAGE_SCOPE =
This sets the locale file scope. It may be redefined by subclasses to change that scope.
'app.errors'
- STATUS =
STATUS should be redefined by subclasses. It cannot be overridden by ctor so it is fixed for a given subclass.
500
- DEFAULT_MSGCODE =
Each Error class’s default msgcode will be based on the class’s name unless DEFAULT_MSGCODE is set for it or a superclass. Supplying a msgcode on the constructor will override the default.
nil
- DEFAULT_LOGLEVEL =
Each Error class’s default loglevel will be based on the class’s STATUS unless DEFAULT_LOGLEVEL is set for it or a superclass. Supplying a loglevel on the constructor will override the default.
nil
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#loglevel ⇒ Object
readonly
Returns the value of attribute loglevel.
-
#msgcode ⇒ Object
readonly
Returns the value of attribute msgcode.
-
#msgobj ⇒ Object
readonly
Returns the value of attribute msgobj.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(msg = nil, loglevel: nil, logmsg: nil, headers: nil, **details) ⇒ Error
constructor
rubocop:disable AbcSize.
-
#logmsg ⇒ Object
This is called by sa_logger.
-
#response ⇒ Object
The response object that is returned to the caller.
- #status ⇒ Object
Constructor Details
#initialize(msg = nil, loglevel: nil, logmsg: nil, headers: nil, **details) ⇒ Error
rubocop:disable AbcSize
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/svcbase/exceptions.rb', line 43 def initialize(msg = nil, loglevel: nil, logmsg: nil, headers: nil, **details) raise "new not allowed for #{self.class}" if self.class == Exceptions::Error @msgcode = msg || details.delete(:msg) || self.class.default_msgcode raise 'msg must be a symbol' unless @msgcode.is_a? Symbol = I18n.t!(@msgcode, scope: self.class::MESSAGE_SCOPE, classname: self.class.name, **details) @msgobj = details.delete :msgobj @details = details @loglevel = loglevel || self.class.default_loglevel @logmsg = logmsg @headers = headers || {} unless details.empty? || details[:message] details[:message] ||= details[:code] ||= msgcode details[:obj] ||= msgobj if msgobj end super rescue I18n::ArgumentError => e # make sure we see these if they happen! log.fatal e raise end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
13 14 15 |
# File 'lib/svcbase/exceptions.rb', line 13 def details @details end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/svcbase/exceptions.rb', line 13 def headers @headers end |
#loglevel ⇒ Object (readonly)
Returns the value of attribute loglevel.
13 14 15 |
# File 'lib/svcbase/exceptions.rb', line 13 def loglevel @loglevel end |
#msgcode ⇒ Object (readonly)
Returns the value of attribute msgcode.
13 14 15 |
# File 'lib/svcbase/exceptions.rb', line 13 def msgcode @msgcode end |
#msgobj ⇒ Object (readonly)
Returns the value of attribute msgobj.
13 14 15 |
# File 'lib/svcbase/exceptions.rb', line 13 def msgobj @msgobj end |
Class Method Details
.classname ⇒ Object
15 16 17 |
# File 'lib/svcbase/exceptions.rb', line 15 def self.classname name.demodulize end |
.default_loglevel ⇒ Object
38 39 40 |
# File 'lib/svcbase/exceptions.rb', line 38 def self.default_loglevel self::DEFAULT_LOGLEVEL || ((400..499).cover?(self::STATUS) ? :error : :fatal) end |
.default_msgcode ⇒ Object
30 31 32 |
# File 'lib/svcbase/exceptions.rb', line 30 def self.default_msgcode self::DEFAULT_MSGCODE || classname.underscore.to_sym # Exceptions::AuthError -> :auth_error end |
Instance Method Details
#logmsg ⇒ Object
This is called by sa_logger.
71 72 73 74 75 |
# File 'lib/svcbase/exceptions.rb', line 71 def logmsg msg = @logmsg || return msg unless msg.is_a? String # this lets us log objects or exceptions too "#{self.class.classname} - #{msg}" end |
#response ⇒ Object
The response object that is returned to the caller.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/svcbase/exceptions.rb', line 78 def response response = { status: :error, error: { code: msgcode, message: } } response[:error][:obj] = msgobj if msgobj response[:error][:details] = details unless details.empty? response end |
#status ⇒ Object
22 23 24 |
# File 'lib/svcbase/exceptions.rb', line 22 def status self.class::STATUS end |