Skip to content

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

[38842] Allow customized not found message

Oliver Günther9a3d822b3513
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 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.rb
index 05ecb408549..aa082b051ad 100644
--- a/lib/api/errors/bad_request.rb
+++ b/lib/api/errors/bad_request.rb
@@ -34,7 +34,7 @@ module API
identifier 'BadRequest'
code 400
- def initialize(message)
+ def initialize(message, **)
super I18n.t('api_v3.errors.code_400', message: message)
end
end
diff --git a/lib/api/errors/error_base.rb b/lib/api/errors/error_base.rb
index e35fb730713..5ab1400635f 100644
--- a/lib/api/errors/error_base.rb
+++ b/lib/api/errors/error_base.rb
@@ -103,9 +103,11 @@ module API
end
end
- def initialize(message)
+ def initialize(message, **)
@message = message
@errors = []
+
+ super message: message
end
end
end
diff --git a/lib/api/errors/internal_error.rb b/lib/api/errors/internal_error.rb
index 83a3135064f..c1555476301 100644
--- a/lib/api/errors/internal_error.rb
+++ b/lib/api/errors/internal_error.rb
@@ -34,7 +34,7 @@ module API
identifier 'InternalServerError'
code 500
- def initialize(error_message = nil)
+ def initialize(error_message = nil, **)
error = I18n.t('api_v3.errors.code_500')
if error_message
diff --git a/lib/api/errors/not_found.rb b/lib/api/errors/not_found.rb
index e8dc138426b..43303bc11c7 100644
--- a/lib/api/errors/not_found.rb
+++ b/lib/api/errors/not_found.rb
@@ -34,10 +34,20 @@ module API
identifier '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)
end
end
end
diff --git a/lib/api/errors/not_implemented.rb b/lib/api/errors/not_implemented.rb
index 1068c364a2e..21bf4881d61 100644
--- a/lib/api/errors/not_implemented.rb
+++ b/lib/api/errors/not_implemented.rb
@@ -34,7 +34,7 @@ module API
identifier 'NotImplemented'.freeze
code 501
- def initialize(error_message = 'Not yet implemented'.freeze)
+ def initialize(error_message = 'Not yet implemented'.freeze, **)
super error_message
end
end
diff --git a/lib/api/utilities/grape_helper.rb b/lib/api/utilities/grape_helper.rb
index 45146b360e1..44953f8dce8 100644
--- a/lib/api/utilities/grape_helper.rb
+++ b/lib/api/utilities/grape_helper.rb
@@ -56,7 +56,7 @@ module API
if error.nil?
error_response_lambda
else
- 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 differently
diff --git a/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rb b/modules/budgets/spec/requests/api/v3/budgets/budget_resource_spec.rb
index 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' do
get get_path
end
- it_behaves_like 'not found'
+ it_behaves_like 'not found', 'User'
end
end
end
diff --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.rb
index 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' do
context 'cost type deleted' do
let!(:cost_type) { FactoryBot.create(:cost_type, :deleted) }
- it_behaves_like 'not found' do
+ it_behaves_like 'not found', 'Cost type' do
let(:id) { cost_type.id }
end
end
diff --git a/spec/requests/api/v3/support/response_examples.rb b/spec/requests/api/v3/support/response_examples.rb
index 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' do
I18n.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',
+ message
end
shared_examples_for 'param validation error' do
diff --git a/spec/requests/api/v3/watcher_resource_spec.rb b/spec/requests/api/v3/watcher_resource_spec.rb
index 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 do
context 'when the work package does not exist' do
let(: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')
end
context 'when the user does not exist' do
@@ -243,10 +241,8 @@ describe 'API v3 Watcher resource', type: :request, content_type: :json do
context 'when work package doesn\'t exist' do
let(: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')
end
end
diff --git a/spec/requests/api/v3/work_packages/delete_resource_spec.rb b/spec/requests/api/v3/work_packages/delete_resource_spec.rb
index 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' do
let(: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')
end
end
diff --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.rb
index 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 d
let(:current_user) { unauthorized_user }
end
- it_behaves_like 'not found'
+ it_behaves_like 'not found',
+ I18n.t('api_v3.errors.not_found.work_package')
end
context 'user with all edit permissions' do
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index 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' do
let(: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')
end
end
@@ -191,7 +192,8 @@ describe 'API v3 Work package resource',
get get_path
end
- it_behaves_like 'not found'
+ it_behaves_like 'not found',
+ I18n.t('api_v3.errors.not_found.work_package')
end
context 'when acting as an anonymous user' do
@@ -200,7 +202,8 @@ describe 'API v3 Work package resource',
get get_path
end
- it_behaves_like 'not found'
+ it_behaves_like 'not found',
+ I18n.t('api_v3.errors.not_found.work_package')
end
end
end