Table of Contents

Class DbExceptionMappers

Namespace
Breeze.AspNetCore
Assembly
Breeze.AspNetCore.NetCore.dll

Ready-made mappings from a database exception to an HTTP status code, for StatusCodeForException.

public static class DbExceptionMappers
Inheritance
DbExceptionMappers
Inherited Members

Remarks

A duplicate key is a conflict with the current state of the resource, so 409 Conflict says more than a blanket 500 - and unlike a 500, a client will not retry it.

These read the provider's error number off the exception by type name and property, rather than referencing the provider. Breeze supports EF Core, NHibernate and whatever database is behind them, so none of the Breeze packages depends on Microsoft.Data.SqlClient, Npgsql or any other client library, and adding one for this would be the wrong trade. If you would rather match on the concrete type, write the mapping yourself - it is one expression, and StatusCodeForException takes any delegate:

StatusCodeForException = ex => ex.GetBaseException() is SqlException { Number: 2627 or 2601 or 547 }
  ? HttpStatusCode.Conflict : null

Fields

SqlServerDuplicateKeyInIndex

SQL Server: attempt to insert a duplicate key in a unique index.

public const int SqlServerDuplicateKeyInIndex = 2601

Field Value

int

SqlServerForeignKeyViolation

SQL Server: a foreign key constraint was violated.

public const int SqlServerForeignKeyViolation = 547

Field Value

int

SqlServerUniqueConstraint

SQL Server: violation of a unique constraint.

public const int SqlServerUniqueConstraint = 2627

Field Value

int

Methods

SqlServer(Exception)

Maps a SQL Server duplicate-key or foreign-key violation to 409 Conflict, and everything else to null, which leaves GlobalExceptionFilter to use its default.

o.Filters.Add(new GlobalExceptionFilter {
  StatusCodeForException = DbExceptionMappers.SqlServer
});
public static HttpStatusCode? SqlServer(Exception exception)

Parameters

exception Exception

The exception the filter caught.

Returns

HttpStatusCode?

409 Conflict, or null to accept the default.

SqlServerErrorNumber(Exception?)

The SQL Server error number carried by an exception, or null if it is not a SqlException.

public static int? SqlServerErrorNumber(Exception? exception)

Parameters

exception Exception

The exception to inspect.

Returns

int?

The error number, or null.

Remarks

Matched on the full type name so that an unrelated exception that happens to have a Number property is not mistaken for one. Both the modern Microsoft.Data.SqlClient and the older System.Data.SqlClient are recognized.