I have a CreateClassification
API which is used to insert data into database table. There is unique constraint in table, So If I try to insert same record it gives the below response.
{
"result": null,
"targetUrl": null,
"success": false,
"error": {
"code": 0,
"message": "An internal error occurred during your request!",
"details": null,
"validationErrors": null
},
"unAuthorizedRequest": false,
"__abp": true
}
API:
await _classificationrepository.InsertAsync(classobj);
But failure reason is not clear by this response message, because there could by many reason for a failed insert, So is there any way to get the proper reason of failure.
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'IX_ClassificationCode'. Cannot insert duplicate key in object 'dbo.Classification'. The duplicate key value is (02).
The statement has been terminated.
As suggested, I have tried this, but it has no impact on api response:
Task Createxyz(XyzInput input);
public async Task Createxyz(XyzInput input)
{
try
{
await _xyzrepository.InsertAsync(classobj);
}
catch (Exception)
{
throw new UserFriendlyException("hello");
}
}
Hitting the below url:
http://localhost:22742/api/services/app/xyz/Createxyz
I have one more doubt, how my Createxyz
gets converted into api?, means how abp provides routing to Createxyz
method, so that end user can call this api
ABP hides exception details from users. For instance, this is not a good error message to show to a users (because of user experience and security of your application):
Violation of UNIQUE KEY constraint 'IX_ClassificationCode'. Cannot insert duplicate key in object 'dbo.Classification'. The duplicate key value is (02).
So, you should handle specific types of exceptions yourself (I don't know the exception type in your case) and throw a UserFriendlyException
yourself.
UserFriendlyException
is a specific type of exception so ABP directly show exception message to the end user.
EDIT
Example:
try
{
await _repository.InsertAsync(...);
await CurrentUnitOfWork.SaveChangesAsync();
}
catch(SqlDuplicateKeyException ex)
{
throw new UserFriendlyException("Duplicate name..!");
}
I don't know if there is such a SqlDuplicateKeyException. To understand it's type, catch general Exception and check it's type or message. This is basic C# practice.