Sha256: f6de81ba6a91a8cd4c56234dc448830c36f4093268e62931a2177a925755cf6d

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require "securerandom"
require "json"

module MinatoErrorHandler
  module Errors
    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 code
        self.class.name
      end

      def backtrace=(backtrace)
        set_backtrace(backtrace)
      end

      def unique_identfier
        @unique_identfier ||= SecureRandom.uuid
      end

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

      def as_json
        serialized_hash(serializable_methods_to_log).as_json
      end

      def json_response
        serialized_hash(serializable_methods_to_response).as_json
      end

      private

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

      def serializable_methods_to_response
        methods_to_exclude = %i[request]
        serializable_methods_to_log.reject { |m| methods_to_exclude.include? m }
      end

      def serialized_hash(methods)
        data = {}
        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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minato_error_handler-0.1.2.pre.5 lib/minato_error_handler/errors/minato_error.rb