Class: Serf::Middleware::ErrorHandler

Inherits:
Object
  • Object
show all
Includes:
Util::ErrorHandling
Defined in:
lib/serf/middleware/error_handler.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::ErrorHandling

#handle_error, #with_error_handling

Methods included from Util::ProtectedCall

#pcall

Constructor Details

#initialize(app, *args) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.

Parameters:

  • app

    the app



23
24
25
26
27
28
29
# 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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



17
18
19
# File 'lib/serf/middleware/error_handler.rb', line 17

def app
  @app
end

#parcel_factoryObject (readonly)

Returns the value of attribute parcel_factory.



18
19
20
# File 'lib/serf/middleware/error_handler.rb', line 18

def parcel_factory
  @parcel_factory
end

Instance Method Details

#call(parcel) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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