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.
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.
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'
# }Cuprum::Errors::CommandNotImplemented, Cuprum::Errors::InvalidParameters, Cuprum::Errors::MultipleErrors, Cuprum::Errors::OperationNotCalled, Cuprum::Errors::UncaughtException
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.
#initialize(message: nil, type: nil, **properties) => Error
#message => String (readonly)
#type => String (readonly)
#==(other) => Boolean
#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.
Back to Documentation | Reference | Cuprum