Rahul Dev
Rahul Dev

Reputation: 141

How to capture status code in catch block of an azure function written .net core 3.1 using visual studio 2019

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

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

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

Related Questions