Error handling

Prev Next

Exceptions

In our Core library you will find a number of exceptions that are one-to-one to the Nexus Link error types, such as FulcrumAssertionFailedException that corresponds to the error type AssertionFailed. If you stick to these exceptions, you will find that our Web and Web.AspNet NuGet libraries support converting back and forth between these exceptions and the corresponding HTTP error responses.

Convert exceptions to HTTP responses

When a REST API encounters an exception, it needs to return the correct HTTP status code and a well formatted error response body .

The functionality to translate an exception into an HTTP response is provided in the NuGet libraries. Just add Nexus.Link.Libraries.Web.AspNet.Pipe.Inbound.ExceptionToFulcrumResponse in your ASP.NET Core middleware.

ASP.NET Core WebApp example:

using Nexus.Link.Libraries.Web.AspNet.Pipe.Inbound;

    public class Startup
    {
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
		{
            app.UseExceptionToFulcrumResponse();

The conversion takes details from the exceptions and creates properly formatted response body
The following exceptions will result in a 400 Bad Request status code:

  • FulcrumBusinessRuleException
  • FulcrumConflictException
  • FulcrumForbiddenAccessException
  • FulcrumNotFoundException
  • FulcrumServiceContractException
  • FulcrumUnauthorizedException

The following exceptions will result in a 500 Internal Error status code:

  • FulcrumAssertionFailedException
  • FulcrumContractException
  • FulcrumNotImplementedException
  • FulcrumTryAgainException

The following exceptions will result in a exception specific status code:

  • FulcrumResourceException: 502 Bad Gateway

Any other exception not listed above, will be reported as a FulcrumAssertionFailedException.

Convert HTTP error responses to exceptions

If you receive a response that complies to the Nexus Link REST guidelines then our code library can take care of converting that response into the appropriate exception.

You add this automatic conversion by adding Nexus.Link.Libraries.Web.Pipe.Outbound.ThrowFulcrumExceptionOnFail to your HttpClient.

Example:

var httpClient = HttpClientFactory.Create(new ThrowFulcrumExceptionOnFail());

The conversion takes details from the error response body to create and throws the correct exception. If the response body is not recognized as one of the known error types, then the error type is deducted from the HTTP status code according to the conversion table in the REST guidelines.

The exception thrown is chosen according to the following table:

FulcrumError FulcrumException Description
Xlent.Fulcrum.AssertionFailed FulcrumResourceException The resource had an internal error.
Xlent.Fulcrum.BusinessRule FulcrumBusinessRuleException A business rule error is most probably aimed at the original caller.
Xlent.Fulcrum.Conflict FulcrumConflictException The conflict most probably origins from the original caller.
Xlent.Fulcrum.Contract FulcrumResourceException The resource had an internal error.
Xlent.Fulcrum.ForbiddenAccess FulcrumForbiddenAccessException The provided credentials were insufficient.
Xlent.Fulcrum.NotFound FulcrumNotFoundException This is probably relevant to the original caller.
Xlent.Fulcrum.NotImplemented FulcrumResourceException The resource had an internal error.
Xlent.Fulcrum.Resource FulcrumResourceException The resource had an error in another resource.
Xlent.Fulcrum.ServiceContract FulcrumContractException The service said that the call did not follow the contract. We see this as an internal error for the code that called the RestClient.
Xlent.Fulcrum.TryAgain FulcrumTryAgainException The original caller should take the decision on if it should try again or not.
Xlent.Fulcrum.Unauthorized FulcrumUnauthorizedException The provided credentials could not be accepted.