Class: GraphitiErrors::ExceptionHandler
- Inherits:
-
Object
- Object
- GraphitiErrors::ExceptionHandler
show all
- Defined in:
- lib/graphiti_errors/exception_handler.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ExceptionHandler.
5
6
7
8
9
10
11
|
# File 'lib/graphiti_errors/exception_handler.rb', line 5
def initialize(options = {})
@status = options[:status]
@title = options[:title]
@message = options[:message]
@meta = options[:meta]
@log = options[:log]
end
|
Instance Attribute Details
#show_raw_error ⇒ Object
Returns the value of attribute show_raw_error.
3
4
5
|
# File 'lib/graphiti_errors/exception_handler.rb', line 3
def show_raw_error
@show_raw_error
end
|
Instance Method Details
#backtrace_cleaner ⇒ Object
22
23
24
|
# File 'lib/graphiti_errors/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/graphiti_errors/exception_handler.rb', line 30
def detail(error)
if @message == true
error.message
else
@message ? @message.call(error) : default_detail
end
end
|
#error_code(error) ⇒ Object
17
18
19
20
|
# File 'lib/graphiti_errors/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
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/graphiti_errors/exception_handler.rb', line 54
def error_payload(error)
{
errors: [
code: error_code(error),
status: status_code(error).to_s,
title: title,
detail: detail(error),
meta: meta(error),
],
}
end
|
#log(error) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/graphiti_errors/exception_handler.rb', line 70
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
66
67
68
|
# File 'lib/graphiti_errors/exception_handler.rb', line 66
def log?
@log != false
end
|
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/graphiti_errors/exception_handler.rb', line 38
def meta(error)
{}.tap do |meta_payload|
if @meta.respond_to?(:call)
meta_payload.merge!(@meta.call(error))
end
if show_raw_error
meta_payload[:__raw_error__] = {
message: error.message,
debug: error.instance_variable_get(:@__graphiti_debug),
backtrace: error.backtrace,
}
end
end
end
|
#status_code(error) ⇒ Object
13
14
15
|
# File 'lib/graphiti_errors/exception_handler.rb', line 13
def status_code(error)
@status || 500
end
|
#title ⇒ Object
26
27
28
|
# File 'lib/graphiti_errors/exception_handler.rb', line 26
def title
@title || "Error"
end
|