error handling
does the api.errorhandler work? I have tried using it but It doesn't seem to be working
Simply, yes, it should be working. However, there are multiple tickets open with some different aspects of it not properly being handled. Please review a few and see if they cover your case. If not, please expand on this issue to specify your exact issue
#33 #27 #103
I've just switched my existing flask-restplus project over to flask-restx. Thanks a ton to all contributors for taking this on!
I have custom error handlers that worked before and after just fine. The problem is that uncaught exceptions in flask-restplus would return 500 with the exception message, but flask-restx doesn't catch the error and return properly. Instead it returns the stack trace as HTML.
I've tried to override the default error handler, but I've found that I must specify the exact exceptions I want to catch (ValueError, RuntimeError, etc). There are obviously way too many exceptions to handle in this way. I've also found that I can catch with with just the Exception base class, but that interferes with the other exceptions.
What should I expect for proper REST API JSON responses and status codes for uncaught exceptions with/without custom error handlers?
@dmulter I'm having the same issue, migrating from flask-restplus. Did you find any solution to this?
No, I just enumerated all the Python exceptions:
@api.errorhandler(AssertionError)
@api.errorhandler(AttributeError)
@api.errorhandler(IndexError)
@api.errorhandler(IOError)
@api.errorhandler(KeyError)
@api.errorhandler(NameError)
@api.errorhandler(MemoryError)
@api.errorhandler(OSError)
@api.errorhandler(RuntimeError)
@api.errorhandler(SyntaxError)
@api.errorhandler(SystemError)
@api.errorhandler(TypeError)
@api.errorhandler(ValueError)
def handle_python_exception(error):
'''Python errors'''
return dict(message=f"Internal error: {str(error)}"), 500
I also ran into this issue. I found that turning off debug mode in my config DEBUG = False resulted in my error handling working as expected. But with DEBUG = True error bodies contain the HTML stack-trace similar to what @dmulter described. Hopefully that helps, although it would be nice if debug mode had the option to return error messaging and not an HTML stack-trace.