coderfromhell
coderfromhell

Reputation: 455

How to handle errors that don't have any HTTP codes in a Flask app?

My Flask app takes input data from the user and performs some ML operations on them. Sometimes, the input given by the user is not correct so, it throws errors. Now, I have tried to make my code more robust so as to handle the values well but sometimes, it ends up showing up some value/type errors like:

TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U16') dtype('<U16') dtype('<U16') 

TypeError: unsupported operand type(s) for -: 'list' and 'list' 

ValueError: Unable to parse string " " at position 488

These are usually accompanied by HTTP status code 500, but all these errors are for different causes. Is there any way I can handle and convey the exact error message to the user in a proper way?

Upvotes: 0

Views: 39

Answers (1)

ced_c
ced_c

Reputation: 149

You must implement an error handler that catch your specific exceptions and return whatever http error you want (with message)

https://flask.palletsprojects.com/en/1.1.x/errorhandling/

In you case you might want to return a 400 bad request

Upvotes: 1

Related Questions