From 20a1c683605a1bcdb8f4aef186c4767e5b0bb658 Mon Sep 17 00:00:00 2001 From: Krisztian Szabo Date: Thu, 19 Mar 2015 18:52:31 +0100 Subject: [PATCH] Raise Ruby exception for every error code --- lib/mkto_rest.rb | 9 ++++++++- lib/mkto_rest/errors.rb | 43 ++++++++++++++++++++++++++++++++++++++++ spec/mkto_rest_helper.rb | 27 +++++++++++++++++++------ spec/mkto_rest_spec.rb | 8 ++++++++ 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 lib/mkto_rest/errors.rb diff --git a/lib/mkto_rest.rb b/lib/mkto_rest.rb index 82c5cde..e6a3c47 100644 --- a/lib/mkto_rest.rb +++ b/lib/mkto_rest.rb @@ -1,6 +1,7 @@ require_relative 'mkto_rest/version' require_relative 'mkto_rest/http_utils' require_relative 'mkto_rest/lead' +require_relative 'mkto_rest/errors' require 'json' module MktoRest @@ -116,8 +117,14 @@ def post(data, url = "https://#{@host}/rest/v1/leads.json") headers = { 'Authorization' => "Bearer #{@token}" } body = MktoRest::HttpUtils.post(url, headers, data.to_json, @options) data = JSON.parse(body, symbolize_names: true) - fail data[:errors].to_s if data[:success] == false + handle_errors(data[:errors]) if data[:success] == false data end + + def handle_errors(errors) + error = errors.first + return if error.nil? + raise MktoRest::Errors.find_by_response_code(error[:code].to_i), error[:message] + end end end diff --git a/lib/mkto_rest/errors.rb b/lib/mkto_rest/errors.rb new file mode 100644 index 0000000..59c171d --- /dev/null +++ b/lib/mkto_rest/errors.rb @@ -0,0 +1,43 @@ +class MktoRest::Errors + RESPONSE_CODE_TO_ERROR = { + 413 => 'RequestEntityTooLarge', + 600 => 'EmptyAccessToken', + 601 => 'AccessTokenInvalid', + 602 => 'AccessTokenExpired', + 603 => 'AccessDenied', + 604 => 'RequestTimedOut', + 605 => 'HTTPMethodNotSupported', + 606 => 'MaxRateLimit', + 607 => 'DailyQuotaReached', + 608 => 'APITemporarilyUnavailable', + 609 => 'InvalidJSON', + 610 => 'RequestedResourceNotFound', + 611 => 'System', + 612 => 'InvalidContentType', + 703 => 'DisabledFeature', + 1001 => 'TypeMismatch', + 1002 => 'MissingParamater', + 1003 => 'UnspecifiedAction', + 1004 => 'LeadNotFound', + 1005 => 'LeadAlreadyExists', + 1006 => 'FieldNotFound', + 1007 => 'MultipleLeadsMatching', + 1008 => 'PartitionAccessDenied', + 1009 => 'PartitionNameUnspecified', + 1010 => 'PartitionUpdateDenied', + 1011 => 'FieldNotSupported', + 1012 => 'InvalidCookieValue', + 1013 => 'ObjectNotFound', + 1014 => 'FailedToCreateObject' + } + + RESPONSE_CODE_TO_ERROR.values.each do |class_name| + const_set(class_name, Class.new(Exception)) + end + + Unknown = Class.new(Exception) + + def self.find_by_response_code(response_code) + const_get(RESPONSE_CODE_TO_ERROR.fetch(response_code, 'Unknown')) + end +end diff --git a/spec/mkto_rest_helper.rb b/spec/mkto_rest_helper.rb index ca853d5..422e838 100644 --- a/spec/mkto_rest_helper.rb +++ b/spec/mkto_rest_helper.rb @@ -99,6 +99,26 @@ def set_update_lead_stub_request(type, value, fields, hostname, token) end def stub_associate_lead_request(hostname, id, cookie, token) + stub_associate_lead_request_with_body(hostname, id, cookie, token, + requestId: 1, + success: true + ) +end + +def stub_failed_associate_lead_request(hostname, id, cookie, token) + stub_associate_lead_request_with_body(hostname, id, cookie, token, + requestId: 2, + success: false, + errors: [ + { + code: '601', + message: 'Unauthorized' + } + ] + ) +end + +def stub_associate_lead_request_with_body(hostname, id, cookie, token, body) headers = { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', @@ -107,12 +127,7 @@ def stub_associate_lead_request(hostname, id, cookie, token) 'User-Agent' => 'Ruby' } - resp_body = { - requestId: 1, - success: true - }.to_json - stub_request(:post, "https://#{hostname}/rest/v1/leads/#{id}/associate.json?cookie=#{cookie}&access_token=#{token}") .with(body: 'null', headers: headers) - .to_return(status: 200, body: resp_body) + .to_return(status: 200, body: body.to_json) end diff --git a/spec/mkto_rest_spec.rb b/spec/mkto_rest_spec.rb index b2e9cd4..dee1aff 100644 --- a/spec/mkto_rest_spec.rb +++ b/spec/mkto_rest_spec.rb @@ -142,6 +142,14 @@ it 'associates the lead' do expect(association).to include(success: true) end + + context 'with an unathorized request' do + before { stub_failed_associate_lead_request(@hostname, id, 'id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169', @token) } + + it 'raises a MktoRest::Errors::AccessTokenInvalid exception with the message' do + expect { association }.to raise_error(MktoRest::Errors::AccessTokenInvalid, 'Unauthorized') + end + end end end end