Sha256: 2bd5b5fe0566a75ce12091105d83168e6c0cd03d8bcde7af4b70a0a5ef5e3c1c

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Pragma
  module Operation
    # A generic error entity to hold error information for HTTP responses.
    #
    # This format is not mandatory, but recommended for consistency and convenience.
    class Error
      # @!attribute [r] error_type
      #   @return [Symbol|String] a machine-readable error type
      #
      # @!attribute [r] error_message
      #   @return [String] a human-readable error message
      #
      # @!attribute [r] meta
      #   @return [Hash] metadata about the error
      attr_reader :error_type, :error_message, :meta

      # Creates a new error entity.
      #
      # @param error_type [Symbol|String] a machine-readable error type
      # @param error_message [String] a human-readable error message
      # @param meta [Hash] metadata about the error
      def initialize(error_type:, error_message:, meta: {})
        @error_type = error_type
        @error_message = error_message
        @meta = meta
      end

      # Converts the entity to a hash ready to be dumped as JSON.
      #
      # @return [Hash]
      def as_json(*)
        {
          error_type: error_type,
          error_message: error_message,
          meta: meta
        }
      end

      # Dumps the JSON representation as a JSON string.
      #
      # @return [String]
      #
      # @see #as_json
      def to_json
        JSON.dump as_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pragma-operation-2.2.0 lib/pragma/operation/error.rb