|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Smithy |
| 4 | + module Client |
| 5 | + # This module is mixed into another module, providing dynamic |
| 6 | + # error classes. Error classes all inherit from {ServiceError}. |
| 7 | + # |
| 8 | + # # Creates and returns the class |
| 9 | + # Weather::Errors::MyNewErrorClass |
| 10 | + # |
| 11 | + # Since the complete list of possible errors returned by services may |
| 12 | + # not be known, this allows us to create them as needed. This also |
| 13 | + # allows users to rescue errors by class without them being concrete |
| 14 | + # classes beforehand. |
| 15 | + # |
| 16 | + # @api private |
| 17 | + module DynamicErrors |
| 18 | + def self.extended(submodule) |
| 19 | + submodule.instance_variable_set('@const_set_mutex', Mutex.new) |
| 20 | + submodule.const_set(:ServiceError, Class.new(ServiceError)) |
| 21 | + end |
| 22 | + |
| 23 | + def const_missing(constant) |
| 24 | + set_error_constant(constant) |
| 25 | + end |
| 26 | + |
| 27 | + # Given the name of a service and an error code, this method |
| 28 | + # returns an error class that extends {ServiceError}. |
| 29 | + # |
| 30 | + # Weather::Errors.error_class('NoSuchCity').new |
| 31 | + # #=> #<Weather::Errors::NoSuchCity> |
| 32 | + # |
| 33 | + # @api private |
| 34 | + def error_class(error_code) |
| 35 | + constant = error_class_constant(error_code) |
| 36 | + if error_const_set?(constant) |
| 37 | + err_class = const_get(constant) |
| 38 | + err_class.code = constant.to_s |
| 39 | + err_class |
| 40 | + else |
| 41 | + set_error_constant(constant) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + # Convert an error code to an error class name/constant. |
| 48 | + # This requires filtering non-safe characters from the constant |
| 49 | + # name and ensuring it begins with an uppercase letter. |
| 50 | + # |
| 51 | + # @param [String] error_code |
| 52 | + # @return [Symbol] Returns a symbolized constant name for the given `error_code`. |
| 53 | + def error_class_constant(error_code) |
| 54 | + constant = error_code.to_s |
| 55 | + constant = constant.gsub(/https?:.*$/, '') |
| 56 | + constant = constant.gsub(/[^a-zA-Z0-9]/, '') |
| 57 | + constant = "Error#{constant}" unless constant.match(/^[a-z]/i) |
| 58 | + constant = constant[0].upcase + constant[1..] |
| 59 | + constant.to_sym |
| 60 | + end |
| 61 | + |
| 62 | + def set_error_constant(constant) # rubocop:disable Naming/AccessorMethodName |
| 63 | + @const_set_mutex.synchronize do |
| 64 | + # Ensure the const was not defined while blocked by the mutex |
| 65 | + if error_const_set?(constant) |
| 66 | + const_get(constant) |
| 67 | + else |
| 68 | + error_class = Class.new(const_get(:ServiceError)) |
| 69 | + error_class.code = constant.to_s |
| 70 | + const_set(constant, error_class) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def error_const_set?(constant) |
| 76 | + # Purposefully not using #const_defined? as that method returns true |
| 77 | + # for constants not defined directly in the current module. |
| 78 | + constants.include?(constant.to_sym) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments