Skip to content

How AuthP handles errors

Jon P Smith edited this page Aug 17, 2021 · 2 revisions

The AuthP library differentiates between two types of errors:

  • Errors that the user should know about.
  • Errors that the developer should know about.

Errors that the user should know about

For these AuthP doesn't use exceptions, but uses the library called GenericServices.StatusGeneric. Using the StatusGeneric has the following benefits:

  • Methods return a status: if the status 'IsValid' is true, then the method succeeded
    • In the success state the status will contain a success message, which is suitable to show to the user.
    • If there were errors it returns a list of ValidationResults, which can be added to ASP.NET Core's ModelState.
  • Unlike exceptions the code can return multiple errors, which is better for the users.

There is also a library called EfCore.GenericServices.AspNetCore which can copy the errors in a IStatusGeneric result into ASP.NET Core's ModelState, or for ASP.NET Core Web API applications the library can turn the status into a HTTP 400 (validation error) response.

You can also turn the IStatusGeneric result into an exception by calling the extension method called IfErrorsTurnToException, e.g.

var status = //some method returning IStatusGeneric
status.IfErrorsTurnToException();

NOTE: See example ASP.NET Core applications, especially Example4, in this AuthPermissions.AspNetCore repo for examples of how I convert the IStatusGeneric into a success message, or show the errors.

Errors that the developer should know about

In these cases the AuthP code throws an exception. For all expected exceptions AuthP has two exceptions:

AuthPermissionsBadDataException

Which is used for bad data

if (string.IsNullOrEmpty(userName))
    throw new AuthPermissionsBadDataException("Cannot be null or an empty string", nameof(userName));

AuthPermissionsException

This is used for non-parameter type errors, such as incorrect configuration of the AuthP library.

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally