I write WebAPI projects with Microsoft.EntityFrameworkCore.
Data goes through an Interface, a Repository, and a Controller.
I have got the habit to put a try ... catch in the repository, if I remember correctly I saw that this way in the documentation.
But suddenly I am wondering ...
Probably that is not a good idea, for that way I have the same result for an error or a non existing record.
Maybe it would be better that the repository throws an exception to the controller, which then has the possibility to distinguish between NotFound and BadRequest.
Probably that is not a good idea, for that way I have the same result for an error or a non existing record.
Yes you should provide specific error messages to the client in your API. For non-existing records you can simply return Status400BadRequest like this:
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
But of course, if I put a try...catch in the Repository, it will return 0, and the controller returns Ok(0), no ?
Thats why you need to apply the if else logic and return different HTTP values. Check my link already given in the above post.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
12 Points
138 Posts
Exceptions in Core async project
Aug 03, 2019 08:28 AM|Gluups|LINK
Hello everybody,
I write WebAPI projects with Microsoft.EntityFrameworkCore.
Data goes through an Interface, a Repository, and a Controller.
I have got the habit to put a try ... catch in the repository, if I remember correctly I saw that this way in the documentation.
But suddenly I am wondering ...
Probably that is not a good idea, for that way I have the same result for an error or a non existing record.
Maybe it would be better that the repository throws an exception to the controller, which then has the possibility to distinguish between NotFound and BadRequest.
Am I pushing an open door ?
Participant
1780 Points
580 Posts
Re: Exceptions in Core async project
Aug 05, 2019 05:15 AM|Yang Shen|LINK
Hi Gluups,
This is strange cause NotFound and BadRequest should show the different exceptions messages.
Can you share us your code in which NotFound and BadRequest give you the same result?
Best Regard,
Yang Shen
Participant
1060 Points
732 Posts
Re: Exceptions in Core async project
Aug 05, 2019 05:02 PM|yogyogi|LINK
Yes you should provide specific error messages to the client in your API. For non-existing records you can simply return Status400BadRequest like this:
Refer this tutorial for all the details:
Returning BadRequest (400), Unauthorized (401), NotFound (404) status codes from Action
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
12 Points
138 Posts
Re: Exceptions in Core async project
Aug 05, 2019 06:19 PM|Gluups|LINK
Yes, that can still refine the result.
But of course, if I put a try...catch in the Repository, it will return 0, and the controller returns Ok(0), no ?
Participant
1060 Points
732 Posts
Re: Exceptions in Core async project
Aug 06, 2019 05:13 AM|yogyogi|LINK
Thats why you need to apply the if else logic and return different HTTP values. Check my link already given in the above post.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
12 Points
138 Posts
Re: Exceptions in Core async project
Aug 06, 2019 05:28 AM|Gluups|LINK
Different integer (or string) values from the repository, and different status values from the controller.