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:
RequestNetworkExceptionThe engine could not reach the server. The original error is in
__cause__.UnexpectedStatusCodeExceptionThe 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 a401answered by a proxy, raises it too. Thestatus_codeattribute holds the status code.UnexpectedContentTypeExceptionThe response carried neither
application/scim+jsonnorapplication/json. Thecontent_typeattribute holds the content type it carried.UnexpectedContentFormatExceptionThe response body was not JSON.
ResponsePayloadValidationExceptionThe body was JSON, but not a payload the expected model accepts. The
ValidationErrorlisting every invalid attribute is in__cause__.InvalidServiceDescriptionExceptionThe 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.