class Serf::Middleware::ErrorHandler

Middleware to catch raised exceptions and return an error parcel instead.

Attributes

app[R]
parcel_factory[R]

Public Class Methods

new(app, *args) click to toggle source

@param app the app

# File lib/serf/middleware/error_handler.rb, line 23
def initialize(app, *args)
  opts = Optser.extract_options! args
  @app = app

  # Tunable knobs
  @parcel_factory = opts.get(:parcel_factory) { Serf::ParcelFactory.new }
end

Public Instance Methods

call(parcel) click to toggle source
# File lib/serf/middleware/error_handler.rb, line 31
def call(parcel)
  # Attempt to execute the app, catching errors
  response_parcel, error_message = with_error_handling do
    app.call parcel
  end

  # Return on success
  return response_parcel if response_parcel

  # We got an error message, so build out and return the error parcel
  return parcel_factory.create(
    kind: 'serf/events/caught_error',
    parent: parcel,
    message: error_message)
end