Class: Serf::Middleware::ErrorHandler
- Inherits:
-
Object
- Object
- Serf::Middleware::ErrorHandler
- 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
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#parcel_factory ⇒ Object
readonly
Returns the value of attribute parcel_factory.
Instance Method Summary collapse
- #call(parcel) ⇒ Object
-
#initialize(app, *args) ⇒ ErrorHandler
constructor
A new instance of ErrorHandler.
Methods included from Util::ErrorHandling
#handle_error, #with_error_handling
Methods included from Util::ProtectedCall
Constructor Details
#initialize(app, *args) ⇒ ErrorHandler
Returns a new instance of ErrorHandler.
23 24 25 26 27 28 29 |
# File 'lib/serf/middleware/error_handler.rb', line 23 def initialize(app, *args) opts = Optser. args @app = app # Tunable knobs @parcel_factory = opts.get(:parcel_factory) { Serf::ParcelFactory.new } end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
17 18 19 |
# File 'lib/serf/middleware/error_handler.rb', line 17 def app @app end |
#parcel_factory ⇒ Object (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, = 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: ) end |