user13411047
user13411047

Reputation:

Create simple flask error application to log exceptions

I've uploaded some code into a server. The code was working locally but when I upload it to the server it gives me an Internal Server Error. The website is running with wsgi and the code is:

    try:
        from decksite import main, APP as application
    except Exception as e:
        from shared import repo
        repo.create_issue('Error starting website', exception=e)
    
    if __name__ == '__main__':
        print('Running manually.  Is something wrong?')
        application.run(host='0.0.0.0', debug=False)

So both the try and the except are failing. I want to add a second exception and pass it all to a simple flask application that would output both exceptions to the browser and log them to a file. The problem is that I don't know how to pass the exception to the error_app flask app and that it breaks in the line where I set the logging config. Here is what I've done. I'm only getting NoneType: None instead of the full exception.

import os, sys
sys.path.append("/home/myuser/public_html/flask");

try:
    from decksite import main, APP as application
except Exception as error:
    #from shared import repo
    #repo.create_issue('Error starting decksite', exception=error)

    #sys.path.insert(0, os.path.dirname(__file__))
    #from error_app import app as application

    # This is the code that goes into the error flask application
    import logging
    import traceback
    from flask import Flask, __version__
    app = Flask(__name__)
    application = app

    @app.route("/")
    def hello():
        return traceback.format_exc()
        # The next line gives Internal Server Error
        logging.basicConfig(filename='example.log', level=logging.DEBUG)
        logging.exception(error)
        return traceback.format_exc()


if __name__ == '__main__':
    print('Running manually.  Is something wrong?')
    application.run(host='0.0.0.0', debug=False)

I don't have sudo in the server and can't ssh to it so unless I'm able to log the errors I'm not going to be able to fix anything.

Edit: I've almost got it as I want:

.htaccess

website.wsgi

error_app.py

website/init.py

website/main.py

Upvotes: 2

Views: 634

Answers (2)

Mohit Kumar
Mohit Kumar

Reputation: 633

You mentioned 500 Internal Server Error is coming. Things are proper in your local but fail on server. Since you don't have ssh access, it might be tough to debug. If you use something like Docker or Kubernetes to build and deploy it can be useful. I can suggest you some ways to debug. The code is not going to try except and failing, the possible reason is the server itself not starting due to missing requirements say some import or it could be anything.
Debug Steps

  • Create a virtual environment and re-install requirements in it similar to your server. This will help you to identify if there is a missing requirement.
  • if you are environment is not production and you are testing the application in the server then put debug = True. It will show an error on the front end. This is definitely not a recommended approach. I am suggesting since you don't have ssh access.
  • If possible create a simple route say /hello and in that just return hello, and check whether it is returning you the right result or not. This will let you know whether your server is starting or not. Even this is not recommendable for production.
  • You can also check the flask app before request and after request. This might also be useful

  • Hopefully, 1st step debugging will help you a lot and you might not need to go to step 2 and step 3. I gave you for the worst-case scenario

    Upvotes: 1

    noslenkwah
    noslenkwah

    Reputation: 1744

    Create a custom 500 handler and print out the trackback

    import traceback
    
    @app.errorhandler(500)
    def internal_server_error(e):
        return render_template('500_error.html', traceback=traceback.format_exc())
    

    Have your '500_error.html' template show you the traceback.

    Upvotes: 2

    Related Questions