@@ -107,6 +107,7 @@ class Routes < Sinatra::Base
107107 # an empty body if the job hasn't finished yet.
108108 get '/:jid.json' do |jid |
109109 job = Job . fetch ( jid )
110+ halt 404 , { error : 'Job not found' } . to_json if job . nil?
110111 halt 202 unless job . done?
111112
112113 report = Report . generate ( job )
@@ -134,10 +135,12 @@ class Routes < Sinatra::Base
134135
135136 # Returns base HTML. Rest happens client-side: polling for and rendering
136137 # the results.
137- get '/:jid' do
138+ get '/:jid' do |jid |
139+ job = Job . fetch ( jid )
140+ raise NotFound , 'Job not found' if job . nil?
141+
138142 erb :report , layout : true
139143 end
140-
141144 # @params sequence_ids: whitespace separated list of sequence ids to
142145 # retrieve
143146 # @params database_ids: whitespace separated list of database ids to
@@ -167,14 +170,17 @@ class Routes < Sinatra::Base
167170 # Download BLAST report in various formats.
168171 get '/download/:jid.:type' do |jid , type |
169172 job = Job . fetch ( jid )
173+ halt 404 , { error : 'Job not found' } . to_json if job . nil?
170174 out = BLAST ::Formatter . new ( job , type )
175+ halt 404 , { error : 'File not found"' } . to_json unless File . exist? ( out . filepath )
171176 send_file out . filepath , filename : out . filename , type : out . mime
172177 end
173178
174179 post '/cloud_share' do
175180 content_type :json
176181 request_params = JSON . parse ( request . body . read )
177182 job = Job . fetch ( request_params [ 'job_id' ] )
183+ halt 404 , { error : 'Job not found' } . to_json if job . nil?
178184
179185 unless job . done?
180186 status 422
@@ -279,7 +285,8 @@ class Routes < Sinatra::Base
279285
280286 # Get the query sequences, selected databases, and advanced params used.
281287 def update_searchdata_from_job ( searchdata )
282- job = Job . fetch ( params [ :job_id ] )
288+ job = fetch_job ( params [ :job_id ] )
289+ return { error : 'Job not found' } . to_json if job . nil?
283290 return if job . imported_xml_file
284291
285292 # Only read job.qfile if we are not going to use Database.retrieve.
@@ -301,5 +308,11 @@ def update_searchdata_from_job(searchdata)
301308 searchdata [ :options ] [ method ] [ 'last search' ] = [ job . advanced ]
302309 end
303310 end
311+
312+ private
313+
314+ def fetch_job ( job_id )
315+ Job . fetch ( job_id )
316+ end
304317 end
305318end
0 commit comments