Class: JsonapiErrorable::ExceptionHandler
- Inherits:
-
Object
- Object
- JsonapiErrorable::ExceptionHandler
- Defined in:
- lib/jsonapi_errorable/exception_handler.rb
Instance Attribute Summary collapse
-
#show_raw_error ⇒ Object
Returns the value of attribute show_raw_error.
Instance Method Summary collapse
- #backtrace_cleaner ⇒ Object
- #detail(error) ⇒ Object
- #error_code(error) ⇒ Object
- #error_payload(error) ⇒ Object
-
#initialize(options = {}) ⇒ ExceptionHandler
constructor
A new instance of ExceptionHandler.
- #log(error) ⇒ Object
- #log? ⇒ Boolean
- #meta(error) ⇒ Object
- #status_code(error) ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ExceptionHandler
Returns a new instance of ExceptionHandler.
5 6 7 8 9 10 11 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 5 def initialize( = {}) @status = [:status] @title = [:title] @message = [:message] @meta = [:meta] @log = [:log] end |
Instance Attribute Details
#show_raw_error ⇒ Object
Returns the value of attribute show_raw_error.
3 4 5 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 3 def show_raw_error @show_raw_error end |
Instance Method Details
#backtrace_cleaner ⇒ Object
22 23 24 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 22 def backtrace_cleaner defined?(Rails) ? Rails.backtrace_cleaner : nil end |
#detail(error) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 30 def detail(error) if @message == true error. else @message ? @message.call(error) : default_detail end end |
#error_code(error) ⇒ Object
17 18 19 20 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 17 def error_code(error) status_code = status_code(error) Rack::Utils::SYMBOL_TO_STATUS_CODE.invert[status_code] end |
#error_payload(error) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 53 def error_payload(error) { errors: [ code: error_code(error), status: status_code(error).to_s, title: title, detail: detail(error), meta: (error) ] } end |
#log(error) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 69 def log(error) return unless log? backtrace = error.backtrace if cleaner = backtrace_cleaner backtrace = cleaner.clean(backtrace) end log_error(error, backtrace) end |
#log? ⇒ Boolean
65 66 67 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 65 def log? @log != false end |
#meta(error) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 38 def (error) {}.tap do || if @meta.respond_to?(:call) .merge!(@meta.call(error)) end if show_raw_error [:__raw_error__] = { message: error., backtrace: error.backtrace.join("\n") } end end end |
#status_code(error) ⇒ Object
13 14 15 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 13 def status_code(error) @status || 500 end |
#title ⇒ Object
26 27 28 |
# File 'lib/jsonapi_errorable/exception_handler.rb', line 26 def title @title || 'Error' end |