- Python Falcon - Discussion
- Python Falcon - Useful Resources
- Python Falcon - Quick Guide
- Python Falcon - Deployment
- Python Falcon - Testing
- Python Falcon - Sqlalchemy Models
- Python Falcon - Websocket
- Python Falcon - CORS
- Python Falcon - Middleware
- Python Falcon - Hooks
- Python Falcon - Error Handling
- Python Falcon - Status Codes
- Python Falcon - Cookies
- Python Falcon - Jinja2 Template
- Python Falcon - Inspect Module
- Falcon - Suffixed Responders
- Python Falcon - Routing
- Python Falcon - App Class
- Python Falcon - Resource Class
- Request & Response
- Python Falcon - API Testing Tools
- Python Falcon - Uvicorn
- Python Falcon - ASGI
- Python Falcon - Waitress
- Python Falcon - Hello World(WSGI)
- Python Falcon - WSGI vs ASGI
- Python Falcon - Environment Setup
- Python Falcon - Introduction
- Python Falcon - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Falcon - Error Handpng
To handle various error situations, the above status codes can be used for the response object. Falcon also provides set of error classes. Their object can be raised when corresponding runtime error situation arises.
These error classes are derived from HTTPError class as their base class. The error object is raised as shown in the following example −
import falcon class MyResource: def on_get(self, req, resp): # some Python code raise falcon.HTTPBadRequest( title="Value Out of Range", description="The value is not between permissible range" )
Predefined Error Classes
Some of the predefined error classes provided by Falcon are as follows −
HTTPBadRequest − 400 Bad Request. The server is unable to process the request due to a cpent error such as malformed request syntax, invapd request message framing etc.
HTTPInvapdHeader − Results in 400 Bad Request because one of the headers in the request is invapd.
HTTPInvapdParam − Represents 400 Bad Request. This error may refer to an invapd parameter in a query string, form, or document that was submitted with the request.
HTTPMissingParam − 00 Bad Request is raised when a parameter is missing from the request.
HTTPForbidden − The server understood the request but refuses to authorize it. The status code is 403 Forbidden.
HTTPNotFound − When the server did not find a current representation for the target resource, a 404 status code is raised. It does not indicate whether this lack of representation is temporary or permanent.
HTTPMethodNotAllowed − 405 Method Not Allowed. The method received in the request-pne is not supported by the target resource.
HTTPLengthRequired − When The server refuses to accept the request without a defined Content- Length. 411 Length Required. Error code.
HTTPUnsupportedMediaType − If the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. Equivalent status code is 415 Unsupported Media Type.
HTTPUnprocessableEntity − If the server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the contained instructions, the error status code raised is 422 Unprocessable Entity. For example, if an XML request body contains well-formed, but semantically erroneous, XML instructions.
HTTPTooManyRequests − A 429 Too Many Requests status code is raised when the user has sent too many requests in a given amount of time (“rate pmiting”).
HTTPInternalServerError − A very common error situation resulting in 500 Internal Server Error. The server encountered an unexpected condition that prevented it from fulfilpng the request.
HTTPNotImplemented − The 501 (Not Implemented) status code indicates that the server does not support the functionapty required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
HTTPServiceUnavailable − 503 Service Unavailable means that the server is currently unable to handle the request due to a temporary overload or scheduled maintenance.
MediaNotFoundError − 400 Bad Request. This Exception is raised by a media handler when trying to parse an empty body.
MediaMalformedError − 400 Bad Request. This Exception is raised by a media handler when trying to parse a malformed body.
Redirection
There are also a set of exceptions, which when raised, trigger a redirection response to the cpent. The status codes are of the type 3xx. These exceptions, represented by following classes, shot-circuit the request processing as a subclass of HttpError.
HTTPMovedPermanently − 301 Moved Permanently. This status code indicates that the target resource has been assigned a new permanent URI.
HTTPFound − 302 Found status code meaning that the target resource resides temporarily under a different URI.
HTTPTemporaryRedirect − This class raises the 307 (Temporary Redirect) status code, which means that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI.
HTTPPermanentRedirect − Results ib 308 Permanent Redirect, indicating that the target resource has been assigned a new permanent URI.