Use representer to render errors

pull/2081/head
Hagen Schink 10 years ago
parent 2502054a0e
commit 7d2728bbd5
  1. 10
      lib/api/errors/error_base.rb
  2. 6
      lib/api/root.rb
  3. 2
      lib/api/v3/activities/activities_api.rb
  4. 52
      lib/api/v3/errors/error_representer.rb
  5. 8
      lib/api/v3/work_packages/work_packages_api.rb

@ -30,21 +30,13 @@
module API module API
module Errors module Errors
class ErrorBase < Grape::Exceptions::Base class ErrorBase < Grape::Exceptions::Base
attr_reader :code attr_reader :code, :identifier, :message
def initialize(code, identifier, message) def initialize(code, identifier, message)
@code = code @code = code
@identifier = identifier @identifier = identifier
@message = message @message = message
end end
def to_json
{
"_type" => "Error",
"errorIdentifier" => @identifier,
"message" => message
}.to_json
end
end end
end end
end end

@ -76,11 +76,13 @@ module API
rescue_from ActiveRecord::RecordNotFound do |e| rescue_from ActiveRecord::RecordNotFound do |e|
api_error = ::API::Errors::NotFound.new(e.message) 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 end
rescue_from ::API::Errors::ErrorBase, rescue_subclasses: true do |e| 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 end
# run authentication before each request # run authentication before each request

@ -57,7 +57,7 @@ module API
representer representer
else else
fail Errors::Validation.new(activity) fail ::API::Errors::Validation.new(activity)
end end
end end

@ -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

@ -48,7 +48,7 @@ module API
attributes = JSON.parse(env['api.request.input']) attributes = JSON.parse(env['api.request.input'])
invalid_attributes = invalid_work_package_update_attributes(attributes) 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 end
def invalid_work_package_update_attributes(attributes) def invalid_work_package_update_attributes(attributes)
@ -64,7 +64,7 @@ module API
if parent_id && !WorkPackage.visible(current_user).exists?(parent_id) if parent_id && !WorkPackage.visible(current_user).exists?(parent_id)
@work_package.errors.add(:parent_id, :not_a_valid_parent) @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
end end
@ -95,7 +95,7 @@ module API
if @representer.represented.model.valid? && @representer.represented.save if @representer.represented.model.valid? && @representer.represented.save
@representer @representer
else else
fail Errors::Validation.new(@representer.represented.model) fail ::API::Errors::Validation.new(@representer.represented.model)
end end
end end
@ -109,7 +109,7 @@ module API
representer representer
else else
fail Errors::Validation.new(work_package) fail ::API::Errors::Validation.new(work_package)
end end
end end
end end

Loading…
Cancel
Save