Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
unprocessable entity has an errors reader now, fixed #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Stachl committed Sep 5, 2013
1 parent 0fd06ce commit 7dddffd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/desk_api/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class Error < StandardError
# @param response_headers [Hash]
# @param code [Integer]
# @return [DeskApi::Error]
def initialize(exception=$!, response_headers={}, code = nil)
def initialize(exception=$!, response_headers={}, code = nil, errors = nil)
@rate_limit = DeskApi::RateLimit.new(response_headers)
@wrapped_exception = exception
@code = code
@errors = errors
exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
end

Expand All @@ -28,8 +29,8 @@ class << self
# @param response [Hash]
# @return [DeskApi::Error]
def from_response(response = {})
error, code = parse_error(response[:body]), response[:status]
new(error, response[:response_headers], code)
error, errors, code = parse_error(response[:body]), parse_errors(response[:body]), response[:status]
new(error, response[:response_headers], code, errors)
end

# @return [Hash]
Expand All @@ -51,6 +52,12 @@ def inherited(descendant)

private

def parse_errors(body)
if body['errors']
body['errors']
end
end

def parse_error(body)
if body['message']
body['message']
Expand Down
1 change: 1 addition & 0 deletions lib/desk_api/error/unprocessable_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Error
# Raised when desk.com returns the HTTP status code 422
class UnprocessableEntity < DeskApi::Error::ClientError
HTTP_STATUS_CODE = 422
attr_reader :errors
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions spec/desk_api/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
end
end
end

context 'on validation error' do
it 'allows access to error hash', :vcr do
begin
subject.articles.create({ subject: 'Testing', body: 'Testing' })
rescue DeskApi::Error::UnprocessableEntity => e
e.errors.should be_an_instance_of(Hash)
e.errors.should eq({"_links" => { "topic" => ["blank"]}})
end
end
end
end

0 comments on commit 7dddffd

Please sign in to comment.