Commit 6b50511ede002bf8bead925786826dfa0b1ab53d
1 parent
f0260209
Exists in
api_tasks
and in
4 other branches
API response with error block
Showing
1 changed file
with
21 additions
and
0 deletions
Show diff stats
lib/noosfero/api/entity.rb
1 | class Noosfero::API::Entity < Grape::Entity | 1 | class Noosfero::API::Entity < Grape::Entity |
2 | 2 | ||
3 | + def initialize(object, options = {}) | ||
4 | + object = nil if object.is_a? Exception | ||
5 | + super object, options | ||
6 | + end | ||
7 | + | ||
8 | + def self.represent(objects, options = {}) | ||
9 | + if options[:is_inner_data] | ||
10 | + super objects, options | ||
11 | + else | ||
12 | + data = super objects, options.merge(is_inner_data: true) | ||
13 | + if objects.is_a? Exception | ||
14 | + data.merge ok: false, error: { | ||
15 | + type: objects.class.name, | ||
16 | + message: objects.message | ||
17 | + } | ||
18 | + else | ||
19 | + data.merge ok: true, error: { type: 'Success', message: '' } | ||
20 | + end | ||
21 | + end | ||
22 | + end | ||
23 | + | ||
3 | def self.fields_condition(fields) | 24 | def self.fields_condition(fields) |
4 | lambda do |object, options| | 25 | lambda do |object, options| |
5 | return true if options[:fields].blank? | 26 | return true if options[:fields].blank? |