Handle errors#

Use this guide to tell apart the ways a request can fail, and to decide which ones an application catches. Two families of exception say two different things: whether the exchange happened at all, and whether the server accepted what it carried.

Catch the error a server returns#

A server refusing a request returns an Error object, which scim2-client raises as the matching SCIMException subclass. to_error() gives the object back:

from scim2_models import SCIMException

try:
    response = scim.create(request)
except SCIMException as exc:
    error = exc.to_error()
    print(f"SCIM error [{error.status}] {error.scim_type}: {error.detail}")
from scim2_models import SCIMException

try:
    response = await scim.create(request)
except SCIMException as exc:
    error = exc.to_error()
    print(f"SCIM error [{error.status}] {error.scim_type}: {error.detail}")

An application preferring a value to an exception passes raise_scim_errors as False, on the client or on one call, and reads the Error among the return values:

from scim2_models import Error

response = scim.create(request, raise_scim_errors=False)
if isinstance(response, Error):
    print(response.detail)

Catch the errors of the exchange itself#

Everything scim2-client raises on its own derives from SCIMClientException, and says at which point the exchange stopped:

RequestNetworkException

The engine could not reach the server. The original error is in __cause__.

UnexpectedStatusCodeException

The server answered a status code the operation does not define, listed per operation in the class attributes of SCIMClient. A failure without a SCIM error, such as a 401 answered by a proxy, raises it too. The status_code attribute holds the status code.

UnexpectedContentTypeException

The response carried neither application/scim+json nor application/json. The content_type attribute holds the content type it carried.

UnexpectedContentFormatException

The response body was not JSON.

ResponsePayloadValidationException

The body was JSON, but not a payload the expected model accepts. The ValidationError listing every invalid attribute is in __cause__.

InvalidServiceDescriptionException

The objects the server publishes do not describe a service that holds together. Raised by discover().

Every one but the first shares SCIMResponseException, which catches any fault of a response in one clause:

from scim2_client import SCIMResponseException

try:
    user = scim.query(User, "my-user-id")
except SCIMResponseException as exc:
    print(f"The server answered something unusable: {exc}")

Read the response behind an exception#

An exception raised while reading a response keeps this response in its response attribute, with its status code and its headers. This holds for the SCIMClientException subclasses and for the SCIMException raised for an error returned by the server. The attribute is None when no response was read, for instance when the request never reached the server:

from scim2_client import SCIMResponseException
from scim2_models import SCIMException

try:
    user = scim.query(User, "my-user-id")
except (SCIMException, SCIMResponseException) as exc:
    if exc.response is not None:
        print(exc.response.status_code, exc.response.headers.get("Retry-After"))

Read the cause of a request the client refused#

A dict payload that does not comply is refused before anything is sent, so no request reaches the server. The SCIMException raised keeps the ValidationError listing every invalid attribute in __cause__:

from scim2_models import SCIMException

payload = {
    "schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
    "userName": None,
}
try:
    scim.create(payload)
except SCIMException as exc:
    for detail in exc.__cause__.errors():
        print(detail["loc"], detail["msg"])

Tolerate a non-conformant peer covers sending such a payload anyway, for a server whose expectations differ from the specifications.