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

Allow passing in the model name to not found for easier matching

Oliver Günther02d503f95178
diff --git a/lib/api/errors/not_found.rb b/lib/api/errors/not_found.rb
index 43303bc11c7..c77e3e3a063 100644
--- a/lib/api/errors/not_found.rb
+++ b/lib/api/errors/not_found.rb
@@ -34,20 +34,22 @@ module API
identifier 'NotFound'
code 404
- def initialize(_message = nil, exception: nil)
- super(better_model_message(exception) || I18n.t('api_v3.errors.code_404'))
+ def initialize(_message = nil, exception: nil, model: nil)
+ # Try to find a localizable error message for
+ # the not found error by checking the "model" property set by rails.
+ model ||= exception&.model&.underscore
+ super not_found_message(model)
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
- model_name = exception.model.underscore
- I18n.t("api_v3.errors.not_found.#{model_name}", default: nil)
+ def not_found_message(model)
+ if model.present?
+ I18n.t("api_v3.errors.not_found.#{model}", default: I18n.t('api_v3.errors.code_404'))
+ else
+ I18n.t('api_v3.errors.code_404')
+ end
end
end
end
diff --git a/lib/api/v3/work_packages/work_packages_api.rb b/lib/api/v3/work_packages/work_packages_api.rb
index d0239e9a571..7bfd05c1ccf 100644
--- a/lib/api/v3/work_packages/work_packages_api.rb
+++ b/lib/api/v3/work_packages/work_packages_api.rb
@@ -77,7 +77,7 @@ module API
@work_package = WorkPackage.find(declared_params[:id])
authorize(:view_work_packages, context: @work_package.project) do
- raise API::Errors::NotFound.new
+ raise API::Errors::NotFound.new nil, model: :work_package
end
end
diff --git a/spec/requests/api/v3/relations_resource_spec.rb b/spec/requests/api/v3/relations_resource_spec.rb
index 5b01b7311c0..f81c5801bae 100644
--- a/spec/requests/api/v3/relations_resource_spec.rb
+++ b/spec/requests/api/v3/relations_resource_spec.rb
@@ -96,7 +96,8 @@ describe 'API v3 Relation resource', type: :request do
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
diff --git a/spec/requests/api/v3/watcher_resource_spec.rb b/spec/requests/api/v3/watcher_resource_spec.rb
index df52cf7aced..9db1e3ddad5 100644
--- a/spec/requests/api/v3/watcher_resource_spec.rb
+++ b/spec/requests/api/v3/watcher_resource_spec.rb
@@ -97,7 +97,8 @@ describe 'API v3 Watcher resource', type: :request, content_type: :json do
context 'user not allowed to see work package' do
let(:permissions) { [] }
- it_behaves_like 'not found'
+ 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 988ae597c6a..6decd64d051 100644
--- a/spec/requests/api/v3/work_packages/delete_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/delete_resource_spec.rb
@@ -84,7 +84,8 @@ describe 'API v3 Work package resource',
context 'without permission to see work packages' do
let(:permissions) { [] }
- it_behaves_like 'not found'
+ it_behaves_like 'not found',
+ I18n.t('api_v3.errors.not_found.work_package')
end
context 'without permission to delete work packages' do
diff --git a/spec/requests/api/v3/work_packages/update_resource_spec.rb b/spec/requests/api/v3/work_packages/update_resource_spec.rb
index cdbdaabef1c..c626275abc1 100644
--- a/spec/requests/api/v3/work_packages/update_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/update_resource_spec.rb
@@ -77,7 +77,8 @@ describe 'API v3 Work package resource',
include_context 'patch request'
- it_behaves_like 'not found'
+ it_behaves_like 'not found',
+ I18n.t('api_v3.errors.not_found.work_package')
end
context 'no permission to edit the work package' do