From 7dddffd99195cab635ca088584abb43c595d27c9 Mon Sep 17 00:00:00 2001 From: Thomas Stachl Date: Wed, 4 Sep 2013 17:02:29 -0700 Subject: [PATCH] unprocessable entity has an errors reader now, fixed #12 --- lib/desk_api/error.rb | 13 +++-- lib/desk_api/error/unprocessable_entity.rb | 1 + .../allows_access_to_error_hash.yml | 54 +++++++++++++++++++ spec/desk_api/error_spec.rb | 11 ++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml diff --git a/lib/desk_api/error.rb b/lib/desk_api/error.rb index e66ccfc..55a17a0 100644 --- a/lib/desk_api/error.rb +++ b/lib/desk_api/error.rb @@ -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 @@ -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] @@ -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'] diff --git a/lib/desk_api/error/unprocessable_entity.rb b/lib/desk_api/error/unprocessable_entity.rb index 3b21cec..1bd5e66 100644 --- a/lib/desk_api/error/unprocessable_entity.rb +++ b/lib/desk_api/error/unprocessable_entity.rb @@ -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 \ No newline at end of file diff --git a/spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml b/spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml new file mode 100644 index 0000000..db6a76a --- /dev/null +++ b/spec/cassettes/DeskApi_Error/on_validation_error/allows_access_to_error_hash.yml @@ -0,0 +1,54 @@ +--- +http_interactions: +- request: + method: post + uri: https://devel.desk.com/api/v2/articles + body: + encoding: UTF-8 + string: '{"subject":"Testing","body":"Testing"}' + headers: + Accept: + - application/json + User-Agent: + - desk.com Ruby Gem v0.1.2 + Content-Type: + - application/json + response: + status: + code: 422 + message: + headers: + Accept-Ranges: + - bytes + Cache-Control: + - no-cache + Content-Type: + - application/json; charset=utf-8 + Date: + - Wed, 04 Sep 2013 23:57:05 GMT + Status: + - 422 Unprocessable Entity + Vary: + - X-AppVersion + X-AppVersion: + - '9.7' + X-Frame-Options: + - SAMEORIGIN + X-Rate-Limit-Limit: + - '60' + X-Rate-Limit-Remaining: + - '59' + X-Rate-Limit-Reset: + - '55' + X-Request-Id: + - a902613e4a179503f1a94b35421449ec + Content-Length: + - '71' + Connection: + - keep-alive + body: + encoding: UTF-8 + string: '{"message":"Validation Failed","errors":{"_links":{"topic":["blank"]}}}' + http_version: + recorded_at: Wed, 04 Sep 2013 23:57:05 GMT +recorded_with: VCR 2.5.0 diff --git a/spec/desk_api/error_spec.rb b/spec/desk_api/error_spec.rb index 8b58501..8d49662 100644 --- a/spec/desk_api/error_spec.rb +++ b/spec/desk_api/error_spec.rb @@ -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 \ No newline at end of file