From a1e196ecbfcd40adbe6221c264adcf653a388d57 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Mon, 6 Jun 2011 16:54:56 +0000 Subject: halts (partially) substituted by OpenTox errors --- application.rb | 4 ++-- lazar.rb | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/application.rb b/application.rb index 6e1dcd7..c03d1b7 100644 --- a/application.rb +++ b/application.rb @@ -23,7 +23,7 @@ before do @uri = uri @id @yaml_file = "public/#{@id}.yaml" - halt 404, "Model #{@id} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{@id} not found." unless File.exists? @yaml_file end # make sure subjectid is not included in params, subjectid is set as member variable @@ -77,7 +77,7 @@ delete '/:id/?' do response['Content-Type'] = 'text/plain' "Model #{@id} deleted." rescue - halt 404, "Model #{@id} does not exist." + raise OpenTox::NotFoundError.new "Model #{@id} does not exist." end end diff --git a/lazar.rb b/lazar.rb index 9b3d07b..fa54ed6 100644 --- a/lazar.rb +++ b/lazar.rb @@ -3,7 +3,7 @@ require "haml" # Get model representation # @return [application/rdf+xml,application/x-yaml] Model representation get '/:id/?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file case @accept when /application\/rdf\+xml/ response['Content-Type'] = 'application/rdf+xml' @@ -18,12 +18,12 @@ get '/:id/?' do response['Content-Type'] = 'text/html' OpenTox.text_to_html File.read(@yaml_file) else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end get '/:id/metadata.?:ext?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) metadata = YAML.load_file(@yaml_file).metadata case @accept @@ -37,7 +37,7 @@ get '/:id/metadata.?:ext?' do end get '/:id/dependent.?:ext?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) feature_uri = YAML.load_file(@yaml_file).metadata[OT.dependentVariables] case @accept @@ -50,12 +50,12 @@ get '/:id/dependent.?:ext?' do when /html/ OpenTox.text_to_html OpenTox::Feature.find(feature_uri).to_yaml else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end get '/:id/predicted/:prop' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file if params[:prop] == "value" or params[:prop] == "confidence" feature = eval "YAML.load_file(@yaml_file).prediction_#{params[:prop]}_feature" case @accept @@ -69,15 +69,15 @@ get '/:id/predicted/:prop' do content_type "text/html" OpenTox.text_to_html feature.metadata.to_yaml else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end else - halt 400, "Unknown URI #{@uri}" + raise OpenTox::BadRequestError.new "Unknown URI #{@uri}" end end get '/:id/predicted.?:ext?' do - halt 404, "Model #{params[:id]} not found." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} not found." unless File.exists? @yaml_file @accept = "application/x-yaml" if params[:ext] and params[:ext].match(/yaml/) features = YAML.load_file(@yaml_file).prediction_features case @accept @@ -93,7 +93,7 @@ get '/:id/predicted.?:ext?' do when /html/ OpenTox.text_to_html features.to_yaml else - halt 400, "Unsupported MIME type '#{@accept}'" + raise OpenTox::BadRequestError.new "Unsupported MIME type '#{@accept}'" end end @@ -101,7 +101,7 @@ end # @param [Body] lazar Model representation in YAML format # @return [String] Model URI post '/?' do # create model - halt 400, "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) + raise OpenTox::BadRequestError.new "MIME type \"#{request.content_type}\" not supported." unless request.content_type.match(/yaml/) @id = next_id @uri = uri @id @yaml_file = "public/#{@id}.yaml" @@ -123,9 +123,9 @@ end # @return [text/uri-list] URI of prediction task (dataset prediction) or prediction dataset (compound prediction) post '/:id/?' do - halt 404, "Model #{params[:id]} does not exist." unless File.exists? @yaml_file + raise OpenTox::NotFoundError.new "Model #{params[:id]} does not exist." unless File.exists? @yaml_file - halt 404, "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] + raise OpenTox::NotFoundError.new "No compound_uri or dataset_uri parameter." unless compound_uri = params[:compound_uri] or dataset_uri = params[:dataset_uri] @lazar = YAML.load_file @yaml_file response['Content-Type'] = 'text/uri-list' @@ -141,14 +141,14 @@ post '/:id/?' do prediction_uri rescue LOGGER.error "Lazar prediction failed for #{compound_uri} with #{$!} " - halt 500, "Prediction of #{compound_uri} with #{@lazar.uri} failed." + raise "Prediction of #{compound_uri} with #{@lazar.uri} failed." end end elsif dataset_uri task = OpenTox::Task.create("Predict dataset",url_for("/#{@lazar.id}", :full)) do |task| @lazar.predict_dataset(dataset_uri, @subjectid, task).uri end - halt 503,task.uri+"\n" if task.status == "Cancelled" + raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled" halt 202,task.uri end -- cgit v1.2.3