Skip to main content

Library that provides a easy to use set of http errors along with useful descriptions

Project description

Http errors provide a list of easy to test and descriptive set of python errors.

Code example

Raising exception

from httperrors import BadRequestError
class View(BaseView):
    def validate(self, request):
        ...
        if not request.body.is_json():
            raise BadRequestError(
                error_message="You must provide a JSON body",
                error_code="NOT_A_JSON_BODY_ERROR",
            )
    def run(self,request):
        ...

Serialization

You will be able to catch this exception somewhere in the flow of your application, map it and serialize it to the corresponding request.

from httperrors import BadRequestError
class BaseView:
def __init__(self):
    try:
        self.validate()
    except BadRequestError as e:
        return json_response(
            status_code=e.status_code,
            body=e.serialize()
        )

Testing

You will be able to easily test your application

from httperrors import BadRequestError
class TestView:
def test_it_raises_bad_request_error_not_json_body(self):
    view = View()
    request = Mock()
    request.body.is_json.return_value = False
    with pytest.raises(BadRequestError) as error:
        view.validate()
        assert error.error_code == "NOT_A_JSON_BODY_ERROR"

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

httperrors-1.0.0.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

httperrors-1.0.0-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

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