ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Errors not showing in flask server

asked 2021-08-12 21:11:57 -0500

Rufus gravatar image

updated 2021-08-12 21:24:32 -0500

Consider the following minimal flask server

import rospy
from flask import Flask, request, Response, make_response

if __name__ == '__main__':
    # Shutdown is handled by Flask
    rospy.init_node("test", disable_signals=True)
    app = Flask(__name__)

    @App.errorhandler(Exception)
    def server_error(error):
        print(str(error))
        rospy.logerror(error) # For some reason logerror doesn't work in errorhandler
        return f"Error: {error}", 500

    @App.route('/test', methods=['POST'])
    def execute():
        raise Exception("my error")
        return Response(status=200, headers={})

    rospy.loginfo("Starting server...")
    app.run(host="127.0.0.1", port=50002)

When POSTing to 127.0.0.1:50002/test, the following 2 lines are printed

my error
500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

It seems rospy.logerror failed and caused another exception to be raised leading to the 500 Internal Server Error. Why is this happening and how do I solve this?

PS:: Why is "@ app" converted to "@App" in the post? This doesn't happen in the preview.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-12 21:22:48 -0500

Rufus gravatar image

Turns out, the issue is due to the typo logerror should be logerr

But more importantly, the reason more helpful error messages (e.g. AttributeError when using logerror) aren't appearing is becauserospy.init_node isn't started in its own thread. The following should be used instead

threading.Thread(target=lambda: rospy.init_node('example_node', disable_signals=True)).start()

as mentioned here

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-08-12 21:11:57 -0500

Seen: 238 times

Last updated: Aug 12 '21