I am not sure whether I understand your question properly or not.
But you can use following approach to return respond with HTTP 500 status on any unspecific exception:
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())
Or you can also use Decorators in python as follow:
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())
generic_error_handler
, which will otherwise gobble it up (see here) – Semblable