Exception: Core::Exceptions::Error

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/svcbase/exceptions.rb

Overview

errors

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

Class Method Summary collapse

Instance Method Summary collapse

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

  message = 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] ||= message
    details[:code] ||= msgcode
    details[:obj] ||= msgobj if msgobj
  end
  super message
rescue I18n::ArgumentError => e
  # make sure we see these if they happen!
  log.fatal e
  raise
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



13
14
15
# File 'lib/svcbase/exceptions.rb', line 13

def details
  @details
end

#headersObject (readonly)

Returns the value of attribute headers.



13
14
15
# File 'lib/svcbase/exceptions.rb', line 13

def headers
  @headers
end

#loglevelObject (readonly)

Returns the value of attribute loglevel.



13
14
15
# File 'lib/svcbase/exceptions.rb', line 13

def loglevel
  @loglevel
end

#msgcodeObject (readonly)

Returns the value of attribute msgcode.



13
14
15
# File 'lib/svcbase/exceptions.rb', line 13

def msgcode
  @msgcode
end

#msgobjObject (readonly)

Returns the value of attribute msgobj.



13
14
15
# File 'lib/svcbase/exceptions.rb', line 13

def msgobj
  @msgobj
end

Class Method Details

.classnameObject



15
16
17
# File 'lib/svcbase/exceptions.rb', line 15

def self.classname
  name.demodulize
end

.default_loglevelObject



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_msgcodeObject



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

#logmsgObject

This is called by sa_logger.



71
72
73
74
75
# File 'lib/svcbase/exceptions.rb', line 71

def logmsg
  msg = @logmsg || message
  return msg unless msg.is_a? String # this lets us log objects or exceptions too
  "#{self.class.classname} - #{msg}"
end

#responseObject

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: message
    }
  }
  response[:error][:obj] = msgobj if msgobj
  response[:error][:details] = details unless details.empty?
  response
end

#statusObject



22
23
24
# File 'lib/svcbase/exceptions.rb', line 22

def status
  self.class::STATUS
end