How to Raise an exception without logging it in python

70 Views Asked by At

I am using AWS Lambda as An Authoriser to validate the users that use the application and it is necessary to raise an Exception("UnAuthorized") to return 401 code to the user. Now every time Exception("UnAuthorized") is raised, Stack trace and an error level log gets published.

I dont want to see this error log, since this is causing issue with log alerts.

ie Raise Exception("UnAuthorized") should do as expected but not show any logs.

1

There are 1 best solutions below

1
Alex Chadyuk On

You can simply return 401 code rather than raising an Exception:

return {
    "statusCode": 401,
    "statusDescription": "Unauthorized",
    "headers": {
        "Content-Type": "application/json"
    },
    "body": json.dumps({
        "Comment": "comment"
    })
}