Add SqlExceptionTranslator config and options class
Introduce SqlExceptionTranslator section in appsettings.json to map common SQL error codes to user-friendly messages. Add SqlExceptionTranslatorOptions class to manage error code/message mapping and support configuration binding.
This commit is contained in:
@@ -8,6 +8,14 @@
|
|||||||
"RecAction": {
|
"RecAction": {
|
||||||
"MaxConcurrentInvocations": 5
|
"MaxConcurrentInvocations": 5
|
||||||
},
|
},
|
||||||
|
"SqlExceptionTranslator": {
|
||||||
|
"ErrorMessages": {
|
||||||
|
"515": "{Operation} '{Target}' failed because a required field was not provided or was null. Verify mandatory values and retry.",
|
||||||
|
"547": "{Operation} '{Target}' failed because one or more referenced entities do not exist or violate relational rules. Please verify identifiers and constraints.",
|
||||||
|
"2601": "{Operation} '{Target}' failed because the data conflicts with a unique constraint. Remove duplicate values and retry.",
|
||||||
|
"2627": "{Operation} '{Target}' failed because the data conflicts with a unique constraint. Remove duplicate values and retry."
|
||||||
|
}
|
||||||
|
},
|
||||||
"AddedWho": "ReC.API",
|
"AddedWho": "ReC.API",
|
||||||
"FakeProfileId": 2
|
"FakeProfileId": 2
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
namespace ReC.Application.Common.Options;
|
||||||
|
|
||||||
|
public class SqlExceptionTranslatorOptions
|
||||||
|
{
|
||||||
|
public HashSet<int> ErrorNumbers { get; private set; } = [];
|
||||||
|
|
||||||
|
private Dictionary<int, string> _badRequestMessages = new()
|
||||||
|
{
|
||||||
|
[515] = "{Operation} '{Target}' failed because a required field was not provided or was null. Verify mandatory values and retry.",
|
||||||
|
[547] = "{Operation} '{Target}' failed because one or more referenced entities do not exist or violate relational rules. Please verify identifiers and constraints.",
|
||||||
|
[2601] = "{Operation} '{Target}' failed because the data conflicts with a unique constraint. Remove duplicate values and retry.",
|
||||||
|
[2627] = "{Operation} '{Target}' failed because the data conflicts with a unique constraint. Remove duplicate values and retry."
|
||||||
|
};
|
||||||
|
|
||||||
|
public Dictionary<int, string> ErrorMessages
|
||||||
|
{
|
||||||
|
get => _badRequestMessages;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_badRequestMessages = value;
|
||||||
|
ErrorNumbers = [.. value.Keys];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user