From 7d2728bbd59ada8784a245d99cca0b04aa4f1619 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Mon, 20 Oct 2014 15:03:33 +0200 Subject: [PATCH] Use representer to render errors --- lib/api/errors/error_base.rb | 10 +--- lib/api/root.rb | 6 ++- lib/api/v3/activities/activities_api.rb | 2 +- lib/api/v3/errors/error_representer.rb | 52 +++++++++++++++++++ lib/api/v3/work_packages/work_packages_api.rb | 8 +-- 5 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 lib/api/v3/errors/error_representer.rb diff --git a/lib/api/errors/error_base.rb b/lib/api/errors/error_base.rb index d8181426c3..2dc531051a 100644 --- a/lib/api/errors/error_base.rb +++ b/lib/api/errors/error_base.rb @@ -30,21 +30,13 @@ module API module Errors class ErrorBase < Grape::Exceptions::Base - attr_reader :code + attr_reader :code, :identifier, :message def initialize(code, identifier, message) @code = code @identifier = identifier @message = message end - - def to_json - { - "_type" => "Error", - "errorIdentifier" => @identifier, - "message" => message - }.to_json - end end end end diff --git a/lib/api/root.rb b/lib/api/root.rb index 76dd451139..67bb76458a 100644 --- a/lib/api/root.rb +++ b/lib/api/root.rb @@ -76,11 +76,13 @@ module API rescue_from ActiveRecord::RecordNotFound do |e| api_error = ::API::Errors::NotFound.new(e.message) - error_response({ status: api_error.code, message: api_error.to_json }) + representer = ::API::V3::Errors::ErrorRepresenter.new(api_error) + error_response({ status: api_error.code, message: representer.to_json }) end rescue_from ::API::Errors::ErrorBase, rescue_subclasses: true do |e| - error_response({ status: e.code, message: e.to_json }) + representer = ::API::V3::Errors::ErrorRepresenter.new(e) + error_response({ status: e.code, message: representer.to_json }) end # run authentication before each request diff --git a/lib/api/v3/activities/activities_api.rb b/lib/api/v3/activities/activities_api.rb index 635f3e1161..e95c9665db 100644 --- a/lib/api/v3/activities/activities_api.rb +++ b/lib/api/v3/activities/activities_api.rb @@ -57,7 +57,7 @@ module API representer else - fail Errors::Validation.new(activity) + fail ::API::Errors::Validation.new(activity) end end diff --git a/lib/api/v3/errors/error_representer.rb b/lib/api/v3/errors/error_representer.rb new file mode 100644 index 0000000000..595959b31f --- /dev/null +++ b/lib/api/v3/errors/error_representer.rb @@ -0,0 +1,52 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2014 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-2013 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 doc/COPYRIGHT.rdoc for more details. +#++ + +require 'roar/decorator' +require 'roar/representer/json/hal' + +module API + module V3 + module Errors + class ErrorRepresenter < Roar::Decorator + include Roar::Representer::JSON::HAL + include Roar::Representer::Feature::Hypermedia + + self.as_strategy = API::Utilities::CamelCasingStrategy.new + + property :_type, exec_context: :decorator + property :errorIdentifier, getter: -> (*) { identifier }, render_nil: true + property :message, getter: -> (*) { message }, render_nil: true + + def _type + 'Error' + end + 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 50131592c3..88a8df95e6 100644 --- a/lib/api/v3/work_packages/work_packages_api.rb +++ b/lib/api/v3/work_packages/work_packages_api.rb @@ -48,7 +48,7 @@ module API attributes = JSON.parse(env['api.request.input']) invalid_attributes = invalid_work_package_update_attributes(attributes) - fail Errors::Validation.new(nil) unless invalid_attributes.empty? + fail ::API::Errors::Validation.new(nil) unless invalid_attributes.empty? end def invalid_work_package_update_attributes(attributes) @@ -64,7 +64,7 @@ module API if parent_id && !WorkPackage.visible(current_user).exists?(parent_id) @work_package.errors.add(:parent_id, :not_a_valid_parent) - fail Errors::Validation.new(@work_package) + fail ::API::Errors::Validation.new(@work_package) end end @@ -95,7 +95,7 @@ module API if @representer.represented.model.valid? && @representer.represented.save @representer else - fail Errors::Validation.new(@representer.represented.model) + fail ::API::Errors::Validation.new(@representer.represented.model) end end @@ -109,7 +109,7 @@ module API representer else - fail Errors::Validation.new(work_package) + fail ::API::Errors::Validation.new(work_package) end end end