Skip to content

Implementation bugs for exception catching! #912

@Panlq

Description

@Panlq

handler(request=request, response=response, exception=exception, **kwargs)

errors = self.validate(input_parameters, context)

if key in input_parameters:

def initialize_handler(handler, value, context):

return handler(value)

exception handler must trigger by bind exception raise
and the handler func args(exception) type hints is bind exception

initialize_handler re-instantiate the custom Exception and pass in the first kwargs(exception), which is actually an instance of the custom Exception.Step 5 above is equivalent to --> CustomException(CustomException()) This is a contradiction and a serious error. Excuse me, what was the purpose of designing this initialize_handler function at that time?

reproduce:

# bug.py
import hug
import time
import hug.development_runner


class UserError(Exception):
    code = 500
    message = "Invalid username"

    def __init__(self):
        ...


api = hug.API(__name__)


@hug.exception(UserError, api=api)
def handler_user_error(request, response, exception: UserError):
    response.status = hug.falcon.HTTP_200
    data = dict(data=None, msg=exception.message, timestamp=time.time(), code=exception.code)
    response.body = hug.output_format.json(data)


route = hug.http(api=api)


@route.get("/test")
def get_data(**kwargs):
    print("GET data")
    raise UserError()
# run-bug-server.py
import hug.development_runner
hug.development_runner.hug("bug.py", port=5000)
curl --location --request GET 'localhost:5000/test'
{"errors": {"exception": "__init__() takes 1 positional argument but 2 were given"}}
{
    "errors": {
        "exception": "__init__() takes 1 positional argument but 2 were given"
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions