module EndpointBase::Concerns
  module ExceptionHandler
    extend ActiveSupport::Concern

    included do
      if EndpointBase.rails?
        rescue_from Exception, :with => :rails_exception_handler
      elsif EndpointBase.sinatra?
        error do
          log_exception(env['sinatra.error'])

          result 500, env['sinatra.error'].message
        end
      end
    end

    private

    def rails_exception_handler(exception)
      log_exception(exception)

      result 500, exception.message
      return false
    end

    def log_exception(exception)
      if Object.const_defined?('Honeybadger')
        Honeybadger.notify(exception, { context: { request: @payload } })
      end

      if Object.const_defined?('Airbrake')
        Airbrake.notify(exception, parameters: @payload)
      end
    end
  end
end

module EndpointBase::Concerns
  module ExceptionHandler
    extend ActiveSupport::Concern

    included do
      if EndpointBase.rails?
        rescue_from Exception, :with => :rails_exception_handler
      elsif EndpointBase.sinatra?
        # Ensure error handlers run
        set :show_exceptions, false

        error do
          log_exception(env['sinatra.error'])
          result 500, env['sinatra.error'].message
        end
      end
    end

    private

    def rails_exception_handler(exception)
      log_exception(exception)

      result 500, exception.message
      return false
    end

    def log_exception(exception)
      if Object.const_defined?('Honeybadger')
        Honeybadger.notify(exception, { context: { request: @payload } })
      end

      if Object.const_defined?('Airbrake')
        Airbrake.notify(exception, parameters: @payload)
      end
    end
  end
end

Make sure all sinatra error handlers run
Make sure all sinatra error handlers run

See sinatra implementation for reference:

https://github.com/sinatra/sinatra/blob/77438afd761e30610c9b1efda38d39fce1d1d01b/lib/sinatra/base.rb#L1097-L1124
