class HTTPException(Exception):
def __new__(cls, error_code, error_message):
return {
"statusCode": error_code,
"body": error_message,
"headers": {"Access-Control-Allow-Origin": "*"},
}
is violating the contract for __new__, failing to return a new instance of HTTPException, and instead returning a random dict object. If you intended to wrap said dict in the exception, the sane way to do it would be to use __init__ (because you don't want to return something that isn't and HTTPException, nor use singletons or caches or immutable objects, which are the reasons to use __new__ instead), and delegate the dict up to the superclass initializer:
class HTTPException(Exception):
def __init__(self, error_code, error_message): # __init__ taking self
# Pass dict up to superclass initializer
super().__init__({
"statusCode": error_code,
"body": error_message,
"headers": {"Access-Control-Allow-Origin": "*"},
})
@classmethod
def magic_func(cls, err):
return cls(
error_code=err.Code,
error_message=err.Message,
)
Hopefully, whatever catches your exception knows to look for that attached dict, because if not, you're going to need to find where that's happening and return the dict the expected way.