From 22aa5e9c2575a317f519fa12d041b6432877249c Mon Sep 17 00:00:00 2001 From: gorsyan <79786004+gorsyan@users.noreply.github.com> Date: Thu, 21 Dec 2023 21:52:58 +0000 Subject: [PATCH] Fixed the job 404 exception error #666 (#710) * Fix Exception Handling and Implement Error Page * Fixed the issues that arose from Code Climate * Last Fix of routes.rb issues --- lib/sequenceserver/job.rb | 2 +- lib/sequenceserver/routes.rb | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/sequenceserver/job.rb b/lib/sequenceserver/job.rb index 9f71500a5..cfca2f8dc 100644 --- a/lib/sequenceserver/job.rb +++ b/lib/sequenceserver/job.rb @@ -41,7 +41,7 @@ def serializable_classes # Fetches job with the given id. def fetch(id) job_file = File.join(DOTDIR, id, 'job.yaml') - fail NotFound unless File.exist?(job_file) + return nil unless File.exist?(job_file) YAML.safe_load_file( job_file, diff --git a/lib/sequenceserver/routes.rb b/lib/sequenceserver/routes.rb index d3aed40aa..2e17de041 100644 --- a/lib/sequenceserver/routes.rb +++ b/lib/sequenceserver/routes.rb @@ -107,6 +107,7 @@ class Routes < Sinatra::Base # an empty body if the job hasn't finished yet. get '/:jid.json' do |jid| job = Job.fetch(jid) + halt 404, { error: 'Job not found' }.to_json if job.nil? halt 202 unless job.done? report = Report.generate(job) @@ -134,10 +135,12 @@ class Routes < Sinatra::Base # Returns base HTML. Rest happens client-side: polling for and rendering # the results. - get '/:jid' do + get '/:jid' do |jid| + job = Job.fetch(jid) + raise NotFound, 'Job not found' if job.nil? + erb :report, layout: true end - # @params sequence_ids: whitespace separated list of sequence ids to # retrieve # @params database_ids: whitespace separated list of database ids to @@ -167,7 +170,9 @@ class Routes < Sinatra::Base # Download BLAST report in various formats. get '/download/:jid.:type' do |jid, type| job = Job.fetch(jid) + halt 404, { error: 'Job not found' }.to_json if job.nil? out = BLAST::Formatter.new(job, type) + halt 404, { error: 'File not found"' }.to_json unless File.exist?(out.filepath) send_file out.filepath, filename: out.filename, type: out.mime end @@ -175,6 +180,7 @@ class Routes < Sinatra::Base content_type :json request_params = JSON.parse(request.body.read) job = Job.fetch(request_params['job_id']) + halt 404, { error: 'Job not found' }.to_json if job.nil? unless job.done? status 422 @@ -279,7 +285,8 @@ class Routes < Sinatra::Base # Get the query sequences, selected databases, and advanced params used. def update_searchdata_from_job(searchdata) - job = Job.fetch(params[:job_id]) + job = fetch_job(params[:job_id]) + return { error: 'Job not found' }.to_json if job.nil? return if job.imported_xml_file # Only read job.qfile if we are not going to use Database.retrieve. @@ -301,5 +308,11 @@ def update_searchdata_from_job(searchdata) searchdata[:options][method]['last search'] = [job.advanced] end end + + private + + def fetch_job(job_id) + Job.fetch(job_id) + end end end