Skip to content

Commit

Permalink
Add error model
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Apr 3, 2017
1 parent d744ece commit e0598d1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/pragma/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'pragma/operation/version'
require 'pragma/operation/base'
require 'pragma/operation/error'

require 'pragma/operation/response'
require 'pragma/operation/response/not_found'
Expand All @@ -13,6 +14,7 @@
require 'pragma/operation/response/ok'
require 'pragma/operation/response/no_content'


module Pragma
# Operations provide business logic encapsulation for your JSON API.
#
Expand Down
11 changes: 11 additions & 0 deletions lib/pragma/operation/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Pragma
module Operation
class Error
def initialize(error_type:, error_message:, meta: {})
@error_type = error_type
@error_message = error_message
@meta = meta
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pragma/operation/response/forbidden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Operation
class Response
class Forbidden < Response
def initialize(
entity: {
entity: Error.new(
error_type: :forbidden,
error_message: 'You are not authorized to access the requested resource.'
},
),
headers: {}
)
super(status: 403, entity: entity, headers: headers)
Expand Down
4 changes: 2 additions & 2 deletions lib/pragma/operation/response/not_found.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Operation
class Response
class NotFound < Response
def initialize(
entity: {
entity: Error.new(
error_type: :not_found,
error_message: 'The requested resource could not be found.'
},
),
headers: {}
)
super(status: 404, entity: entity, headers: headers)
Expand Down
4 changes: 2 additions & 2 deletions lib/pragma/operation/response/unprocessable_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class UnprocessableEntity < Response
def initialize(entity: nil, headers: {}, errors: nil)
fail ArgumentError, 'You cannot provide both :entity and :errors!' if entity && errors

entity ||= {
entity ||= Error.new(
error_type: :unprocessable_entity,
error_message: 'The provided resource is in an unexpected format.',
meta: {
errors: errors || {}
}
}
)

super(status: 422, entity: entity, headers: headers)
end
Expand Down

0 comments on commit e0598d1

Please sign in to comment.