struct Err(T)

Overview

Result error .

Defined in:

result.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from struct Result(T)

err? : Bool err?, ok? : Bool ok?, state : Tuple(Symbol, Symbol, T) state, status : Symbol status, status?(s : Symbol) : Bool status?, type : Symbol type, unwrap unwrap, value : T value

Constructor methods inherited from struct Result(T)

new new

Class methods inherited from struct Result(T)

err? : Bool err?, ok? : Bool ok?

Constructor Detail

def self.[](exception) : Err #

Syntax sugar for Err.fail(exception). exception must be an Exception or a String.

Err["Oops"] # => same as Err.fail("Oops")

def self.conflict(exception) : Err #

Creates a new Err instance with the status :conflict. This method is a shortcut for Err.new :conflict, exception.

res = Err.conflict(exception)
res.status # => :conflict

# Raise
res.unwrap

# Or get the exception value
pp res.value

def self.fail(exception) : Err #

Creates a new Err instance with the status :fail. This method is a shortcut for Err.new :fail, exception.

res = Err.fail(exception)
res.status # => :fail

# Raise
res.unwrap

# Or get the exception value
pp res.value

def self.input(exception) : Err #

Creates a new Err instance with the status :input. This method is a shortcut for Err.new :input, exception.

res = Err.input(exception)
res.status # => :input

# Raise
res.unwrap

# Or get the exception value
pp res.value

def self.new(status : Symbol, exception : T) #

Creates a new Err instance with a custom #status.


def self.new(exception : T) #

def self.not_allowed(exception) : Err #

Creates a new Err instance with the status :not_allowed. This method is a shortcut for Err.new :not_allowed, exception.

res = Err.not_allowed(exception)
res.status # => :not_allowed

# Raise
res.unwrap

# Or get the exception value
pp res.value

def self.not_found(exception) : Err #

Creates a new Err instance with the status :not_found. This method is a shortcut for Err.new :not_found, exception.

res = Err.not_found(exception)
res.status # => :not_found

# Raise
res.unwrap

# Or get the exception value
pp res.value

def self.timeout(exception) : Err #

Creates a new Err instance with the status :timeout. This method is a shortcut for Err.new :timeout, exception.

res = Err.timeout(exception)
res.status # => :timeout

# Raise
res.unwrap

# Or get the exception value
pp res.value

Class Method Detail

def self.type : Symbol #

Result type as a Symbol.

Err.type # => :err

Instance Method Detail

def status : Symbol #
Description copied from struct Result(T)

Result status as a Symbol (:done or :fail, etc).


def type : Symbol #

Result type as a Symbol.

res.type # => :err

def unwrap #

Unwrap the result #value (like Result::unwrap in Rust). This method raises exception.


def value : T #

Returns the result value. Returns a Exception or a String