Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

flapek/Joint.Exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Joint.Exception

Branch Build status
master Build Status
develop Build Status

Exception

With the usage of Exception package, you can define Exception mapper and send it as a response.

Installation

dotnet add package Joint.Exception

Dependencies

Usage

Create your own exceptions separated by structure(DomainException, AppException, InfrastructureException) or if you don't have structure use DefaultException.

With structure

public class EmptyAccessTokenException : DomainException
{
  public override string Code { get; } = "empty_access_token";
  public override HttpStatusCode StatusCodes { get; } = HttpStatusCode.BadRequest;

  public EmptyAccessTokenException() : base("Empty access token.")
  {
  }
}
public class UserNotFoundException : AppException
{
  public override string Code { get; } = "user_not_found";
  public override HttpStatusCode StatusCodes { get; } = HttpStatusCode.NotFound;
  public Guid UserId { get; }

  public UserNotFoundException(Guid userId) : base($"User with ID: '{userId}' was not found.")
  {
      UserId = userId;
  }
}

Without structure

public class UserNotFoundException : DefaultException
{
  public override string Code { get; } = "user_not_found";
  public override HttpStatusCode StatusCodes { get; } = HttpStatusCode.NotFound;
  public Guid UserId { get; }

  public UserNotFoundException(Guid userId) : base($"User with ID: '{userId}' was not found.")
  {
      UserId = userId;
  }
}

Create dedicated ExceptionToResponseMapper class that implements IExceptionToResponseMapper interface with Map() method:

public ExceptionResponse Map(Exception exception)
  => exception switch
  {
    DomainException ex => new ExceptionResponse(new { code = ex.Code, reason = ex.Message }, ex.StatusCodes),
    AppException ex => new ExceptionResponse(new { code = ex.Code, reason = ex.Message }, ex.StatusCodes),
    InfrastructureException ex => new ExceptionResponse(new { code = ex.Code, reason = ex.Message }, ex.StatusCodes),
    _ => new ExceptionResponse(new { code = "error", reason = "There was an error." }, HttpStatusCode.BadRequest)
  };

Extend IJointBuilder with AddErrorHandler<T>() that will register the required services.

public static IJointBuilder RegisterJoint(this IJointBuilder builder)
{
    builder.AddErrorHandler<>()
    // Other services.

    return builder;
}

Then, invoke UseErrorHandler() extension from IApplicationBuilder.

public static IApplicationBuilder UseJoint(this IApplicationBuilder app)
{
    app.UseErrorHandler();
    // Other services.

    return app;
}

Table of Contents

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published