Sha256: 97aa29782a7b80e0542d60e4e229cb4bbeeb1bb7889950eaf8c487f55bf00952

Contents?: true

Size: 1.9 KB

Versions: 23

Compression:

Stored size: 1.9 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)
    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

23 entries across 23 versions & 1 rubygems

Version Path
fanforce-plugin-factory-2.0.0.rc17 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc16 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc15 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc13 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc12 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc11 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc10 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc9 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc8 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc7 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc6 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc5 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc3 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-1.6.1 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc2 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-2.0.0.rc1 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-1.6.0 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-1.6.0.rc6 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-1.6.0.rc5 lib/fanforce/plugin_factory/sinatra/error_handling.rb
fanforce-plugin-factory-1.6.0.rc4 lib/fanforce/plugin_factory/sinatra/error_handling.rb