Reputation: 141
I'm developing azure functions written in .NET CORE 3.1 using Visual Studio 2019. I have a catch block as below
catch (Exception ex)
{
log.LogError(ex, "Error: " + ex.Message + "");
var errormodel = new { isError = true, Message = ex.Message, errorInnerException = ex.InnerException, stackTrace = ex.StackTrace };
return new ObjectResult(errormodel)
{
};
throw ex;
}
I'm not able to capture status code here... Any idea how to capture status code in catch block?
Upvotes: 0
Views: 127
Reputation: 29950
You can install this package: ServiceStack 5.10.4, then in your catch block, you can just use the ToStatusCode()
method.
The code like below:
catch(Exception ex)
{
var x = ex.ToStatusCode();
log.LogError(ex, "Error: " + ex.Message + ", Status Code:" + ex.ToStatusCode());
var errormodel = new { isError = true, Message = ex.Message, errorInnerException = ex.InnerException, stackTrace = ex.StackTrace, StatusCode = ex.ToStatusCode() };
return new ObjectResult(errormodel)
{
};
throw ex;
}
Upvotes: 1