class Fanforce::App::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::App.config.show_error_details admin_details = fetch_admin_details(e, errors) Bugsnag.notify(e, admin_details.merge(app_id: Fanforce::App.app_id)) if Fanforce::App.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