bag.web.exceptions module

Exceptions for web development.

exception bag.web.exceptions.Problem(error_msg, status_int=400, error_title=None, error_debug=None, **kw)[source]

Bases: Exception

Great exception class for application-level errors.

Service (or “action”) layers should be independent of web frameworks; however, often I feel I want the service layer to determine the HTTP code returned by a web service, instead of the controller layer. So I raise this exception in the service layer and capture it in the controller layer.

Very useful when coupled with the ajax_view decorator, which does the capture part.

When developing a user interface of any kind, it is great when all the webservices respect a standardized interface for returning errors. This class outputs these fields by convention:

  • error_msg: the string to be displayed to the end user
  • error_title: by default the HTTP error title
  • error_debug: should NOT be shown to end users; for devs only
HTTP = {400: 'Bad request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not found', 409: 'Conflict', 410: 'Gone', 413: 'Request entity too large', 422: 'Unprocessable entity', 500: 'Internal server error'}
exception bag.web.exceptions.Unprocessable(**adict)[source]

Bases: Exception

Exception that mimics Colander’s Invalid.

…because both have an asdict() method. However, this one can be instantiated without a schema. Example usage:

if not data.get('user_email'):
    raise Unprocessable(user_email='This field is required.')
bag.web.exceptions.ensure(condition, *a, **kw)[source]

Use an assert-like syntax to raise Problem exceptions.