Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept the Validation error from Flask restplus namespace class

Currently the Namespace parser validates the request arguments and throws error like

{
    "errors": {
        "file": "Missing required parameter in an uploaded file"
    },
    "message": "Input payload validation failed"
}

From the flask-app i want to intercept or handle these exceptions and send a customised response for consistency like { "errors": { "file": "Missing required parameter in an uploaded file" }, "message": "Input payload validation failed", "id" : "some customer id " }

Is it possible to handle this exception from app level instead of doing it for every api

like image 489
geekops Avatar asked May 07 '26 12:05

geekops


1 Answers

According to this issue https://github.com/noirbizarre/flask-restplus/issues/530 there is a workaround to have a personalized message.

That said, it is possible to implement this with the definition of the BadRequest error handler and modify the data attribute:

@api.errorhandler(BadRequest)
def bad_request(self):
    self.data.update({'id': 'some customer id'})

    return {}, 400

Though, there is no clean way to avoid the empty dictionary return, as it is discarded.

like image 107
Juan David Rodríguez Cuervo Avatar answered May 11 '26 01:05

Juan David Rodríguez Cuervo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!