Cuprum

An opinionated implementation of the Command pattern for Ruby applications. Cuprum wraps your business logic in a consistent, object-oriented interface and features status and error management, composability and control flow management.

Class: Cuprum::Error

Parent Namespace
Cuprum
Defined In
lib/cuprum/error.rb

Table Of Contents

Overview

Wrapper class for encapsulating an error state for a failed Cuprum result.

Additional details can be passed by setting the #message or by using a subclass of Cuprum::Error.

Examples

error = Cuprum::Error.new(message: 'Something went wrong')
error.type
#=> 'cuprum.error'
error.message
#=> 'Something went wrong'

An Error With Custom Type

error = Cuprum::Error.new(
  message: 'Something went wrong',
  type:    'custom.errors.generic',
)
error.type
#=> 'custom.errors.generic'

An Error Subclass

class LightsError < Cuprum::Error
  TYPE = 'custom.errors.wrong_number_of_lights'

  def initialize(count)
    super(message: "There are #{count} lights!")

    @count = count
  end

  private def as_json_data
    { 'count' => count }
  end
end

error = LightsError.new(4)
error.type
#=> 'custom.errors.wrong_number_of_lights'
error.message
#=> 'There are 4 lights!'
error.as_json
#=> {
#     'data'    => { 'count' => 4 },
#     'message' => 'There are 4 lights!',
#     'type'    => 'custom.errors.wrong_number_of_lights'
#   }

Back To Top

Direct Subclasses

Cuprum::Errors::CommandNotImplemented, Cuprum::Errors::InvalidParameters, Cuprum::Errors::MultipleErrors, Cuprum::Errors::OperationNotCalled, Cuprum::Errors::UncaughtException

Back To Top

Constants

TYPE

= 'cuprum.error'

Short string used to identify the type of error.

Primarily used for serialization. This value can be overridden by passing in the :type parameter to the constructor.

Subclasses of Cuprum::Error should define their own default TYPE constant.

Back To Top

Constructor

#initialize(message: nil, type: nil, **properties) => Error

Parameters

Returns

Back To Top

Instance Attributes

#message => String (readonly)

Returns

#type => String (readonly)

Returns

Back To Top

Instance Methods

#==(other) => Boolean

Parameters

Returns

#as_json => Hash<String, Object>

Generates a serializable representation of the error object.

By default, contains the #type and #message properties and an empty :data Hash. This can be overridden in subclasses by overriding the private method #as_json_data; this should always return a Hash with String keys and whose values are basic objects or data structures of the same.

Returns

Back To Top


Back to Documentation | Reference | Cuprum