Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Exception is not fired #5990


User avatar
0
antonis created

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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    antonis created

    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

  • User Avatar
    1
    aaron created
    Support Team
    try
    {
        await repo.InsertAsync(input);
        await CurrentUnitOfWork.SaveChangesAsync(); // Add this
    }
    
  • User Avatar
    0
    antonis created

    Thanks aaron