Reputation: 198496
Currently, I have something like this:
def valid?(stuff)
#...
end
get '/somewhere'
return status 403 unless valid?(something) && valid?(something_else)
# ...
end
(In this specific case, I am checking whether a param
containing a file name is inside the directory allowed for that parameter, to prevent users from accessing what they shouldn't.)
But it gets unwieldy, especially since I have the same structure in multiple places. I'd rather do something like this:
def ensure_valid(stuff)
raise Forbidden unless valid?(stuff)
end
get '/somewhere'
ensure_valid(something)
ensure_valid(something_else)
# ...
end
Is there such an exception? If not, how can I configure Sinatra (or Rack, for that matter) to abort the request with status 403
if it catches a custom exception?
Upvotes: 1
Views: 2523
Reputation: 4340
halt 403 unless valid?(something) && valid?(something_else)
should also work
source: http://www.sinatrarb.com/intro.html#Halting
Upvotes: 3
Reputation: 26
Maybe what you're looking for is the "error" block?
http://www.sinatrarb.com/intro.html#Error
error MyCustomError do
return status 403
end
raise MyCustomError unless valid?(thing)
Upvotes: 1