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
error handling in bcf v2.1
ulferts3a89fceb0d32
diff --git a/lib/api/root.rb b/lib/api/root.rbindex 053b557bcf7..7510ef236ed 100644--- a/lib/api/root.rb+++ b/lib/api/root.rb@@ -28,19 +28,17 @@# See docs/COPYRIGHT.rdoc for more details.#++-# Root class of the API-# This is the place for all API wide configuration, helper methods, exceptions-# rescuing, mounting of differnet API versions etc.-module APIclass Root < ::API::RootAPI- #content_type 'hal+json', 'application/hal+json; charset=utf-8'- #content_type :json, 'application/json; charset=utf-8'+ content_type 'hal+json', 'application/hal+json; charset=utf-8'format 'hal+json'formatter 'hal+json', API::Formatter.new+ default_format 'hal+json'parser :json, API::V3::Parser.new+ error_representer ::API::V3::Errors::ErrorRepresenter+version 'v3', using: :path domount API::V3::Rootenddiff --git a/lib/api/root_api.rb b/lib/api/root_api.rbindex 1cb7a70b8ac..9afc2201cee 100644--- a/lib/api/root_api.rb+++ b/lib/api/root_api.rb@@ -39,8 +39,7 @@ module APIinclude OpenProject::Authentication::Scopeextend API::Utilities::GrapeHelper- content_type 'hal+json', 'application/hal+json; charset=utf-8'- content_type :json, 'application/json; charset=utf-8'+ content_type :json, 'application/json; charset=utf-8'use OpenProject::Authentication::Manager@@ -194,6 +193,20 @@ module APIendend+ 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+ end++ @error_representer+ end+### Return JSON error response on authentication failure.OpenProject::Authentication.handle_failure(scope: API_V3) do |warden, _opts|diff --git a/lib/api/utilities/grape_helper.rb b/lib/api/utilities/grape_helper.rbindex db73104cf5a..c32e123985e 100644--- a/lib/api/utilities/grape_helper.rb+++ b/lib/api/utilities/grape_helper.rb@@ -67,9 +67,8 @@ module APIdef default_error_response(headers, log)lambda { |e|original_exception = $!- representer = ::API::V3::Errors::ErrorRepresenter.new e+ representer = error_representer.new eresp_headers = instance_exec &headers- env['api.format'] = 'hal+json'if log == trueOpenProject.logger.error original_exception, reference: :APIv3diff --git a/lib/api/v3/errors/error_representer.rb b/lib/api/v3/errors/error_representer.rbindex 6573659bce6..efb400e8289 100644--- a/lib/api/v3/errors/error_representer.rb+++ b/lib/api/v3/errors/error_representer.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is a project management system.# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)@@ -41,14 +42,14 @@ module APIproperty :_type, exec_context: :decoratorproperty :error_identifier, exec_context: :decorator, render_nil: true- property :message, getter: -> (*) { message }, render_nil: true+ property :message, getter: ->(*) { message }, render_nil: trueproperty :details, embedded: truecollection :errors,embedded: true,class: ::API::Errors::ErrorBase,decorator: ::API::V3::Errors::ErrorRepresenter,- if: -> (*) { !Array(errors).empty? }+ if: ->(*) { !Array(errors).empty? }def _type'Error'diff --git a/modules/bcf/app/controllers/bcf/api/root.rb b/modules/bcf/app/controllers/bcf/api/root.rbindex bac93ddc452..5b3ee97435a 100644--- a/modules/bcf/app/controllers/bcf/api/root.rb+++ b/modules/bcf/app/controllers/bcf/api/root.rb@@ -32,21 +32,15 @@# This is the place for all API wide configuration, helper methods, exceptions# rescuing, mounting of different API versions etc.-require 'open_project/authentication'-module Bcf::APIclass Root < ::API::RootAPIformat :jsonformatter :json, API::Formatter.new- # run authentication before each request- before do- # authenticate- # TODO: proper authentication- User.current = User.find_by login: 'admin'- set_localization- enforce_content_type- end+ default_format :json++ error_representer ::Bcf::API::V2_1::Errors::ErrorRepresenter+ error_formatter :json, ::Bcf::API::ErrorFormatter::Jsonversion '2.1', using: :path do# /projectsdiff --git a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rbindex aba2a834347..e38e3f1fcd8 100644--- a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb+++ b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb@@ -33,7 +33,7 @@ module Bcf::API::V2_1resources :projects doroute_param :id, regexp: /\A(\d+)\z/ doafter_validation do- @project = Project.find(params[:id])+ @project = Project.visible(current_user).find(params[:id])endget dodiff --git a/modules/bcf/app/formatters/bcf/api/error_formatter/json.rb b/modules/bcf/app/formatters/bcf/api/error_formatter/json.rbnew file mode 100644index 00000000000..47e93ef905d--- /dev/null+++ b/modules/bcf/app/formatters/bcf/api/error_formatter/json.rb@@ -0,0 +1,43 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Bcf::API+ module ErrorFormatter+ module Json+ extend Grape::ErrorFormatter::Base++ class << self+ def call(message, _backtrace, _options = {}, env = nil, _original_exception = nil)+ present(message, env)+ end+ end+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/errors/error_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/errors/error_representer.rbnew file mode 100644index 00000000000..2d7d6a8da14--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/errors/error_representer.rb@@ -0,0 +1,39 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Bcf::API::V2_1::Errors+ class ErrorRepresenter < Roar::Decorator+ include Representable::JSON++ property :message,+ getter: ->(*) { message },+ render_nil: true+ end+enddiff --git a/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rbindex 670eff66114..123b50b78d5 100644--- a/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb+++ b/modules/bcf/spec/requests/api/bcf/v2_1/projects/projects_api_spec.rb@@ -32,13 +32,23 @@ require 'rack/test'describe 'BCF 2.1 projects resource', type: :request doinclude Rack::Test::Methods+ let(:member_user) do+ FactoryBot.create(:user,+ member_in_project: project)+ end+ let(:non_member_user) do+ FactoryBot.create(:user)+ end+let(:project) { FactoryBot.create(:project) }subject(:response) { last_response }describe 'GET /api/bcf/2.1/projects/:project_id' dolet(:path) { "/api/bcf/2.1/projects/#{project.id}" }+ let(:current_user) { member_user }before do+ login_as(current_user)get pathend@@ -61,5 +71,28 @@ describe 'BCF 2.1 projects resource', type: :request doexpect(subject.headers['Content-Type']).to eql 'application/json; charset=utf-8'end++ context 'lacking permissions' do+ let(:current_user) { non_member_user }++ it 'responds 404 NOT FOUND' do+ expect(subject.status)+ .to eql 404+ end++ it 'states a NOT FOUND message' do+ expected = {+ message: 'The requested resource could not be found.'+ }++ expect(subject.body)+ .to be_json_eql(expected.to_json)+ end++ it 'is has a json content type header' do+ expect(subject.headers['Content-Type'])+ .to eql 'application/json; charset=utf-8'+ end+ endendend