Reputation: 99
I'm building my user service and am trying to decouple it from the API. When the user service encounters an error, it sets an error attribute on the service in the following format:
{
'message': '<user friendly error message>',
'code': '<http code>'
}
However, I don't want to reference falcon at all within the service, so it looks like this in practice:
self.error = {
'message': 'Duplicate Email',
'code': 409
}
return False
Now once this is returned to the API, I need to turn that into a falcon.HTTP_XXX object, is the best way of doing this to create a dictionary in the route that has the number as the key and the falcon objects as a reference? Such as:
responses = {
409: falcon.HTTP_409
400: falcon.HTTP_400
}
And then taking the return from the user service and then resolving the object be passing the error num as an index? Like:
user = self.user_service.get_user()
if not user:
resp.status = responses[self.user_service.error.code]
return
Upvotes: 0
Views: 230
Reputation: 99
Actually found the solution myself.
Instead of storing the responses in a dictionary, Falcon provides a utility to parse the status from an integer.
falcon.get_http_status(self.user_service.error.code)
https://falcon.readthedocs.io/en/stable/_modules/falcon/util/misc.html#get_http_status
Upvotes: 2