Managing exceptions
In this recipe, you will learn how to manage exceptions in an original way.
Getting ready
We created an empty web application with VS 2017.
How to do it...
- First, we will create a
Result
class. This class will be returned by theService
layer. It allows us to manage the error messages to log and to display to the views:
public class Result { public bool IsSuccess { get; } public string SuccessMessageToLog { get; set; } public string ErrorToLog { get; } public string ErrorToDisplay { get; set; } public ErrorType? ErrorType { get; } public bool IsFailure => !IsSuccess; protected Result(bool isSuccess, string error, ErrorType? errorType) { if ((isSuccess && error != string.Empty) || (isSuccess && !errorType.HasValue)) throw new InvalidOperationException(); if ((!isSuccess && error == string.Empty) || (isSuccess && errorType.HasValue)) throw new InvalidOperationException(); IsSuccess = isSuccess; ...