Monday, 15 March 2010

Ruby rack middleware to filter all the exceptions raised in the stack -



Ruby rack middleware to filter all the exceptions raised in the stack -

hi had question on how appropriately manage exception handling ruby rack.

i building lightweight api sinatra/rack , every route through exception (e.g. missing parameter, invalid format etc). want able grab of these exceptions , handle them in middleware application. don't want grab or 'rescue' within each get, post, put, delete block (e.g. post '/me')

i tried this, exceptionhandler @ top of rack stack.

i missing something?

class api::exceptionhandler def initialize(application) @application = application end def call(environment) @application.call environment rescue api::missingparametererror => parameter [ 400, { }, [ "parameter #{parameter} missing" ] ] end end

ok, catnapper said, code above should work! didn't mention middleware inheriting class inherits sinatra::base.

and sinatra has own exception handlers, can modified this

set :raise_errors, false set :show_exceptions, false

and how write exception handler in sinatra (for illustration in question):

error api::missingparametererror |parameter| status 400 headers { } body "parameter #{parameter} missing" end

ruby exception-handling rack middleware

No comments:

Post a Comment