Skip to content

Commit

Permalink
Merge branch 'implement_ruby_errors' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Krisztian Szabo committed Mar 19, 2015
2 parents fa60889 + 20a1c68 commit fb5051b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/mkto_rest.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
43 changes: 43 additions & 0 deletions lib/mkto_rest/errors.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 21 additions & 6 deletions spec/mkto_rest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
8 changes: 8 additions & 0 deletions spec/mkto_rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb5051b

Please sign in to comment.