6

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?

Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
user3450049
  • 825
  • 1
  • 10
  • 20

2 Answers2

3

Flask supports registering error handlers. Defining error handlers enables customizing the server error logs as well as returning more meaningful information to the client.

Example

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
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • thanks for the answer, complete with code. I implemented the code block you suggested and nothing appears in the log when I get the 500 error. Do I need to wrap my code in a try/catch and raise the HTTPException in the catch part? Any other reasons why I could be getting an error but not triggering the HTTPException? – user3450049 Sep 26 '19 at 03:37
  • @user3450049 Try catching the Python `Exception` class rather than `HTTPException` to ensure that all exceptions are handled. – Christopher Peisert Sep 26 '19 at 04:02
  • I understand that we have the python root logger, flask logger (flask.app), werkzeug logger if we use the flask server and a production server logger if we use any other production server. Can you point me to any documentation to understand what gets logged in which logger please – variable Jan 16 '20 at 11:56
0

Your flask application should be logging the exception when it occurs. Aside from combing through your logs (which should be stored somewhere centrally) you could consider something like Sentry.io, which is pretty easy to setup with Flask apps.

stets
  • 101
  • 1
  • 3
  • Thanks for the answer. My issue is that I don't know where the error is raised, nothing appears in the logs. I need to be able to catch an error and log it -- any ideas there? – user3450049 Sep 26 '19 at 03:38