Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions lib/adyen/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ class AdyenError < StandardError
attr_reader :code, :response, :request, :msg

def initialize(request = nil, response = nil, msg = nil, code = nil)
attributes = {
code: code,
request: request,
response: response,
msg: msg
}.select { |_k, v| v }.map { |k, v| "#{k}:#{v}" }.join(', ')
message = "#{self.class.name} #{attributes}"
super(message)
@code = code
@response = response
@request = request
Expand All @@ -12,65 +20,44 @@ def initialize(request = nil, response = nil, msg = nil, code = nil)

class AuthenticationError < AdyenError
def initialize(msg, request)
@code = 401
@response = nil
@request = request
@msg = msg
super(request, nil, msg, 401)
end
end

class PermissionError < AdyenError
def initialize(msg, request)
@code = 403
@response = nil
@request = request
@msg = msg
super(request, nil, msg, 403)
end
end

class FormatError < AdyenError
def initialize(msg, request, response)
@code = 422
@response = response
@request = request
@msg = msg
super(request, response, msg, 422)
end
end

class ServerError < AdyenError
def initialize(msg, request, response)
@code = 500
@response = response
@request = request
@msg = msg
super(request, response, msg, 500)
end
end

class ConfigurationError < AdyenError
def initialize(msg, request)
@code = 905
@response = nil
@request = request
@msg = msg
super(request, nil, msg, 905)
end
end

class ValidationError < AdyenError
def initialize(msg, request)
@code = nil
@response = nil
@request = request
@msg = msg
super(request, nil, msg, nil)
end
end

# catchall for errors which don't have more specific classes
class APIError < AdyenError
def initialize(msg, request, response, code)
@code = code
@response = response
@request = request
@msg = msg
super(request, response, msg, code)
end
end
end
17 changes: 17 additions & 0 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Adyen::AdyenError do
describe '#to_s' do
it 'describes using the error properties' do
expect(Adyen::AdyenError.new('request', 'response', 'message', 'code').to_s).to eq('Adyen::AdyenError code:code, request:request, response:response, msg:message')
end
it 'skips the null properties' do
expect(Adyen::AdyenError.new('request', nil, nil, 'code').to_s).to eq('Adyen::AdyenError code:code, request:request')
end
it 'uses the proper error class name' do
expect(Adyen::PermissionError.new('a message', 'a request').to_s).to eq('Adyen::PermissionError code:403, request:a request, msg:a message')
end
end
end