Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation bugs for exception catching! #912

Open
Panlq opened this issue Dec 9, 2022 · 0 comments
Open

Implementation bugs for exception catching! #912

Panlq opened this issue Dec 9, 2022 · 0 comments

Comments

@Panlq
Copy link

Panlq commented Dec 9, 2022

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"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant