Module: Onsi::ErrorResponder

Extended by:
ActiveSupport::Concern
Defined in:
lib/onsi/error_responder.rb

Instance Method Summary collapse

Instance Method Details

#notify_unhandled_exception(exception) ⇒ Object



92
93
94
# File 'lib/onsi/error_responder.rb', line 92

def notify_unhandled_exception(exception)
  Rails.logger.error "Unhandled Exception `#{exception.class.name}: #{exception.message}`"
end

#render_error(response) ⇒ Object



17
18
19
# File 'lib/onsi/error_responder.rb', line 17

def render_error(response)
  render(response.renderable)
end

#render_error_404(_error) ⇒ Object



28
29
30
31
32
# File 'lib/onsi/error_responder.rb', line 28

def render_error_404(_error)
  response = ErrorResponse.new(404)
  response.add(404, 'not_found')
  render_error(response)
end

#render_error_422(error) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/onsi/error_responder.rb', line 34

def render_error_422(error)
  response = ErrorResponse.new(422)
  error.record.errors.details.each do |name, details|
    details.each do |info|
      response.add(
        422,
        'validation_error',
        title: "Validation Error: #{info[:error]}",
        meta: info.merge(param: name)
      )
    end
  end
  render_error(response)
end

#render_error_500(error) ⇒ Object



21
22
23
24
25
26
# File 'lib/onsi/error_responder.rb', line 21

def render_error_500(error)
  notify_unhandled_exception(error)
  response = ErrorResponse.new(500)
  response.add(500, 'internal_server_error', meta: (error))
  render_error(response)
end

#respond_invalid_version_error_400(error) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/onsi/error_responder.rb', line 69

def respond_invalid_version_error_400(error)
  notify_unhandled_exception(error)
  response = ErrorResponse.new(400)
  response.add(
    400,
    'invalid_version',
    details: "API version #{error.version} unsupported for #{error.klass.name.underscore}"
  )
  render_error(response)
end

#respond_missing_attr_error_400(error) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/onsi/error_responder.rb', line 80

def respond_missing_attr_error_400(error)
  response = ErrorResponse.new(400)
  response.add(
    400,
    'missing_attribute',
    meta: {
      attribute: error.attribute
    }
  )
  render_error(response)
end

#respond_missing_relationship_error_400(error) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/onsi/error_responder.rb', line 59

def respond_missing_relationship_error_400(error)
  response = ErrorResponse.new(400)
  response.add(
    400,
    'missing_relationship',
    meta: { param: error.key }
  )
  render_error(response)
end

#respond_param_error_400(error) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/onsi/error_responder.rb', line 49

def respond_param_error_400(error)
  response = ErrorResponse.new(400)
  response.add(
    400,
    'missing_parameter',
    meta: { param: error.param }
  )
  render_error(response)
end