I have the following table/model
[Table("Client")]
public class Client : Entity<int>, IHasCreationTime
{
[Required]
[StringLength(128)]
public string Name { get; set; }
[StringLength(512)]
public string Description { get; set; }
}
and the following method in controller:
public async Task<Response> CreateClient(Client input)
{
input.Name = null;
Response result = new Response();
try
{
await repo.InsertAsync(input);
}
catch (Exception ex)
{
if (ex.InnerException != null)
result.ErrorMessage = ex.InnerException.Message;
else
result.ErrorMessage = ex.Message;
}
return result;
}
based on the fact that I set input.Name = null
when await repo.InsertAsync(input);
is called it throws an exception (Name is required).
Problem is that the exception is not catched inside the catch clause but instead I get in client app (angular) a 500 error (exception is catched somewhere else)
This is a problem for me as I need to catch the exception and perform some other logic. Why does it fail?
Thanks
4 Answer(s)
-
0
Hi @antonis
You can try to make your app service method non transactional https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#non-transactional-unit-of-work or you can manually create a unitOfWork and use try-catch block inside of it.
-
0
Hi,
I have tried what you suggested but still no luck. While debugging I can also see that the client is not inserted until the cursor exits the app service
-
1
try { await repo.InsertAsync(input); await CurrentUnitOfWork.SaveChangesAsync(); // Add this }
-
0
Thanks aaron