HTTP
Use http_response_code_status
to determine the correct Dispatch behavior depending on the HTTP status code, regardless of the HTTP client you prefer.
import dispatchfrom dispatch.integrations.http import http_response_code_statusimport httpx
@dispatch.functiondef fetch_cat_fact(): response = httpx.get("https://cat-facts.dispatch.run")
# Raise `httpx.HTTPStatusError` exception when request is not successful response.raise_for_status()
return response.text
def handle_request_error(error: httpx.HTTPStatusError): return http_response_code_status(error.response.status_code)
dispatch.register_error_type(httpx.HTTPStatusError, handle_request_error)
def main(): fetch_cat_fact.dispatch()
dispatch.run(main)
Here’s a reference of HTTP status codes and their corresponding Dispatch status.
HTTP status code | Description | Dispatch status |
---|---|---|
1xx | Informational | Status.PERMANENT_ERROR |
2xx | Successful | Status.OK |
3xx | Redirection | Status.PERMANENT_ERROR |
3xx | Redirection | Status.PERMANENT_ERROR |
4xx | Client error | Status.PERMANENT_ERROR |
400 | Bad request | Status.INVALID_ARGUMENT |
401 | Unauthorized | Status.UNAUTHENTICATED |
403 | Forbidden | Status.PERMISSION_DENIED |
404 | Not found | Status.NOT_FOUND |
408 | Request timeout | Status.TIMEOUT |
429 | Too many requests | Status.THROTTLED |
5xx | Server error | Status.TEMPORARY_ERROR |
501 | Not implemented | Status.PERMANENT_ERROR |