diff --git a/lib/mailjet/exception/errors.rb b/lib/mailjet/exception/errors.rb index ef77fb1..fba6a6a 100644 --- a/lib/mailjet/exception/errors.rb +++ b/lib/mailjet/exception/errors.rb @@ -55,7 +55,11 @@ def initialize(message = nil, response = nil) end api_message = begin - Yajl::Parser.parse(response.response_body)['ErrorMessage'] + if response.response_body.present? + Yajl::Parser.parse(response.response_body)['ErrorMessage'] + else + 'Unauthorized' + end rescue Yajl::ParseError response.response_body rescue NoMethodError diff --git a/spec/resources/resource_spec.rb b/spec/resources/resource_spec.rb index b10e60e..5a7d779 100644 --- a/spec/resources/resource_spec.rb +++ b/spec/resources/resource_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Mailjet::Resource, :vcr do context '/invalid_credentials' do it 'should raise api connection error' do - expect{ Mailjet::Apikey.first }.to raise_error(Mailjet::Unauthorized, /the server responded with status 401 - Invalid Domain or API key/) + expect{ Mailjet::Apikey.first }.to raise_error(Mailjet::Unauthorized, /Invalid Domain or API key: Unauthorized/) end end diff --git a/spec/resources/send_spec.rb b/spec/resources/send_spec.rb index e8748a1..53a2926 100644 --- a/spec/resources/send_spec.rb +++ b/spec/resources/send_spec.rb @@ -68,7 +68,7 @@ }] } - expect{ described_class.create(message, version: 'v3.1') }.to raise_error(Mailjet::Unauthorized, /the server responded with status 401 - Invalid Domain or API key/) + expect{ described_class.create(message, version: 'v3.1') }.to raise_error(Mailjet::Unauthorized, /Visit API keys management section to check your keys/) end end end