-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathDbExceptionHandler.cs
More file actions
88 lines (77 loc) · 3.37 KB
/
DbExceptionHandler.cs
File metadata and controls
88 lines (77 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using EntityFramework.Exceptions.Common;
namespace CleanArchitecture.Blazor.Application.Common.ExceptionHandlers;
public class DbExceptionHandler<TRequest, TResponse> : MessageExceptionHandler<TRequest, TResponse>
where TRequest : IRequest<Result<int>>
where TResponse : Result<int>
{
private readonly ILogger _logger;
private readonly ILoggerFactory _loggerFactory;
public DbExceptionHandler(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
_logger = _loggerFactory.CreateLogger(nameof(DbExceptionHandler<TRequest, TResponse>));
}
protected override ValueTask<ExceptionHandlingResult<TResponse>> Handle(TRequest request, Exception exception,
CancellationToken cancellationToken)
{
if (exception is not DbUpdateException dbUpdateException)
{
return NotHandled;
}
return Handled((TResponse)Result<int>.Failure(GetErrors(dbUpdateException)));
}
private string[] GetErrors(DbUpdateException exception)
{
return exception switch
{
UniqueConstraintException e => GetUniqueConstraintExceptionErrors(e),
CannotInsertNullException e => GetCannotInsertNullExceptionErrors(e),
MaxLengthExceededException e => GetMaxLengthExceededExceptionErrors(e),
NumericOverflowException e => GetNumericOverflowExceptionErrors(e),
ReferenceConstraintException e => GetReferenceConstraintExceptionErrors(e),
_ => new[] { exception.GetBaseException().Message }
};
}
private string[] GetUniqueConstraintExceptionErrors(UniqueConstraintException exception)
{
var tableName = string.IsNullOrWhiteSpace(exception.SchemaQualifiedTableName) ? "unknown table" : exception.SchemaQualifiedTableName;
var properties = exception.ConstraintProperties != null && exception.ConstraintProperties.Any()
? string.Join(", ", exception.ConstraintProperties)
: "unknown properties";
return new[]
{
$"A unique constraint violation occurred on constraint in table '{tableName}'. " +
$"'{properties}'. Please ensure the values are unique."
};
}
private string[] GetCannotInsertNullExceptionErrors(CannotInsertNullException exception)
{
return new[]
{
"Some required information is missing. Please make sure all required fields are filled out."
};
}
private string[] GetMaxLengthExceededExceptionErrors(MaxLengthExceededException exception)
{
return new[]
{
"Some input is too long. Please shorten the data entered in the fields."
};
}
private string[] GetNumericOverflowExceptionErrors(NumericOverflowException exception)
{
return new[]
{
"A number you entered is too large or too small. Please enter a number within the allowed range."
};
}
private string[] GetReferenceConstraintExceptionErrors(ReferenceConstraintException exception)
{
var tableName = string.IsNullOrWhiteSpace(exception.SchemaQualifiedTableName) ? "unknown table" : exception.SchemaQualifiedTableName;
return new[]
{
$"The operation failed because this record is linked to other records in {tableName}. " +
$"Please remove any related records first"
};
}
}