Sha256: 9ba5a4c492c6061f0e99a192546d11bf6c835db3446511acf90c2e433cd08ee7
Contents?: true
Size: 1.32 KB
Versions: 2
Compression:
Stored size: 1.32 KB
Contents
module Notifications class Client class RequestError < StandardError attr_reader :code, :body def initialize(response) @code = response.code @body = parse_body(response.body) end def parse_body(body) JSON.parse(body) rescue JSON::ParserError body end def message return body if body.is_a?(String) error_messages = body.fetch('errors') .map { |e| "#{e.fetch('error')}: #{e.fetch('message')}" } error_messages.join(", ") end end class ClientError < RequestError; end class BadRequestError < ClientError; end class AuthError < ClientError; end class NotFoundError < ClientError; end class RateLimitError < ClientError; end class ServerError < RequestError; end module ErrorHandling def build_error(response) error_class_for_code(response.code.to_i).new(response) end def error_class_for_code(code) case code when 400 BadRequestError when 403 AuthError when 404 NotFoundError when 429 RateLimitError when (400..499) ClientError when (500..599) ServerError else RequestError end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
notifications-ruby-client-5.1.0 | lib/notifications/client/request_error.rb |
notifications-ruby-client-4.0.0 | lib/notifications/client/request_error.rb |