python - Implementing API exception flask-restful -


i trying catch exception raised when url provided messy , wrong url , return error response json. did implement logic.

the exception raised inside analysis class when key_id not valid key s3.

def url_error(status_code, message, reason):     response = jsonify({         'status': status_code,         'message': message,         'reason': reason     })     response.status_code = status_code     return response   class rowcolumncount(resource):     def get(self, key_id):         try:             rc = analysis(key_id=key_id)         except s3responseerror e:             return url_error(e.status, e.message, e.reason)         json_response = json.loads(rc.count_rows_columns())         return json_response 

the above code works fine kinda getting repetitive 50 different resource classes. each resource class should handle specific error. how make decorator, such code repetitiveness reduced.

i using flask, flask-restful, python 3.4.3

there couple of ways can achieve you're trying think cleanest way extend resource class described in flask-restful docs here , create new decorator job catch s3responseerror , return appropriate response. can subclass resources new base resource class.

also suggest specify api level json_output method described here , here way have return dict of resources , they'll converted json appropriately.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -