I Ich bin mir nicht sicher, ob ich deine Frage richtig verstehe oder nicht.
Aber Sie können folgende Vorgehensweise verwenden, um zu reagieren Rückkehr mit HTTP 500 den Status von unspezifischen Ausnahme:
class MyFirstAPI:
def on_post(self, req, res):
try:
json_data = json.loads(req.stream.read().decode('utf8'))
# some task
res.status = falcon.HTTP_200
res.body = json.dumps({'status': 1, 'message': "success"})
except Exception as e:
res.status = falcon.HTTP_500
res.body = json.dumps({'status': 0,
'message': 'Something went wrong, Please try again'
})
app = falcon.API()
app.add_route("/my-api/", MyFirstAPI())
Oder Sie können auch Dekorateure in Python wie folgt verwendet werden:
def my_500_error_decorator(func):
def wrapper(*args):
try:
func(*args)
except Exception as e:
resp.status = falcon.HTTP_500
resp.body = json.dumps({'status': 0, 'message': 'Server Error'})
return wrapper
class MyFirstAPI:
@my_500_error_decorator
def on_post(self, req, res):
try:
json_data = json.loads(req.stream.read().decode('utf8'))
# some task
res.status = falcon.HTTP_200
res.body = json.dumps({'status': 1, 'message': "success"})
app = falcon.API()
app.add_route("/my-api/", MyFirstAPI())