Hi all,
Am having Employee class inthat there are two properties
1. Emp name
2. Empid
Am going to insert the details in db and returning employee class as response
C#
Public async Task save data(employee request)
{
Try
{
Return await insertasync(request);
}
Catch(exception ex)
{
Return new employee();
}Above codes return always 200 success code even if error occurs
How can I return 500 error response to the method
Can anyone let me know


Lokesh VarmanRank #374 · 6.8K reputationPosted Jul 8, 2023, 2:06 AM EDT
To return a 500 error response in your code, you can modify the catch block to throw an exception with an appropriate HTTP status code. Here's an example:
public async Task SaveData(Employee request) { try { return await InsertAsync(request); } catch (Exception ex) { // Throw an exception with HTTP status code 500 (Internal Server Error) throw new Exception("An error occurred while saving the data.", ex); } }Amit Kumar SinghRank #562 · 4.3K reputationPosted Jul 9, 2023, 1:15 AM EDT
Hello,
Refer below link
https://code-maze.com/global-error-handling-aspnetcore/