Table of Contents

ResultError Struct

Definition

Namespace
Bodu.Functional
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
ResultError.cs

Represents the error carried by a failed Result or Result<T>: a human-readable message, an optional machine-readable code, and an optional originating exception.

public readonly struct ResultError : IEquatable<ResultError>
Implements
Inherited Members
Extension Methods

Remarks

Create errors with FromMessage(string, string?) or FromException(Exception, string?), or lift a plain message through the implicit conversion from string. default(ResultError) is valid and total: Message returns Empty while Code and Exception are null.

Equality compares Code and Message ordinally and Exception by reference; the hash code is computed from Code and Message only, so two errors that differ solely by exception instance hash identically but compare unequal.

Properties

Code

Gets the optional machine-readable error code.

public string? Code { get; }

Property Value

string

The code supplied when the error was created, or null when none was supplied.

Exception

Gets the exception the error was created from, when any.

public Exception? Exception { get; }

Property Value

Exception

The exception passed to FromException(Exception, string?), or null when the error did not originate from an exception.

Message

Gets the human-readable error message.

public string Message { get; }

Property Value

string

The message supplied when the error was created. Never null - Empty for default(ResultError).

Methods

Equals(ResultError)

Determines whether the specified error is equal to the current instance.

public bool Equals(ResultError other)

Parameters

other ResultError

The error to compare with this instance.

Returns

bool

true if Code and Message compare equal ordinally and Exception is the same instance; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current error.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare. Only another ResultError can be equal; all other types, including null, return false.

Returns

bool

true if obj is a ResultError equal to this instance; otherwise, false.

FromException(Exception, string?)

Creates an error from the specified exception and optional code.

public static ResultError FromException(Exception exception, string? code = null)

Parameters

exception Exception

The originating exception. Must not be null.

code string

The optional machine-readable code.

Returns

ResultError

A ResultError whose Message is the exception's Message and whose Exception is the exception itself.

Exceptions

ArgumentNullException

exception is null.

FromMessage(string, string?)

Creates an error from the specified message and optional code.

public static ResultError FromMessage(string message, string? code = null)

Parameters

message string

The human-readable error message. Must not be null.

code string

The optional machine-readable code.

Returns

ResultError

A ResultError carrying the message and code, with no exception.

Examples

var plain = ResultError.FromMessage("The file was not found.");
var coded = ResultError.FromMessage("The file was not found.", "IO.NotFound");

Exceptions

ArgumentNullException

message is null.

GetHashCode()

Returns a hash code for the current instance consistent with Equals(ResultError).

public override int GetHashCode()

Returns

int

A hash code combining Code and Message. Exception is deliberately excluded so errors differing only by exception instance still hash identically.

ToString()

Returns a string representation of the error.

public override string ToString()

Returns

string

Message when Code is null or empty; otherwise "Code: Message".

Operators

operator ==(ResultError, ResultError)

Determines whether two errors are equal.

public static bool operator ==(ResultError left, ResultError right)

Parameters

left ResultError

The first error to compare.

right ResultError

The second error to compare.

Returns

bool

true if the codes and messages compare equal ordinally and the exceptions are the same instance; otherwise, false.

implicit operator ResultError(string)

Converts a message string to an error with no code and no exception.

public static implicit operator ResultError(string message)

Parameters

message string

The human-readable error message. Must not be null.

Returns

ResultError

A ResultError carrying the message.

Remarks

The conversion delegates to FromMessage(string, string?) and therefore rejects a null string rather than producing a default error.

Exceptions

ArgumentNullException

message is null.

operator !=(ResultError, ResultError)

Determines whether two errors are not equal.

public static bool operator !=(ResultError left, ResultError right)

Parameters

left ResultError

The first error to compare.

right ResultError

The second error to compare.

Returns

bool

true if the errors differ; otherwise, false.

Applies to

ProductVersions
.NET8, 10