Skip to content

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 dispatch
from dispatch.integrations.http import http_response_code_status
import httpx
@dispatch.function
def 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 codeDescriptionDispatch status
1xxInformationalStatus.PERMANENT_ERROR
2xxSuccessfulStatus.OK
3xxRedirectionStatus.PERMANENT_ERROR
3xxRedirectionStatus.PERMANENT_ERROR
4xxClient errorStatus.PERMANENT_ERROR
400Bad requestStatus.INVALID_ARGUMENT
401UnauthorizedStatus.UNAUTHENTICATED
403ForbiddenStatus.PERMISSION_DENIED
404Not foundStatus.NOT_FOUND
408Request timeoutStatus.TIMEOUT
429Too many requestsStatus.THROTTLED
5xxServer errorStatus.TEMPORARY_ERROR
501Not implementedStatus.PERMANENT_ERROR