-
Notifications
You must be signed in to change notification settings - Fork 0
ResultError Type
A lightweight, immutable error type for functional error handling in C# applications. ResultError
is a structured error representation designed for railway-oriented programming (ROP) patterns. It provides:
- Immutable error details (code, name, message, exception)
- Factory methods with validation
- Implicit conversions from
string
andException
- Deconstruction support
- Serialization capabilities
Here's a concise table summarizing the ResultError
public API properties:
Property | Type | Description |
---|---|---|
Code |
int |
Numeric error code (e.g., 400 for validation errors) |
Name |
string |
Human-readable error identifier (e.g., "ValidationError") |
Message |
string |
Descriptive error message for developers/users |
Exception |
Exception? |
Optional source exception that caused the error (null if not applicable) |
Key Characteristics:
- All properties are read-only (
get
only) - Immutable structure - values can't change after creation
- Null handling:
-
Code
/Name
/Message
are guaranteed non-null -
Exception
is nullable (optional)
-
- Default values:
-
Name
defaults to "UnspecifiedError" -
Code
defaults to 1 (ResultErrorCode.Generic
)
-
Example: Accessing public properties
var error = ResultError.New("InvalidRequest", "Missing required fields");
Console.WriteLine(error.Code); // 1 (default code)
Console.WriteLine(error.Name); // "InvalidRequest"
Console.WriteLine(error.Message); // "Missing required fields"
Console.WriteLine(error.Exception); // null
The ResultError
does not have any public constructor, therefore to create an instance of ResultError
you need to use ResultError.New
factory method or use implicit conversion from string
or Exception
:
// Factory methods
ResultError.New(string message)
ResultError.New(Exception exception)
ResultError.New(int code, string message)
ResultError.New(string name, string message)
ResultError.New(string name, string message, Exception exception)
ResultError.New(int code, string message, Exception exception)
// Deconstruction
var (code, name, msg, ex) = error;
implicit conversion from string
or Exception
// Implicit conversion from string
ResultError error = "Error message";
// Implicit conversion from Exception
ResultError error = new Exception("Error message");
Method | Parameters | Returns | Description |
---|---|---|---|
WithCode |
int code |
ResultError |
Creates new error with specified numeric code |
WithMessage |
string message |
ResultError |
Creates new error with updated message |
WithName |
string name |
ResultError |
Creates new error with custom identifier |
WithException |
Exception exception |
ResultError |
Creates new error with attached exception |
TryGetException |
- | Maybe<Exception> |
Safely retrieves associated exception |
Key Characteristics:
- Immutability: All methods return new instances
- Fluent Interface: Enables method chaining
- Null Safety: Validated parameters
- Performance: Aggressive inlining
- Consistency: Maintains original error data
Example: Basic Error Customization and exception handling
var error = ResultError.New("Initial error")
.WithCode(400)
.WithName("ValidationError")
.WithMessage("Invalid email format")
.WithException(new FormatException("Email should be like test@example.com"));
//Output: [400] ValidationError: Invalid email format
Console.WriteLine(error.ToString());
// Examples: Handling Exceptions
error.
TryGetException(). // returns an inctance of Maybe<Exception>
//Output: Email should be like test@example.com
.TapIfSome( error => Console.WriteLine(error.Message));
Inspired by LanguageExt, this library offers a more compact and user-friendly alternative with extensive examples and tutorials.
There is a very detailed YouTube channel with a dedicated video tutorial playlist for this library.
Star this repository and follow me on GitHub to stay informed about new releases and updates. Your support fuels this project's growth!
If my content adds value to your projects, consider supporting me via crypto.
- Bitcoin: bc1qlfljm9mysdtu064z5cf4yq4ddxgdfztgvghw3w
- USDT(TRC20): TJFME9tAnwdnhmqGHDDG5yCs617kyQDV39
Thank you for being part of this community—let’s build smarter, together