Skip to content

Commit

Permalink
Pluralized Errors namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Dec 13, 2024
1 parent 689dff9 commit ab17b68
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Pluralized namespace to APIs for consistency. Direct references to APIs will need to be updated from
`Peddler::API::SomeAPI` to `Peddler::APIs::SomeAPI`. The shorthand method `Peddler.some_api` remains unchanged.
- Pluralized the namespaces `APIs` and `Errors` for consistency. Direct references will need to be updated.

### Added

Expand Down
21 changes: 15 additions & 6 deletions lib/peddler/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

module Peddler
class Error < StandardError
class InvalidInput < Error; end
class NotFound < Error; end
class QuotaExceeded < Error; end
class Unauthorized < Error; end

attr_reader :response

# @!visibility private
Expand All @@ -15,7 +10,14 @@ def build(response)
error = JSON.parse(response).dig("errors").first
class_name = error.dig("code")
message = error.dig("message")
klass = const_defined?(class_name) ? const_get(class_name) : const_set(class_name, Class.new(Error))
klass = if Errors.const_defined?(class_name)
Errors.const_get(class_name)
else
Errors.const_set(
class_name,
Class.new(Error),
)
end

klass.new(message, response)
rescue NameError
Expand All @@ -28,4 +30,11 @@ def initialize(msg = nil, response = nil)
super(msg)
end
end

module Errors
class InvalidInput < Error; end
class NotFound < Error; end
class QuotaExceeded < Error; end
class Unauthorized < Error; end
end
end
14 changes: 7 additions & 7 deletions test/peddler/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ def test_invalid_input
response = '{"errors":[{"code":"InvalidInput","message":"InvalidInput"}]}'
error = Error.build(response)

assert_kind_of(Error::InvalidInput, error)
assert_kind_of(Errors::InvalidInput, error)
end

def test_not_found
response = '{"errors":[{"code":"NotFound","message":"NotFound"}]}'
error = Error.build(response)

assert_kind_of(Error::NotFound, error)
assert_kind_of(Errors::NotFound, error)
end

def test_quota_exceeded
response = '{"errors":[{"code":"QuotaExceeded","message":"You exceeded your quota for the requested resource."}]}'
error = Error.build(response)

assert_kind_of(Error::QuotaExceeded, error)
assert_kind_of(Errors::QuotaExceeded, error)
end

def test_unauthorized
response = '{"errors":[{"code":"Unauthorized","message":"Access to requested resource is denied."}]}'
error = Error.build(response)

assert_kind_of(Error::Unauthorized, error)
assert_kind_of(Errors::Unauthorized, error)
end

def test_other_error
refute_includes(Error.constants, :OtherError)
refute_includes(Errors.constants, :OtherError)

response = '{"errors":[{"code":"OtherError","message":"OtherError"}]}'
error = Error.build(response)

assert_includes(Error.constants, :OtherError)
assert_kind_of(Error::OtherError, error)
assert_includes(Errors.constants, :OtherError)
assert_kind_of(Errors::OtherError, error)
end

def test_invalid_class_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "peddler/apis/reports_2021_06_30"

module Peddler
class Error
module Errors
class NotFoundTest < Minitest::Test
include FeatureHelpers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "peddler/apis/product_pricing_v0"

module Peddler
class Error
module Errors
class QuotaExceededTest < Minitest::Test
include FeatureHelpers

Expand Down

0 comments on commit ab17b68

Please sign in to comment.