Skip to content

Working with Exceptions

Youssef Sellami edited this page Jul 27, 2021 · 4 revisions

the library exposes several functions to help you work with exceptions in a simple manner.

1- Converting exceptions to result objects:

to convert an exception to Result Object you can use ToResult() function.

public Result DoSomeWork()
{
  try
  {
    // you code goes here
    return Result.Success();
  }
  catch(Exception ex)
  {
    return ex.ToResult();
  }
}

you can check the code of the function to see what it does to convert the exception to a result object ToResult()
if you want to add the exception as an error to an existing resut object you can use WithErrors()

public Result DoSomeWork()
{
  try
  {
    // you code goes here
    return Result.Success();
  }
  catch(Exception ex)
  {
    return Result.Failure()
      .WithError(ex);
  }
}

2- Converting Result objects to an exception:

to convert a result object to an exception you use this function ToException(), the return type of this function is a custom exception type which is ResultException this type inherits from Exception and add few props on top of it.

public class ResultException : Exception {

    /// <summary>
    /// the error code associated with this result exception
    /// </summary>
    public string Code { get; set; }

    /// <summary>
    /// this list of errors associated with the result exception.
    /// </summary>
    public ICollection<ResultError> Errors { get; }

    /// <summary>
    /// a unique log trace code used to trace the result in logs
    /// </summary>
    public string LogTraceCode { get; }
}

the full class code can be found here

var resut = SomeOperation();

// if the operation has failed we will convert the result to exception
if (result.IsFailure()) {
    ResultException ex = result.ToException();
    
    /**
    to convert and throw 
    result.ToException().throw();
    */
}

now let say that you want to convert the result object to a custom exception type base on the result object failure, utilizing the Code property we can do that very easily. we just need to register a mapping between the Code and our exception using ResultExceptionMapper here is an example:

// let assume that we need to throw an exception of type InvalidEmailHostException 
// when the operation failed because the email has an invalid host.

// define the exception -> code mapping
ResultExceptionMapper.AddMapping("invalid_email_host", result => {
     // you can use this action to init an instance of your exception
     return new InvalidEmailHostException(result);
});

// get the result of the operation
var result = someOperation(email);

// check if the result has failed because the email has an invalid host
if (result.FailedBecause("invalid_email_host")) {
    InvalidEmailHostException exception = (InvalidEmailHostException)result.ToException();
    // your code ...

    /**
    to cast the exception using AS<>();
    var exception = result.ToException()
         .As<InvalidEmailHostException>();

    to convert and throw 
    result.ToException().throw();
    */
}

as we have seen the function ToException() return the type ResultException so your custom exception must inherit from this type

public class InvalidEmailHostException : ResultException {
    // your code ...
}

Clone this wiki locally