hug icon indicating copy to clipboard operation
hug copied to clipboard

Implementation bugs for exception catching!

Open Panlq opened this issue 3 years ago • 0 comments

https://github.com/hugapi/hug/blob/8b5ac00632543addfdcecc326d0475a685a0cba7/hug/interface.py#L941

https://github.com/hugapi/hug/blob/8b5ac00632543addfdcecc326d0475a685a0cba7/hug/interface.py#L910

https://github.com/hugapi/hug/blob/8b5ac00632543addfdcecc326d0475a685a0cba7/hug/interface.py#L273

https://github.com/hugapi/hug/blob/8b5ac00632543addfdcecc326d0475a685a0cba7/hug/interface.py#L359

https://github.com/hugapi/hug/blob/8b5ac00632543addfdcecc326d0475a685a0cba7/hug/interface.py#L363

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"
    }
}

Panlq avatar Dec 09 '22 12:12 Panlq