Package: layaberr
Exceptions handling for layabTo be able to throw exceptions in your code and send a proper HTTP response automatically to your client your need to add error handler(s) to your API and endpoints.Add all handlers at oncefromflask_restplusimportResourceimportlayaberrapi=None# Your flask-restplus API instanceerror_responses=layaberr.add_error_handlers(api)@api.route("/your_endpoint")@api.doc(**error_responses)classYourFlaskRestPlusResource(Resource):defget(self):return"test"Add some handlersfromflask_restplusimportResourceimportlayaberrapi=None# Your flask-restplus API instanceerror_response=layaberr.add_failed_validation_handler(api)@api.route("/your_endpoint")classYourFlaskRestPlusResource(Resource):@api.response(error_response)defget(self):return"test"Supported ExceptionsThe following exceptions are handled:ValidationFailedIn case your endpoint raises ValidationFailed, an HTTP error 400 (Bad Request) will be sent to the client.Error not specific to a field in received dataThis code:fromlayaberrimportValidationFailedreceived_data=NoneraiseValidationFailed(received_data,message="This is the error message")Will result in the following JSON response sent to the client:{"fields":[{"item":1,"field_name":"","messages":["This is the error message"]}]}Error specific to a field in a received dictionaryThis code:fromlayaberrimportValidationFailedreceived_data={"field 1":"value 1"}raiseValidationFailed(received_data,errors={"field 1":["Invalid value"]})Will result in the following JSON response sent to the client:{"fields":[{"item":1,"field_name":"field 1","messages":["Invalid value"]}]}Error specific to a field in a received list of dictionariesThis code:fromlayaberrimportValidationFailedreceived_data=[{"field 1":"value 1"},{"field 1":"value 2"}]raiseValidationFailed(received_data,errors={1:{"field 1":["Invalid value"]}})Will result in the following JSON response sent to the client:{"fields":[{"item":2,"field_name":"field 1","messages":["Invalid value"]}]}ModelCouldNotBeFoundIn case your endpoint raises ModelCouldNotBeFound, an HTTP error 404 (Not Found) will be sent to the client.This code:fromlayaberrimportModelCouldNotBeFoundrequested_data=NoneraiseModelCouldNotBeFound(requested_data)Will result in the following JSON response sent to the client:{"message":"Corresponding model could not be found."}BadRequestIn case your endpoint raises BadRequest, an HTTP error 400 (Bad Request) will be sent to the client.This code:fromwerkzeug.exceptionsimportBadRequestraiseBadRequest("The exception message")Will result in the following JSON response sent to the client:{"message":"The exception message"}UnauthorizedIn case your endpoint raises Unauthorized, an HTTP error 401 (Unauthorized) will be sent to the client.This code:fromwerkzeug.exceptionsimportUnauthorizedraiseUnauthorized("The exception message")Will result in the following JSON response sent to the client:{"message":"The exception message"}ForbiddenIn case your endpoint raises Forbidden, an HTTP error 403 (Forbidden) will be sent to the client.This code:fromwerkzeug.exceptionsimportForbiddenraiseForbidden("The exception message")Will result in the following JSON response sent to the client:{"message":"The exception message"}Exception (this is the default handler)In case your endpoint raises an Exception, an HTTP error 500 (Internal Server Error) will be sent to the client.This code:raiseException("The exception message")Will result in the following JSON response sent to the client:{"message":"The exception message"}How to installpython 3.6+must be installedUse pip to install module:python-mpipinstalllayaberr