Sha256: e6838d8701546516cd8f6b76756072fa41a193d0eb05a719f4ffd3142ac199ac

Contents?: true

Size: 987 Bytes

Versions: 1

Compression:

Stored size: 987 Bytes

Contents

# frozen_string_literal: true

require "securerandom"
require "json"

module MinatoErrorHandler
  class MinatoError < StandardError
    def initialize(details: {}, caused_by: nil, status_code: 500)
      @caused_by = caused_by
      @details = details
      @status_code = status_code
      super
    end

    def self.from_error(error)
      return error if error.instance_of?(MinatoErrorHandler::MinatoError)

      new caused_by: error.cause
    end

    attr_reader :details, :status_code

    def message
      "An unknow error has occurred!"
    end

    def code
      self.class.name
    end

    def unique_identfier
      @unique_identfier || SecureRandom.uuid
    end

    def caused_by
      @caused_by || cause
    end

    def serialized_hash
      { message: message, code: code, unique_identfier: unique_identfier,
        details: details, caused_by: caused_by, status_code: status_code }
    end

    def to_json(*_args)
      serialized_hash.to_json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minato_error_handler-0.1.0 lib/minato_error_handler/minato_error.rb