Sha256: 283543153450ca2d2209398510fea6247d2041c841551e66fbb46b66a72a35d7

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

class Fanforce::Plugin::Sinatra
  set :raise_errors, false
  set :show_exceptions, false

  error 400..600 do
    e = env['sinatra.error'] or (next response if e.nil?)

    if Object.const_defined?('Fanforce') and Fanforce.const_defined?('API') and e.is_a?(Fanforce::API::Error)
      errors = e.errors
    else
      errors = [{message: 'There has been an internal server error. Our developers have been notified.'}]
    end
    message = errors[0][:message]

    show_error_details = params[:show_error_details] || Fanforce::Plugin.config.show_error_details
    admin_details = fetch_admin_details(e, errors)
    Bugsnag.notify(e, admin_details.merge(plugin_id: Fanforce::Plugin.plugin_id)) if Fanforce::Plugin.is_bugsnag_enabled?

    body = {errors: message}
    body.merge!(admin_details.merge(backtrace: e.backtrace)).delete(:public_response) if show_error_details

    status(e.respond_to?(:code ) ? e.code : 500)
    if show_error_details == 'text'
      response.headers['Content-Type'] = 'text/plain'
      format_as_text(body)
    else
      json(body) rescue body.to_s
    end
  end

  def format_as_text(body)
    text =  "--------------------------------------------------------------------------------------------------------\n"
    text += "ERROR: #{body[:raw_message]}\n"
    text += "CLASS: #{body[:error_class]}\n" if body[:error_class]
    text += "\nCURL COMMAND: #{body[:curl_command]}\n" if body[:curl_command]
    text += "\nBACKTRACE:\n#{body[:backtrace].join("\n")}\n" if body[:backtrace]
    text += "--------------------------------------------------------------------------------------------------------\n"
  end

  def fetch_admin_details(e, errors=[], additional_options={})
    admin_details = {error_class: e.class.name, public_response: errors}
    admin_details[:curl_command] = e.curl_command if e.respond_to?(:curl_command)
    admin_details[:raw_message] = e.message
    admin_details.merge(additional_options)
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
fanforce-plugin-factory-2.0.0.rc29 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc28 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc27 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc26 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc25 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc24 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc23 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc22 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc21 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc19 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc18 lib/fanforce/plugin_factory/sinatra/error_handling.rb