class Beekeeper::TrapApiExceptions

Public Class Methods

new(app) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 11
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 15
def call(env)
  request = ActionDispatch::Request.new env
  @app.call(env)
rescue StandardError => e
  # if request.path_info.include? '/api'
    render_json_error(request, e)
  # else
  #   raise e
  # end
end

Private Instance Methods

get_error(status) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 47
def get_error(status)
  Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500])
end
get_errors(exception) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 51
def get_errors(exception)
  return [
    {
      code: exception.class.name,
      message: exception.message
    }
  ]
end
get_status(request, exception) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 41
def get_status(request, exception)
  backtrace_cleaner = request.get_header 'action_dispatch.backtrace_cleaner'
  wrapper = ::ActionDispatch::ExceptionWrapper.new(backtrace_cleaner, exception)
  return wrapper.status_code
end
render_json_error(request, exception) click to toggle source
# File lib/beekeeper/trap_api_exceptions.rb, line 28
def render_json_error(request, exception)
  response = ExceptionBlueprinter.render_as_hash(exception, view: :basic, request: request)
  body = response.to_json
  return [
    response[:status],
    {
      'Content-Type' => "json; charset=#{::ActionDispatch::Response.default_charset}",
      'Content-Length' => body.bytesize.to_s
    },
    [body]
  ]
end