0

Rosanswers logo

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.


Originally posted by Rufus on ROS Answers with karma: 1083 on 2021-08-12

Post score: 0

Rufus
  • 466
  • 1
  • 5
  • 9

1 Answers1

0

Rosanswers logo

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


Originally posted by Rufus with karma: 1083 on 2021-08-12

This answer was ACCEPTED on the original site

Post score: 0

Rufus
  • 466
  • 1
  • 5
  • 9