Skip to main content
Log in

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.

Implemented traits

AnyType, Boolable, CollectionElement, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable, Representable, Stringable, UnknownDestructibility, Writable, _CurlyEntryFormattable

Methods

__init__

__init__(out self)

Default constructor.

@implicit __init__(out self, value: StringLiteral)

Construct an Error object with a given string literal.

Args:

  • value (StringLiteral): The error message.

@implicit __init__(out self, src: String)

Construct an Error object with a given string.

Args:

  • src (String): The error message.

@implicit __init__(out self, src: StringRef)

Construct an Error object with a given string ref.

Args:

  • src (StringRef): The error message.

__init__(out self, *, other: Self)

Copy the object.

Args:

  • other (Self): The value to copy.

__copyinit__

__copyinit__(out self, existing: Self)

Creates a deep copy of an existing error.

Args:

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

__del__

__del__(owned self)

Releases memory if allocated.

__bool__

__bool__(self) -> Bool

Returns True if the error is set and false otherwise.

Returns:

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

__str__

__str__(self) -> String

Converts the Error to string representation.

Returns:

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:

A printable representation of the error message.

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:

The pointer to the underlying memory.