Sha256: 4f9f22c4088d3ab254e192db838667dabc47c049cec0c9f2dbf3d0ef11b5d2a8

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "securerandom"
require "json"

module MinatoErrorHandler
  class MinatoError < StandardError
    attr_accessor :details, :caused_by, :request

    def message
      raise_method_not_implemented_error __method__
    end

    def status_code
      raise_method_not_implemented_error __method__
    end

    def serializable_methods
      raise_method_not_implemented_error __method__
    end

    def code
      self.class.name
    end

    def unique_identfier
      @unique_identfier ||= SecureRandom.uuid
    end

    def to_json(*_args)
      serialized_hash.to_json
    end

    def as_json
      serialized_hash.as_json
    end

    protected

    def default_serializable_methods
      %i[caused_by code details message request status_code unique_identfier]
    end

    def serialized_hash
      data = {}
      serializable_methods.each do |method|
        data[method] = send(method)
      end
      data
    end

    def raise_method_not_implemented_error(method_name)
      raise "You must define \"#{method_name}\" in your concrete class."
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
minato_error_handler-0.1.2.pre.4 lib/minato_error_handler/minato_error.rb
minato_error_handler-0.1.2.pre.3 lib/minato_error_handler/minato_error.rb
minato_error_handler-0.1.2.pre.2 lib/minato_error_handler/minato_error.rb