Skip to content

ResultError Type

Zeid Youssefzadeh edited this page Feb 17, 2025 · 2 revisions

Overview

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 and Exception
  • Deconstruction support
  • Serialization capabilities

Public API

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

^ Back To Top

Usage

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

// 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");

^ Back To Top

Extension Methods

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:

  1. Immutability: All methods return new instances
  2. Fluent Interface: Enables method chaining
  3. Null Safety: Validated parameters
  4. Performance: Aggressive inlining
  5. Consistency: Maintains original error data

Usage Examples

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));

^ Back To Top

Clone this wiki locally