Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send message with actionresult

I need to return the server error from azure functions.

Now I implement the same using InternalServerErrorResult(). It only sends the error code and no response/message can be sent with this function.

How to implement an exception handler where the error code and message can be sent together using actionresult in azure functions

current implementation

catch (Exception ex)
            {
                log.LogInformation("An error occured {0}" + ex);
                //json = new Response(ex.StackTrace, AppConstants.ErrorCodes.SystemException).SerializeToString();
                return (ActionResult)new InternalServerErrorResult();
            }

this returns with an empty response in postman with error 500

like image 454
Sanjeev S Avatar asked Sep 18 '25 08:09

Sanjeev S


1 Answers

Note that this is from Microsoft.AspNetCore.Mvc namespace:

var result = new ObjectResult(new { error = "your error message here" })
{
    StatusCode = 500
};

Based on configured formatters it will return serialized object to client. For JSON (it's default) it will return following:

{ "error" : "your error message here" }
like image 98
bot_insane Avatar answered Sep 19 '25 22:09

bot_insane