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

fix content type in error handling

ulferts0a9a7f02d1a9
diff --git a/lib/api/root.rb b/lib/api/root.rb
index d06d515575d..4544cfd758b 100644
--- a/lib/api/root.rb
+++ b/lib/api/root.rb
@@ -37,9 +37,18 @@ module API
parser :json, API::V3::Parser.new
- error_representer ::API::V3::Errors::ErrorRepresenter
+ error_representer ::API::V3::Errors::ErrorRepresenter, 'hal+json'
authentication_scope OpenProject::Authentication::Scope::API_V3
+ OpenProject::Authentication.handle_failure(scope: API_V3) do |warden, _opts|
+ e = grape_error_for warden.env, self
+ error_message = I18n.t('api_v3.errors.code_401_wrong_credentials')
+ api_error = ::API::Errors::Unauthenticated.new error_message
+ representer = ::API::V3::Errors::ErrorRepresenter.new api_error
+
+ e.error_response status: 401, message: representer.to_json, headers: warden.headers, log: false
+ end
+
version 'v3', using: :path do
mount API::V3::Root
end
diff --git a/lib/api/root_api.rb b/lib/api/root_api.rb
index 37586aaeb79..f6828f69ac1 100644
--- a/lib/api/root_api.rb
+++ b/lib/api/root_api.rb
@@ -193,43 +193,20 @@ module API
end
end
- def self.error_representer(klass = nil)
- if klass
- @error_representer = klass
-
- # Have the representer class available in the instances
- # via a helper.
- helpers do
- define_method(:error_representer, -> { klass })
- end
+ def self.error_representer(klass, content_type)
+ # Have the vars available in the instances via helpers.
+ helpers do
+ define_method(:error_representer, -> { klass })
+ define_method(:error_content_type, -> { content_type })
end
-
- @error_representer
end
- def self.authentication_scope(sym = nil)
- if sym
- @authentication_scope = sym
-
- # Have the representer class available in the instances
- # via a helper.
- helpers do
- define_method(:authentication_scope, -> { sym })
- end
+ def self.authentication_scope(sym)
+ # Have the scope available in the instances
+ # via a helper.
+ helpers do
+ define_method(:authentication_scope, -> { sym })
end
-
- @authentication_scope
- end
-
- ##
- # Return JSON error response on authentication failure.
- OpenProject::Authentication.handle_failure(scope: API_V3) do |warden, _opts|
- e = grape_error_for warden.env, self
- error_message = I18n.t('api_v3.errors.code_401_wrong_credentials')
- api_error = ::API::Errors::Unauthenticated.new error_message
- representer = error_representer.new api_error
-
- e.error_response status: 401, message: representer.to_json, headers: warden.headers, log: false
end
error_response ActiveRecord::RecordNotFound, ::API::Errors::NotFound, log: false
diff --git a/lib/api/utilities/grape_helper.rb b/lib/api/utilities/grape_helper.rb
index c32e123985e..068d18d0b4d 100644
--- a/lib/api/utilities/grape_helper.rb
+++ b/lib/api/utilities/grape_helper.rb
@@ -69,6 +69,7 @@ module API
original_exception = $!
representer = error_representer.new e
resp_headers = instance_exec &headers
+ env['api.format'] = error_content_type
if log == true
OpenProject.logger.error original_exception, reference: :APIv3
diff --git a/modules/bcf/app/controllers/bcf/api/root.rb b/modules/bcf/app/controllers/bcf/api/root.rb
index b7b661fb042..e7dc6b3a968 100644
--- a/modules/bcf/app/controllers/bcf/api/root.rb
+++ b/modules/bcf/app/controllers/bcf/api/root.rb
@@ -39,7 +39,7 @@ module Bcf::API
default_format :json
- error_representer ::Bcf::API::V2_1::Errors::ErrorRepresenter
+ error_representer ::Bcf::API::V2_1::Errors::ErrorRepresenter, :json
error_formatter :json, ::Bcf::API::ErrorFormatter::Json
authentication_scope OpenProject::Authentication::Scope::BCF_V2_1