Sha256: 33cfea50ec2d2b8745108b425d8c15c9b5d7ccb35e92cf6f0e899c1dfdd3adb6

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Housecanary
  class Error < StandardError # :nodoc:
    attr_reader :code

    NoContent = Class.new(self)
    BadRequest = Class.new(self)
    Unauthorized = Class.new(self)
    Forbidden = Class.new(self)
    NotFound = Class.new(self)
    InternalServerError = Class.new(self)
    TooManyRequests = Class.new(self)

    ERRORS_MAP = {
      204 => Housecanary::Error::NoContent,
      400 => Housecanary::Error::BadRequest,
      401 => Housecanary::Error::Unauthorized,
      403 => Housecanary::Error::Forbidden,
      404 => Housecanary::Error::NotFound,
      429 => Housecanary::Error::TooManyRequests,
      500 => Housecanary::Error::InternalServerError
    }.freeze

    class << self
      def from_response(body)
        message, status = parse_error(body)
        new(message, status)
      end

      def parse_error(body)
        message = body.fetch(:message, nil) || body.fetch(:api_code_description, nil)
        status = body.fetch(:status, nil) || body.fetch(:api_code, nil)
        [message, status]
      end
    end

    private

    def initialize(message = '', code = nil)
      super(message)
      @code = code
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
housecanary-ruby-0.2.2 lib/housecanary/error.rb
housecanary-ruby-0.2.1 lib/housecanary/error.rb
housecanary-ruby-0.2.0 lib/housecanary/error.rb