Sha256: efb95d792ce5e1f5886b5adfe85248ab407c160074c7ddab7f55b6e61a010f44

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require "http"

module Mihari
  class Error < StandardError; end

  class ValueError < Error; end

  class ConfigurationError < Error; end

  class ResponseError < Error; end

  class AnalyzerError < Error
    # @return [StandardException, nil]
    attr_reader :cause

    #
    # @param [String] msg
    # @param [String] analyzer
    # @param [StandardException, nil] cause
    #
    def initialize(msg, analyzer, cause: nil)
      super("#{msg} (from #{analyzer})")

      @cause = cause
      set_backtrace(cause.backtrace) if cause
    end

    def detail
      return nil unless cause.respond_to?(:detail)

      cause.detail
    end
  end

  class IntegrityError < Error; end

  #
  # HTTP status code error
  #
  class StatusCodeError < ::HTTP::Error
    # @return [Integer]
    attr_reader :status_code

    # @return [String, nil]
    attr_reader :body

    #
    # @param [String] msg
    # @param [Integer] status_code
    # @param [String, nil] body
    #
    def initialize(msg, status_code, body)
      super(msg)

      @status_code = status_code
      @body = body
    end

    def detail
      { status_code: status_code, body: body }
    end
  end

  #
  # (dry-schema) Schema validation error
  #
  class ValidationError < Error
    attr_reader :errors

    #
    # @param [String] msg
    # @param [Dry::Schema::MessageSet] errors
    #
    def initialize(msg, errors)
      super(msg)

      @errors = errors
    end

    def detail
      errors.to_h
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mihari-7.0.5 lib/mihari/errors.rb