|
2 | 2 |
|
3 | 3 | module Grape
|
4 | 4 | module ErrorFormatter
|
5 |
| - module Base |
6 |
| - def present(message, env) |
7 |
| - present_options = {} |
8 |
| - presented_message = message |
9 |
| - if presented_message.is_a?(Hash) |
10 |
| - presented_message = presented_message.dup |
11 |
| - present_options[:with] = presented_message.delete(:with) |
| 5 | + class Base |
| 6 | + class << self |
| 7 | + def call(message, backtrace, options = {}, env = nil, original_exception = nil) |
| 8 | + merge_backtrace = backtrace.present? && options.dig(:rescue_options, :backtrace) |
| 9 | + merge_original_exception = original_exception && options.dig(:rescue_options, :original_exception) |
| 10 | + |
| 11 | + wrapped_message = wrap_message(present(message, env)) |
| 12 | + if wrapped_message.is_a?(Hash) |
| 13 | + wrapped_message[:backtrace] = backtrace if merge_backtrace |
| 14 | + wrapped_message[:original_exception] = original_exception.inspect if merge_original_exception |
| 15 | + end |
| 16 | + |
| 17 | + format_structured_message(wrapped_message) |
12 | 18 | end
|
13 | 19 |
|
14 |
| - presenter = env[Grape::Env::API_ENDPOINT].entity_class_for_obj(presented_message, present_options) |
| 20 | + def present(message, env) |
| 21 | + present_options = {} |
| 22 | + presented_message = message |
| 23 | + if presented_message.is_a?(Hash) |
| 24 | + presented_message = presented_message.dup |
| 25 | + present_options[:with] = presented_message.delete(:with) |
| 26 | + end |
| 27 | + |
| 28 | + presenter = env[Grape::Env::API_ENDPOINT].entity_class_for_obj(presented_message, present_options) |
| 29 | + |
| 30 | + unless presenter || env[Grape::Env::GRAPE_ROUTING_ARGS].nil? |
| 31 | + # env['api.endpoint'].route does not work when the error occurs within a middleware |
| 32 | + # the Endpoint does not have a valid env at this moment |
| 33 | + http_codes = env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info].http_codes || [] |
| 34 | + |
| 35 | + found_code = http_codes.find do |http_code| |
| 36 | + (http_code[0].to_i == env[Grape::Env::API_ENDPOINT].status) && http_code[2].respond_to?(:represent) |
| 37 | + end if env[Grape::Env::API_ENDPOINT].request |
15 | 38 |
|
16 |
| - unless presenter || env[Grape::Env::GRAPE_ROUTING_ARGS].nil? |
17 |
| - # env['api.endpoint'].route does not work when the error occurs within a middleware |
18 |
| - # the Endpoint does not have a valid env at this moment |
19 |
| - http_codes = env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info].http_codes || [] |
| 39 | + presenter = found_code[2] if found_code |
| 40 | + end |
20 | 41 |
|
21 |
| - found_code = http_codes.find do |http_code| |
22 |
| - (http_code[0].to_i == env[Grape::Env::API_ENDPOINT].status) && http_code[2].respond_to?(:represent) |
23 |
| - end if env[Grape::Env::API_ENDPOINT].request |
| 42 | + if presenter |
| 43 | + embeds = { env: env } |
| 44 | + embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION) |
| 45 | + presented_message = presenter.represent(presented_message, embeds).serializable_hash |
| 46 | + end |
24 | 47 |
|
25 |
| - presenter = found_code[2] if found_code |
| 48 | + presented_message |
26 | 49 | end
|
27 | 50 |
|
28 |
| - if presenter |
29 |
| - embeds = { env: env } |
30 |
| - embeds[:version] = env[Grape::Env::API_VERSION] if env.key?(Grape::Env::API_VERSION) |
31 |
| - presented_message = presenter.represent(presented_message, embeds).serializable_hash |
| 51 | + def wrap_message(message) |
| 52 | + return message if message.is_a?(Hash) |
| 53 | + |
| 54 | + { message: message } |
| 55 | + end |
| 56 | + |
| 57 | + def format_structured_message(_structured_message) |
| 58 | + raise NotImplementedError |
32 | 59 | end
|
33 | 60 |
|
34 |
| - presented_message |
| 61 | + def inherited(klass) |
| 62 | + super |
| 63 | + ErrorFormatter.register(klass) |
| 64 | + end |
35 | 65 | end
|
36 | 66 | end
|
37 | 67 | end
|
|
0 commit comments