Class: Pragma::Operation::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/pragma/operation/error.rb

Overview

A generic error entity to hold error information for HTTP responses.

This format is not mandatory, but recommended for consistency and convenience.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_type:, error_message:, meta: {}) ⇒ Error

Creates a new error entity.

Parameters:

  • error_type (Symbol|String)

    a machine-readable error type

  • error_message (String)

    a human-readable error message

  • meta (Hash) (defaults to: {})

    metadata about the error



24
25
26
27
28
# File 'lib/pragma/operation/error.rb', line 24

def initialize(error_type:, error_message:, meta: {})
  @error_type = error_type
  @error_message = error_message
  @meta = meta
end

Instance Attribute Details

#error_messageString (readonly)

Returns a human-readable error message.

Returns:

  • (String)

    a human-readable error message



17
# File 'lib/pragma/operation/error.rb', line 17

attr_reader :error_type, :error_message, :meta

#error_typeSymbol|String (readonly)

Returns a machine-readable error type.

Returns:

  • (Symbol|String)

    a machine-readable error type



17
18
19
# File 'lib/pragma/operation/error.rb', line 17

def error_type
  @error_type
end

#metaObject (readonly)

Returns the value of attribute meta.



17
# File 'lib/pragma/operation/error.rb', line 17

attr_reader :error_type, :error_message, :meta

Instance Method Details

#as_jsonHash

Converts the entity to a hash ready to be dumped as JSON.

Returns:

  • (Hash)


33
34
35
36
37
38
39
# File 'lib/pragma/operation/error.rb', line 33

def as_json(*)
  {
    error_type: error_type,
    error_message: error_message,
    meta: meta
  }
end

#to_jsonString

Dumps the JSON representation as a JSON string.

Returns:

  • (String)

See Also:



46
47
48
# File 'lib/pragma/operation/error.rb', line 46

def to_json
  JSON.dump as_json
end