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

JSONError inheriting from HTTPError causes named error with details #586

Open
fmigneault opened this issue Apr 15, 2024 · 2 comments
Open

Comments

@fmigneault
Copy link

Somewhat like #275, but since it dates from 2015, things changed a lot in the code.

Because cornice.renderer.JSONError inherits from pyramid.httpexceptions.HTTPError, its __str__ implementation looks for the detail attribute. A simple logging call such as logger.info("exception: {!s}", err) is enough to cause this exception to crash.

The HTTPException itself the following:

    def __str__(self):
        return str(self.detail) if self.detail else self.explanation

So either the JSON body could be set to detail and let it represent it as plain string, or define detail/explanation accordingly with some more explicit message.

@fmigneault
Copy link
Author

The cause is related to this initialization call:

class JSONError(exc.HTTPError):
def __init__(self, serializer, serializer_kw, errors, status=400):
body = {"status": "error", "errors": errors}
Response.__init__(self, serializer(body, **serializer_kw))
self.status = status
self.content_type = "application/json"

Because the Response.__init__ is called rather than HTTPError.__init__, not all attributes expected by this class get properly assigned, and notably, the detail attribute that is defined only under HTTPError, and not Response.

@fmigneault
Copy link
Author

@leplatrem Is it possible to see if replacing the base class __init__ call is sufficient to fix this issue?

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