Skip to main content

Thin wrapper for layabase and layab error handling

Project description

Exceptions handling for layab

pypi version Build status Coverage Code style: black Number of tests Number of downloads

To 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 once

from flask_restplus import Resource
import layaberr

api = None # Your flask-restplus API instance
error_responses = layaberr.add_error_handlers(api)

@api.route("/your_endpoint")
@api.doc(**error_responses)
class YourFlaskRestPlusResource(Resource):
    def get(self):
        return "test"

Add some handlers

from flask_restplus import Resource
import layaberr

api = None # Your flask-restplus API instance
error_response = layaberr.add_failed_validation_handler(api)

@api.route("/your_endpoint")
class YourFlaskRestPlusResource(Resource):
    @api.response(error_response)
    def get(self):
        return "test"

Supported Exceptions

The following exceptions are handled:

ValidationFailed

In 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 data

This code:

from layaberr import ValidationFailed

received_data = None
raise ValidationFailed(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 dictionary

This code:

from layaberr import ValidationFailed

received_data = {"field 1": "value 1"}
raise ValidationFailed(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 dictionaries

This code:

from layaberr import ValidationFailed

received_data = [{"field 1": "value 1"}, {"field 1": "value 2"}]
raise ValidationFailed(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"]}]}

ModelCouldNotBeFound

In case your endpoint raises ModelCouldNotBeFound, an HTTP error 404 (Not Found) will be sent to the client.

This code:

from layaberr import ModelCouldNotBeFound

requested_data = None
raise ModelCouldNotBeFound(requested_data)

Will result in the following JSON response sent to the client:

{"message":  "Corresponding model could not be found."}

BadRequest

In case your endpoint raises BadRequest, an HTTP error 400 (Bad Request) will be sent to the client.

This code:

from werkzeug.exceptions import BadRequest

raise BadRequest("The exception message")

Will result in the following JSON response sent to the client:

{"message":  "The exception message"}

Unauthorized

In case your endpoint raises Unauthorized, an HTTP error 401 (Unauthorized) will be sent to the client.

This code:

from werkzeug.exceptions import Unauthorized

raise Unauthorized("The exception message")

Will result in the following JSON response sent to the client:

{"message":  "The exception message"}

Forbidden

In case your endpoint raises Forbidden, an HTTP error 403 (Forbidden) will be sent to the client.

This code:

from werkzeug.exceptions import Forbidden

raise Forbidden("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:

raise Exception("The exception message")

Will result in the following JSON response sent to the client:

{"message":  "The exception message"}

How to install

  1. python 3.6+ must be installed
  2. Use pip to install module:
python -m pip install layaberr

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layaberr-2.2.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

layaberr-2.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file layaberr-2.2.0.tar.gz.

File metadata

  • Download URL: layaberr-2.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.7

File hashes

Hashes for layaberr-2.2.0.tar.gz
Algorithm Hash digest
SHA256 e9347cbd3e14e1aca4f0480864302bc40ae2d4eae69f9459316b2b6c537ad2a2
MD5 4d00b696e14455cd688f0ff036c87fa4
BLAKE2b-256 c3c3cea72d9fdc943cd01a35c14c86f96729bc800d39eef066f00ef8bdb9bfd7

See more details on using hashes here.

File details

Details for the file layaberr-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: layaberr-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.6.7

File hashes

Hashes for layaberr-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4eb7f3484c71d4071fc81e7fd7b6d201ad667454a2c082c3e51fdbfa9eb59b28
MD5 853341b2d44b6cad21d8bd5cc2e5904f
BLAKE2b-256 6b47b2ac0f88ae79d2b3b5ff4f122096ef56bc74a09f7b78307ee78eea20972e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page