I'm using Python and Flask, served by Waitress, to host a POST
API. I'm calling the API from a C# program that posts data and gets a string response. At least 95% of the time, it works fine, but sometimes the C# program reports an error:
(500) Internal Server Error
.
There is no further description of the error or why it occurs. The only clue is that it usually happens in clusters -- when the error occurs once, it likely occurs several times in a row. Without any intervention, it then goes back to running normally.
Since the error is so rare, it is hard to troubleshoot. Any ideas as to how to debug or get more information? Is there error handling I can do from either the C# side or the Flask/Waitress side?
Make sure debug mode is off, then try again. Here is a comment directly from the code itself: Default exception handling that kicks in when an exception occurs that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged and the handler for a 500 internal server error is used.
There is a problem with the resource you are looking for, and it cannot be displayed. The first solution to a 500 internal server error is to refresh the page. If the error persists, you may try clearing the cookies, deactivating faulty plugins or themes, fixing the . htaccess file, or contacting your hosting provider.
Flask supports registering error handlers. Defining error handlers enables customizing the server error logs as well as returning more meaningful information to the client.
import logging
import traceback
from flask import Flask, jsonify
from werkzeug.exceptions import HTTPException
app = Flask(__name__)
logger = logging.getLogger()
@app.errorhandler(HTTPException)
def handle_http_exception(error):
error_dict = {
'code': error.code,
'description': error.description,
'stack_trace': traceback.format_exc()
}
log_msg = f"HTTPException {error_dict.code}, Description: {error_dict.description}, Stack trace: {error_dict.stack_trace}"
logger.log(msg=log_msg)
response = jsonify(error_dict)
response.status_code = error.code
return response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With