From fc024a51fc513ddd5cfefd1b5d92961178040ec1 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 17 Nov 2022 08:50:30 +0100 Subject: [PATCH 1/5] add message argument to not_found method --- app/controllers/application_controller.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b91c0449..f31891523 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -82,13 +82,21 @@ def anonymous_user user ||= User.new({"id" => 0}) end - def not_found + def ontology_not_found(ontology_acronym) + not_found("Ontology #{ontology_acronym} not found") + end + + def concept_not_found(concept_id) + not_found("Concept #{concept_id} not found") + end + + def not_found(message = '') if request.xhr? - render plain: "Error: load failed" + render plain: message || "Error: load failed" return end - - raise ActiveRecord::RecordNotFound.new('Not Found') + + raise ActiveRecord::RecordNotFound.new(message || 'Not Found') end NOTIFICATION_TYPES = { :notes => "CREATE_NOTE_NOTIFICATION", :all => "ALL_NOTIFICATION" } From be374a67cc1ba17fa74623d27b92e1262856c060 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 17 Nov 2022 11:54:52 +0100 Subject: [PATCH 2/5] update files to use ontologies/concepts not found --- app/controllers/ajax_proxy_controller.rb | 8 ++++---- app/controllers/concepts_controller.rb | 18 +++++++++--------- app/controllers/ontologies_controller.rb | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/controllers/ajax_proxy_controller.rb b/app/controllers/ajax_proxy_controller.rb index 56817a1f1..d1feda69a 100644 --- a/app/controllers/ajax_proxy_controller.rb +++ b/app/controllers/ajax_proxy_controller.rb @@ -37,7 +37,7 @@ def jsonp end def json_class - not_found if params[:conceptid].nil? || params[:conceptid].empty? + concept_not_found if params[:conceptid].nil? || params[:conceptid].empty? params[:ontology] ||= params[:ontologyid] if params[:ontologyid].to_i > 0 @@ -48,10 +48,10 @@ def json_class end @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? @concept = @ontology.explore.single_class({}, params[:conceptid]) - not_found if @concept.nil? + concept_not_found(params[:conceptid]) if @concept.nil? render_json @concept.to_json end @@ -59,7 +59,7 @@ def json_class def json_ontology @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? simple_ontology = simplify_ontology_model(@ontology) # application_controller (cached) render_json simple_ontology.to_json end diff --git a/app/controllers/concepts_controller.rb b/app/controllers/concepts_controller.rb index a96c02ceb..a5d5d851d 100644 --- a/app/controllers/concepts_controller.rb +++ b/app/controllers/concepts_controller.rb @@ -21,7 +21,7 @@ def show if request.xhr? display = params[:callback].eql?('load') ? {full: true} : {display: "prefLabel"} @concept = @ontology.explore.single_class(display, params[:id]) - not_found if @concept.nil? + concept_not_found(params[:id]) if @concept.nil? show_ajax_request # process an ajax call else # Get the latest 'ready' submission, or fallback to any latest submission @@ -29,7 +29,7 @@ def show @submission = get_ontology_submission_ready(@ontology) # application_controller @concept = @ontology.explore.single_class({full: true}, params[:id]) - not_found if @concept.nil? + concept_not_found(params[:id]) if @concept.nil? show_uri_request # process a full call render :file => '/ontologies/visualize', :use_full_path => true, :layout => 'ontology' @@ -79,7 +79,7 @@ def show_tree end @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first if @ontology.nil? - not_found + ontology_not_found(params[:ontology]) else get_class(params) # application_controller render :partial => "ontologies/treeview" @@ -94,14 +94,14 @@ def property_tree return end @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? @root = @ontology.property_tree render json: LinkedData::Client::Models::Property.properties_to_hash(@root.children) end # Renders a details pane for a given ontology/concept def details - not_found if params[:conceptid].nil? || params[:conceptid].empty? + concept_not_found('') if params[:conceptid].nil? || params[:conceptid].empty? if params[:ontology].to_i > 0 orig_id = params[:ontology] @@ -112,10 +112,10 @@ def details end @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? @concept = @ontology.explore.single_class({full: true}, CGI.unescape(params[:conceptid])) - not_found if @concept.nil? + concept_not_found(CGI.unescape(params[:conceptid])) if @concept.nil? if params[:styled].eql?("true") render :partial => "details", :layout => "partial" @@ -126,10 +126,10 @@ def details def biomixer @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? @concept = @ontology.explore.single_class({full: true}, params[:conceptid]) - not_found if @concept.nil? + concept_not_found(params[:conceptid]) if @concept.nil? @immediate_load = true diff --git a/app/controllers/ontologies_controller.rb b/app/controllers/ontologies_controller.rb index 5126f9163..36e37f786 100644 --- a/app/controllers/ontologies_controller.rb +++ b/app/controllers/ontologies_controller.rb @@ -264,7 +264,7 @@ def show # Note: find_by_acronym includes ontology views @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(params[:ontology]).first - not_found if @ontology.nil? + ontology_not_found(params[:ontology]) if @ontology.nil? # Handle the case where an ontology is converted to summary only. # See: https://github.com/ncbo/bioportal_web_ui/issues/133. From de4f759f70efcd89e337a69b38408a151c926900 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 17 Nov 2022 08:55:05 +0100 Subject: [PATCH 3/5] add missing roots not found message --- app/controllers/application_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f31891523..92f586e5d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -398,7 +398,7 @@ def get_class(params) roots = @ontology.explore.roots if roots.nil? || roots.empty? LOG.add :debug, "Missing roots for #{@ontology.acronym}" - not_found + not_found("Missing roots for #{@ontology.acronym}") end @root = LinkedData::Client::Models::Class.new(read_only: true) @@ -411,14 +411,14 @@ def get_class(params) # Some ontologies have "too many children" at their root. These will not process and are handled here. if @concept.nil? LOG.add :debug, "Missing class #{root_child.links.self}" - not_found + not_found("Missing class #{root_child.links.self}") end else # if the id is coming from a param, use that to get concept @concept = @ontology.explore.single_class({full: true}, params[:conceptid]) if @concept.nil? || @concept.errors LOG.add :debug, "Missing class #{@ontology.acronym} / #{params[:conceptid]}" - not_found + not_found("Missing class #{@ontology.acronym} / #{params[:conceptid]}") end # Create the tree @@ -427,7 +427,7 @@ def get_class(params) roots = @ontology.explore.roots if roots.nil? || roots.empty? LOG.add :debug, "Missing roots for #{@ontology.acronym}" - not_found + not_found("Missing roots for #{@ontology.acronym}") end if roots.any? {|c| c.id == @concept.id} rootNode = roots From 0513683764cd1137f751f9f357f43ad546a73380 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 17 Nov 2022 11:02:09 +0100 Subject: [PATCH 4/5] custom the error not found message --- app/controllers/application_controller.rb | 7 ++++++- app/views/errors/not_found.html.erb | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 92f586e5d..553cb0726 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,7 +15,7 @@ class ApplicationController < ActionController::Base helper :all # include all helpers, all the time helper_method :bp_config_json, :current_license, :using_captcha? - + rescue_from ActiveRecord::RecordNotFound, with: :not_found_record # Pull configuration parameters for REST connection. REST_URI = $REST_URL API_KEY = $API_KEY @@ -694,4 +694,9 @@ def init_trial_license end end + private + def not_found_record(exception) + @error_message = exception.message + render 'errors/not_found', status: 404 + end end diff --git a/app/views/errors/not_found.html.erb b/app/views/errors/not_found.html.erb index 1af4c7b49..2a0701060 100644 --- a/app/views/errors/not_found.html.erb +++ b/app/views/errors/not_found.html.erb @@ -1,3 +1,5 @@ <%= content_tag :div, id: "bd", style: "clear: both; text-align: center; margin-top: 100px; margin-bottom: 100px;" do -%> -

The page you are looking for wasn't found. Please try again.

+

+ <%= @error_message || "The page you are looking for wasn't found. Please try again." %> +

<% end -%> \ No newline at end of file From 6e6d672fb709f8a6aa8ab5b452946e48cf7e0a2d Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 17 Nov 2022 11:57:32 +0100 Subject: [PATCH 5/5] use ontology_not_found in show_label and redirect_new_api --- app/controllers/application_controller.rb | 2 +- app/controllers/concepts_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 553cb0726..668e04c16 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -233,7 +233,7 @@ def redirect_new_api(class_view = false) return end acronym = BPIDResolver.id_to_acronym(params[:ontology]) - not_found unless acronym + ontology_not_found(acronym) unless acronym if class_view @ontology = LinkedData::Client::Models::Ontology.find_by_acronym(acronym).first concept = get_class(params).first.to_s diff --git a/app/controllers/concepts_controller.rb b/app/controllers/concepts_controller.rb index a5d5d851d..13e844301 100644 --- a/app/controllers/concepts_controller.rb +++ b/app/controllers/concepts_controller.rb @@ -47,7 +47,7 @@ def show_label end @ontology = LinkedData::Client::Models::Ontology.find(ont_id) @ontology ||= LinkedData::Client::Models::Ontology.find_by_acronym(ont_id).first - not_found unless @ontology + ontology_not_found(ont_id) unless @ontology # Retrieve a class prefLabel or return the class ID (URI) # - mappings may contain class URIs that are not in bioportal (e.g. obo-xrefs) cls = @ontology.explore.single_class(cls_id)