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
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.rbindex 43303bc11c7..c77e3e3a063 100644--- a/lib/api/errors/not_found.rb+++ b/lib/api/errors/not_found.rb@@ -34,20 +34,22 @@ module APIidentifier '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)endprivate- ##- # 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')+ endendendenddiff --git a/lib/api/v3/work_packages/work_packages_api.rb b/lib/api/v3/work_packages/work_packages_api.rbindex 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_packageendenddiff --git a/spec/requests/api/v3/relations_resource_spec.rb b/spec/requests/api/v3/relations_resource_spec.rbindex 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 doget pathend- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endendenddiff --git a/spec/requests/api/v3/watcher_resource_spec.rb b/spec/requests/api/v3/watcher_resource_spec.rbindex 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 docontext 'user not allowed to see work package' dolet(:permissions) { [] }- it_behaves_like 'not found'+ 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 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' dolet(:permissions) { [] }- it_behaves_like 'not found'+ it_behaves_like 'not found',+ I18n.t('api_v3.errors.not_found.work_package')endcontext 'without permission to delete work packages' dodiff --git a/spec/requests/api/v3/work_packages/update_resource_spec.rb b/spec/requests/api/v3/work_packages/update_resource_spec.rbindex 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')endcontext 'no permission to edit the work package' do