From ab17b68e0bae47724d67f6ce986cb62ee5420d66 Mon Sep 17 00:00:00 2001 From: Hakan Ensari Date: Fri, 13 Dec 2024 13:57:42 +0100 Subject: [PATCH] Pluralized Errors namespace --- CHANGELOG.md | 3 +-- lib/peddler/error.rb | 21 +++++++++++++------ test/peddler/error_test.rb | 14 ++++++------- .../{error => errors}/not_found_test.rb | 2 +- .../{error => errors}/quota_exceeded_test.rb | 2 +- .../NotFoundTest/test_not_found.yml | 0 .../QuotaExceededTest/test_quota_exceeded.yml | 0 7 files changed, 25 insertions(+), 17 deletions(-) rename test/peddler/{error => errors}/not_found_test.rb (96%) rename test/peddler/{error => errors}/quota_exceeded_test.rb (97%) rename test/vcr_cassettes/Peddler/{Error => Errors}/NotFoundTest/test_not_found.yml (100%) rename test/vcr_cassettes/Peddler/{Error => Errors}/QuotaExceededTest/test_quota_exceeded.yml (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5938b18..9563f08a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/peddler/error.rb b/lib/peddler/error.rb index 8500d8fc..1efb35b4 100644 --- a/lib/peddler/error.rb +++ b/lib/peddler/error.rb @@ -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 @@ -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 @@ -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 diff --git a/test/peddler/error_test.rb b/test/peddler/error_test.rb index c3d0d884..4e8f472f 100644 --- a/test/peddler/error_test.rb +++ b/test/peddler/error_test.rb @@ -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 diff --git a/test/peddler/error/not_found_test.rb b/test/peddler/errors/not_found_test.rb similarity index 96% rename from test/peddler/error/not_found_test.rb rename to test/peddler/errors/not_found_test.rb index 8c61db98..d93913c4 100644 --- a/test/peddler/error/not_found_test.rb +++ b/test/peddler/errors/not_found_test.rb @@ -5,7 +5,7 @@ require "peddler/apis/reports_2021_06_30" module Peddler - class Error + module Errors class NotFoundTest < Minitest::Test include FeatureHelpers diff --git a/test/peddler/error/quota_exceeded_test.rb b/test/peddler/errors/quota_exceeded_test.rb similarity index 97% rename from test/peddler/error/quota_exceeded_test.rb rename to test/peddler/errors/quota_exceeded_test.rb index 23ed9005..7c2654f9 100644 --- a/test/peddler/error/quota_exceeded_test.rb +++ b/test/peddler/errors/quota_exceeded_test.rb @@ -5,7 +5,7 @@ require "peddler/apis/product_pricing_v0" module Peddler - class Error + module Errors class QuotaExceededTest < Minitest::Test include FeatureHelpers diff --git a/test/vcr_cassettes/Peddler/Error/NotFoundTest/test_not_found.yml b/test/vcr_cassettes/Peddler/Errors/NotFoundTest/test_not_found.yml similarity index 100% rename from test/vcr_cassettes/Peddler/Error/NotFoundTest/test_not_found.yml rename to test/vcr_cassettes/Peddler/Errors/NotFoundTest/test_not_found.yml diff --git a/test/vcr_cassettes/Peddler/Error/QuotaExceededTest/test_quota_exceeded.yml b/test/vcr_cassettes/Peddler/Errors/QuotaExceededTest/test_quota_exceeded.yml similarity index 100% rename from test/vcr_cassettes/Peddler/Error/QuotaExceededTest/test_quota_exceeded.yml rename to test/vcr_cassettes/Peddler/Errors/QuotaExceededTest/test_quota_exceeded.yml