OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[38842] Allow customized not found message
Oliver Günther9a3d822b3513
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex ab72615b58a..d8d68579dac 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2989,6 +2989,8 @@ en:code_409: "Could not update the resource because of conflicting modifications."code_429: "Too many requests. Please try again later."code_500: "An internal error has occurred."+ not_found:+ work_package: "The work package you are looking for cannot be found or has been deleted."expected:date: "YYYY-MM-DD (ISO 8601 date only)"duration: "ISO 8601 duration"diff --git a/lib/api/errors/bad_request.rb b/lib/api/errors/bad_request.rbindex 05ecb408549..aa082b051ad 100644--- a/lib/api/errors/bad_request.rb+++ b/lib/api/errors/bad_request.rb@@ -34,7 +34,7 @@ module APIidentifier 'BadRequest'code 400- def initialize(message)+ def initialize(message, **)super I18n.t('api_v3.errors.code_400', message: message)endenddiff --git a/lib/api/errors/error_base.rb b/lib/api/errors/error_base.rbindex e35fb730713..5ab1400635f 100644--- a/lib/api/errors/error_base.rb+++ b/lib/api/errors/error_base.rb@@ -103,9 +103,11 @@ module APIendend- def initialize(message)+ def initialize(message, **)@message = message@errors = []++ super message: messageendendenddiff --git a/lib/api/errors/internal_error.rb b/lib/api/errors/internal_error.rbindex 83a3135064f..c1555476301 100644--- a/lib/api/errors/internal_error.rb+++ b/lib/api/errors/internal_error.rb@@ -34,7 +34,7 @@ module APIidentifier 'InternalServerError'code 500- def initialize(error_message = nil)+ def initialize(error_message = nil, **)error = I18n.t('api_v3.errors.code_500')if error_messagediff --git a/lib/api/errors/not_found.rb b/lib/api/errors/not_found.rbindex e8dc138426b..43303bc11c7 100644--- a/lib/api/errors/not_found.rb+++ b/lib/api/errors/not_found.rb@@ -34,10 +34,20 @@ module APIidentifier 'NotFound'code 404- def initialize(*args)- opts = args.last.is_a?(Hash) ? args.last : {}+ def initialize(_message = nil, exception: nil)+ super(better_model_message(exception) || I18n.t('api_v3.errors.code_404'))+ end++ private++ ##+ # Try to find a localizable error message for+ # the not found error by checking the "model" property set by rails.+ def better_model_message(exception)+ return unless exception&.model- super opts[:message] || I18n.t('api_v3.errors.code_404')+ model_name = exception.model.underscore+ I18n.t("api_v3.errors.not_found.#{model_name}", default: nil)endendenddiff --git a/lib/api/errors/not_implemented.rb b/lib/api/errors/not_implemented.rbindex 1068c364a2e..21bf4881d61 100644--- a/lib/api/errors/not_implemented.rb+++ b/lib/api/errors/not_implemented.rb@@ -34,7 +34,7 @@ module APIidentifier 'NotImplemented'.freezecode 501- def initialize(error_message = 'Not yet implemented'.freeze)+ def initialize(error_message = 'Not yet implemented'.freeze, **)super error_messageendenddiff --git a/lib/api/utilities/grape_helper.rb b/lib/api/utilities/grape_helper.rbindex 45146b360e1..44953f8dce8 100644--- a/lib/api/utilities/grape_helper.rb+++ b/lib/api/utilities/grape_helper.rb@@ -56,7 +56,7 @@ module APIif error.nil?error_response_lambdaelse- lambda { |e| instance_exec error.new(e.message), &error_response_lambda }+ lambda { |e| instance_exec error.new(e.message, exception: e), &error_response_lambda }end# We do this lambda business because #rescue_from behaves differentlydiff --git a/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rb b/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rbindex 6b312e48ca1..500b1693c36 100644--- a/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rb+++ b/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rb@@ -100,7 +100,7 @@ describe 'API v3 Budget resource' doget get_pathend- it_behaves_like 'not found'+ it_behaves_like 'not found', 'User'endendenddiff --git a/modules/costs/spec/requests/api/cost_types/cost_type_resource_spec.rb b/modules/costs/spec/requests/api/cost_types/cost_type_resource_spec.rbindex 1949fb6bb0d..3ac47630144 100644--- a/modules/costs/spec/requests/api/cost_types/cost_type_resource_spec.rb+++ b/modules/costs/spec/requests/api/cost_types/cost_type_resource_spec.rb@@ -61,7 +61,7 @@ describe 'API v3 Cost Type resource' docontext 'cost type deleted' dolet!(:cost_type) { FactoryBot.create(:cost_type, :deleted) }- it_behaves_like 'not found' do+ it_behaves_like 'not found', 'Cost type' dolet(:id) { cost_type.id }endenddiff --git a/spec/requests/api/v3/support/response_examples.rb b/spec/requests/api/v3/support/response_examples.rbindex a2f1fc3c431..f1d3c704eb2 100644--- a/spec/requests/api/v3/support/response_examples.rb+++ b/spec/requests/api/v3/support/response_examples.rb@@ -112,12 +112,11 @@ shared_examples_for 'unauthorized access' doI18n.t('api_v3.errors.code_403')end-shared_examples_for 'not found' do+shared_examples_for 'not found' do |message = I18n.t('api_v3.errors.code_404')|it_behaves_like 'error response',404,- 'NotFound' do- let(:message) { I18n.t('api_v3.errors.code_404') }- end+ 'NotFound',+ messageendshared_examples_for 'param validation error' dodiff --git a/spec/requests/api/v3/watcher_resource_spec.rb b/spec/requests/api/v3/watcher_resource_spec.rbindex 540efc2249e..df52cf7aced 100644--- a/spec/requests/api/v3/watcher_resource_spec.rb+++ b/spec/requests/api/v3/watcher_resource_spec.rb@@ -154,10 +154,8 @@ describe 'API v3 Watcher resource', type: :request, content_type: :json docontext 'when the work package does not exist' dolet(:post_path) { api_v3_paths.work_package_watchers 9999 }- it_behaves_like 'not found' do- let(:id) { 9999 }- let(:type) { 'WorkPackage' }- end+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endcontext 'when the user does not exist' do@@ -243,10 +241,8 @@ describe 'API v3 Watcher resource', type: :request, content_type: :json docontext 'when work package doesn\'t exist' dolet(:delete_path) { api_v3_paths.watcher watching_user.id, 9999 }- it_behaves_like 'not found' do- let(:id) { 9999 }- let(:type) { 'WorkPackage' }- end+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endenddiff --git a/spec/requests/api/v3/work_packages/delete_resource_spec.rb b/spec/requests/api/v3/work_packages/delete_resource_spec.rbindex aff65e317c9..988ae597c6a 100644--- a/spec/requests/api/v3/work_packages/delete_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/delete_resource_spec.rb@@ -76,10 +76,8 @@ describe 'API v3 Work package resource',context 'for a non-existent work package' dolet(:path) { api_v3_paths.work_package 1337 }- it_behaves_like 'not found' do- let(:id) { 1337 }- let(:type) { 'WorkPackage' }- end+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endenddiff --git a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rbindex b5c2499c774..edb2f1281d4 100644--- a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb@@ -77,7 +77,8 @@ describe 'API v3 Work package form resource', type: :request, with_mail: false dlet(:current_user) { unauthorized_user }end- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endcontext 'user with all edit permissions' dodiff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rbindex ff1ddd81595..88e861f2958 100644--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb@@ -181,7 +181,8 @@ describe 'API v3 Work package resource',context 'requesting nonexistent work package' dolet(:get_path) { api_v3_paths.work_package 909090 }- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endend@@ -191,7 +192,8 @@ describe 'API v3 Work package resource',get get_pathend- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endcontext 'when acting as an anonymous user' do@@ -200,7 +202,8 @@ describe 'API v3 Work package resource',get get_pathend- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endendend