Class: ShowJsonExceptions::Middleware

Inherits:
ActionDispatch::ShowExceptions
  • Object
show all
Defined in:
lib/show_json_exceptions.rb

Instance Method Summary collapse

Instance Method Details

#render_exception(env, exception) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/show_json_exceptions.rb', line 8

def render_exception(env, exception)
  log_error(exception)
  exception = original_exception(exception)

  request = ActionDispatch::Request.new(env)
  exception_data = ["Exception: #{exception.message}"]

  if @consider_all_requests_local || request.local?
    exception_data << {backtrace: exception.backtrace}
  end

  render status_code(exception), {
          successful: false,
          result: {},
          error_messages: exception_data
  }.to_json
rescue Exception => failsafe_error
  $stderr.puts "Error during failsafe response: #{failsafe_error}\n  #{failsafe_error.backtrace * "\n  "}"
  FAILSAFE_RESPONSE
end

#render_json(status, body) ⇒ Object



29
30
31
# File 'lib/show_json_exceptions.rb', line 29

def render_json(status, body)
  [status, {'Content-Type' => "application/json; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
end