Skip to content
Dispatch Dispatch Dispatch

httpx

Dispatch provides a built-in integration with httpx, offering proper error handling out of the box. It’s enabled by default and it takes no extra code to set it up.

import dispatch
@dispatch.function
def fetch_cat_fact():
response = httpx.get("https://cat-facts.dispatch.run")
# Raise `httpx.HTTPError` exception when request is not successful
response.raise_for_status()
return response.text
def main():
fetch_cat_fact.dispatch()
dispatch.run(main)

Exception handling

HTTPStatusError

Depending on the status_code attribute, a different Dispatch behavior is applied.

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

InvalidURL

Returns Status.INVALID_ARGUMENT.

UnsupportedProtocol

Returns Status.INVALID_ARGUMENT.

TimeoutException

Returns Status.TIMEOUT.

HTTPError

Returns Status.TEMPORARY_ERROR, unless exception is HTTPStatusError, InvalidURL, UnsupportedProtocol or TimeoutException, since HTTPError is a parent class of these. Learn more in httpx documentation.

StreamError

Returns Status.TEMPORARY_ERROR

CookieConflict

Returns Status.TEMPORARY_ERROR.

Response handling

If a function returns httpx.Response instance, Dispatch looks at the status_code attribute and determines behavior the same way as HTTPStatusError exception.

import dispatch
@dispatch.function
def fetch_cat_fact():
response = httpx.get("https://cat-facts.dispatch.run")
return response