Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pull rescues into a method

  • Loading branch information...
commit c5e17585040d50aa7c394fb55b2b1d51c32aebd4 1 parent b500bb2
@bmarini bmarini authored ches committed
Showing with 14 additions and 8 deletions.
  1. +14 −8 lib/rack/gridfs/endpoint/base.rb
View
22 lib/rack/gridfs/endpoint/base.rb
@@ -15,15 +15,13 @@ def initialize(options = {})
end
def call(env)
- request = Rack::Request.new(env)
- id = identifier_for_path(request.path_info)
- file = find_file(id)
+ with_rescues do
+ request = Rack::Request.new(env)
+ id = identifier_for_path(request.path_info)
+ file = find_file(id)
- response_for(file, request)
- rescue Mongo::GridError, BSON::InvalidObjectId
- [404, {'Content-Type' => 'text/plain'}, ['File not found.' + id]]
- rescue Mongo::GridFileNotFound
- [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
+ response_for(file, request)
+ end
end
def identifier_for_path(path)
@@ -36,6 +34,14 @@ def db
protected
+ def with_rescues
+ yield
+ rescue Mongo::GridError, BSON::InvalidObjectId => e
+ [ 404, {'Content-Type' => 'text/plain'}, ["File not found. #{e}"] ]
+ rescue Mongo::GridFileNotFound
+ [ 404, {'Content-Type' => 'text/plain'}, ['File not found.'] ]
+ end
+
def response_for(file, request)
[ 200, headers(file), file ]
end
Please sign in to comment.
Something went wrong with that request. Please try again.