Move JSONHTTPException and JSONBadRequest to new module flask.exceptions.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Anton I. Sipos · committed by Anton I. Sipos · parent 9711fd4 · Browse files · 2 files·+50 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+50 added−38 removed
- other:+50 added−38 removed
Provenance
- Commit
71b3511- Tree
12c5c17- Author
Anton I. Sipos <aisipos@gmail.com>- Committer
Anton I. Sipos <aisipos@gmail.com>- Parent
9711fd4
2 files changed+50 added−38 removed
added:flask/exceptions.py+49 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.exceptions4+ ~~~~~~~~~~~~5+6+ Flask specific additions to :class:`~werkzeug.exceptions.HTTPException`7+8+ :copyright: (c) 2011 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+from werkzeug.exceptions import HTTPException, BadRequest12+from .helpers import json13+14+15+class JSONHTTPException(HTTPException):16+ """A base class for HTTP exceptions with ``Content-Type:17+ application/json``.18+19+ The ``description`` attribute of this class must set to a string (*not* an20+ HTML string) which describes the error.21+22+ """23+24+ def get_body(self, environ):25+ """Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to26+ return the description of this error in JSON format instead of HTML.27+28+ """29+ return json.dumps(dict(description=self.get_description(environ)))30+31+ def get_headers(self, environ):32+ """Returns a list of headers including ``Content-Type:33+ application/json``.34+35+ """36+ return [('Content-Type', 'application/json')]37+38+39+class JSONBadRequest(JSONHTTPException, BadRequest):40+ """Represents an HTTP ``400 Bad Request`` error whose body contains an41+ error message in JSON format instead of HTML format (as in the superclass).42+43+ """44+45+ #: The description of the error which occurred as a string.46+ description = (47+ 'The browser (or proxy) sent a request that this server could not '48+ 'understand.'49+ )modified:flask/wrappers.py+1 added-38 removed
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 12 from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase13-from werkzeug.exceptions import BadRequest, HTTPException13 from werkzeug.utils import cached_property14 15+from .exceptions import JSONBadRequest16 from .debughelpers import attach_enctype_error_multidict17 from .helpers import json, _assert_have_json18 from .globals import _request_ctx_stack19 20 21-class JSONHTTPException(HTTPException):22- """A base class for HTTP exceptions with ``Content-Type:23- application/json``.24-25- The ``description`` attribute of this class must set to a string (*not* an26- HTML string) which describes the error.27-28- """29-30- def get_body(self, environ):31- """Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to32- return the description of this error in JSON format instead of HTML.33-34- """35- return json.dumps(dict(description=self.get_description(environ)))36-37- def get_headers(self, environ):38- """Returns a list of headers including ``Content-Type:39- application/json``.40-41- """42- return [('Content-Type', 'application/json')]43-44-45-class JSONBadRequest(JSONHTTPException, BadRequest):46- """Represents an HTTP ``400 Bad Request`` error whose body contains an47- error message in JSON format instead of HTML format (as in the superclass).48-49- """50-51- #: The description of the error which occurred as a string.52- description = (53- 'The browser (or proxy) sent a request that this server could not '54- 'understand.'55- )56-57-21 class Request(RequestBase):22 """The request object used by default in Flask. Remembers the23 matched endpoint and view arguments.24 125 unmodified lines