Class: Graphiti::Rails::InvalidRequestHandler

Inherits:
ExceptionHandler
  • Object
show all
Defined in:
lib/graphiti/rails/exception_handlers.rb

Instance Method Summary collapse

Methods inherited from ExceptionHandler

#formatted_response

Instance Method Details

#build_payload(show_details: false, traces: nil, style: nil) ⇒ Object

Mostly copied from GraphitiErrors could use some cleanup NOTE: That ‘style` is ignored



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphiti/rails/exception_handlers.rb', line 61

def build_payload(show_details: false, traces: nil, style: nil)
  errors = exception.errors

  errors_payload = []
  errors.details.each_pair do |attribute, att_errors|
    att_errors.each_with_index do |error, idx|
      code = error[:error]
      message = errors.messages[attribute][idx]

      errors_payload << {
        code: "bad_request",
        status: "400",
        title: "Request Error",
        detail: errors.full_message(attribute, message),
        source: {
          pointer: attribute.to_s.tr(".", "/").gsub(/\[(\d+)\]/, '/\1'),
        },
        meta: {
          attribute: attribute,
          message: message,
          code: code,
        },
      }
    end
  end

  { errors: errors_payload }
end