Cornices / cornice

Build Web Services with Pyramid.
https://cornice.readthedocs.io
Other
384 stars 149 forks source link

JSONError inheriting from HTTPError causes named error with details #586

Open fmigneault opened 6 months ago

fmigneault commented 6 months ago

Somewhat like https://github.com/Cornices/cornice/issues/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 commented 4 months ago

The cause is related to this initialization call: https://github.com/Cornices/cornice/blob/abfcfd9eeefaf281237ba84a3b90ef5ee0698aa9/src/cornice/renderer.py#L13-L18

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 commented 4 months ago

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