Skip to main content

Mojo struct

Error

@register_passable struct Error

This type represents an Error.

Fields

  • data (UnsafePointer[SIMD[uint8, 1]]): A pointer to the beginning of the string data being referenced.
  • loaded_length (Int): The length of the string being referenced. Error instances conditionally own their error message. To reduce the size of the error instance we use the sign bit of the length field to store the ownership value. When loaded_length is negative it indicates ownership and a free is executed in the destructor.
  • stack_trace (StackTrace): The stack trace of the error. By default the stack trace is not collected for the Error, unless user sets the stack_trace_depth parameter to value >= 0.

Implemented traits

AnyType, Boolable, Copyable, Defaultable, ExplicitlyCopyable, Movable, Representable, Stringable, UnknownDestructibility, Writable

Methods

__init__

__init__() -> Self

Default constructor.

@implicit __init__(value: StringLiteral[value]) -> Self

Construct an Error object with a given string literal.

Args:

@implicit __init__(src: String) -> Self

Construct an Error object with a given string.

Args:

  • src (String): The error message.

@implicit __init__(src: StringSlice[origin]) -> Self

Construct an Error object with a given string ref.

Args:

__init__[*Ts: Writable](*args: *Ts, *, sep: StringSlice[StaticConstantOrigin] = "", end: StringSlice[StaticConstantOrigin] = "") -> Self

Construct an Error by concatenating a sequence of Writable arguments.

Parameters:

  • *Ts (Writable): The types of the arguments to format. Each type must be satisfy Writable.

Args:

  • *args (*Ts): A sequence of Writable arguments.
  • sep (StringSlice): The separator used between elements.
  • end (StringSlice): The String to write after printing the elements.

__copyinit__

__copyinit__(existing: Self) -> Self

Creates a deep copy of an existing error.

Args:

  • existing (Self): The error to copy from.

__del__

__del__(var self)

Releases memory if allocated.

__bool__

__bool__(self) -> Bool

Returns True if the error is set and false otherwise.

Returns:

Bool: True if the error object contains a value and False otherwise.

__str__

__str__(self) -> String

Converts the Error to string representation.

Returns:

String: A String of the error message.

write_to

write_to[W: Writer](self, mut writer: W)

Formats this error to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.

__repr__

__repr__(self) -> String

Converts the Error to printable representation.

Returns:

String: A printable representation of the error message.

byte_length

byte_length(self) -> Int

Get the length of the Error string in bytes.

Notes: This does not include the trailing null terminator in the count.

Returns:

Int: The length of the Error string in bytes.

unsafe_cstr_ptr

unsafe_cstr_ptr(self) -> UnsafePointer[SIMD[int8, 1]]

Retrieves a C-string-compatible pointer to the underlying memory.

The returned pointer is guaranteed to be NUL terminated, and not null.

Returns:

UnsafePointer: The pointer to the underlying memory.

as_string_slice

as_string_slice(self) -> StringSlice[ImmutableAnyOrigin]

Returns a string slice of the data maybe owned by the Error.

Notes: Since the data is not guaranteed to be owned by the Error, the resulting StringSlice is given an ImmutableAnyOrigin.

Returns:

StringSlice: A string slice pointing to the data maybe owned by the Error.

get_stack_trace

get_stack_trace(self) -> StackTrace

Returns the stack trace of the error.

Returns:

StackTrace: The stringable stack trace of the error.

Was this page helpful?