aas_client3_2.errors

Map the unexpected responses of the server to exceptions.

A response with an unexpected status code raises an HttpError, or one of its subclasses. A response which can not be de-serialized raises an UnexpectedResponse.

We wrap the HTTP errors in our own exceptions instead of letting the transport raise them, or returning the status codes to the users, for the following reasons:

  • The specification gives the status codes a meaning. Section “Mapping of Status Codes” of the specification (Table 4, see https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.2/http-rest-api/http-rest-api.html#mapping-of-status-codes) maps the generic outcomes of the operations to the HTTP status codes, such as 404 for a missing resource and 409 for a resource which already exists. Hence, the users catch NotFound or Conflict instead of comparing integers, and the functions return only native data in the happy path.

  • We follow the meanings of Table 4 literally. A status code which a server easily sends in a different situation keeps its own exception, so that the users do not draw the wrong conclusion:

    • 403 means that “the server refuses the action due to certain restrictions, e.g. profiles or roles”. Hence, a server which implements only a subset of the operations as a service specification profile might respond with Forbidden, the same as for a missing role.

    • 405 means that “the server rejected the request for the requested resource, e.g. /invoke only for the operation submodel element”. This is a request which does not fit the resource, not a missing implementation, so we raise MethodNotAllowed.

    • 501 means that “the server does not support the functionality to fulfill the request”, so we raise NotImplementedByServer.

  • The specification defines the payload of the error responses. The servers report their messages as a Result with a list of Message (see https://industrialdigitaltwin.io/aas-specifications/IDTA-01002/v3.2/specification/interfaces-payload.html#Result). We parse it into HttpError.result, so that the users get the messages of the server as native data, and the message of the exception includes them. However, the specification does not oblige the servers to always provide the messages, and a reverse proxy in front of a server responds with a body of its own. Hence, the result is optional, and the exception always carries the raw HttpError.response, including the headers such as WWW-Authenticate.

  • The client does not depend on any HTTP library. The transports report every status code as an ordinary response, so the errors of the client do not depend on the exceptions of an HTTP library, such as requests.HTTPError. Only the network errors are raised by the transport itself, and propagate unchanged. All the errors of the client derive from Error, so that the users can tell them apart from the network errors.

aas_client3_2.errors.MAX_STREAMED_ERROR_BODY_SIZE: Final[int] = 1048576

Maximum number of bytes read from the body of an error response when streaming, so that a misbehaving server can not exhaust the memory

exception aas_client3_2.errors.Error[source]

Represent the base for all the errors raised by the client.

exception aas_client3_2.errors.HttpError(response: Response, result: Result | None)[source]

Signal that the server responded with an unexpected status code.

__init__(response: Response, result: Result | None) → None[source]

Initialize with the given values.

response: Response

Raw response of the server

result: Result | None

Messages of the server, if the body could be parsed as a result

exception aas_client3_2.errors.BadRequest(response: Response, result: Result | None)[source]

Signal that the server responded with 400 Bad Request.

exception aas_client3_2.errors.Unauthorized(response: Response, result: Result | None)[source]

Signal that the server responded with 401 Unauthorized.

The header WWW-Authenticate of the response usually tells why, for example, error="invalid_token".

exception aas_client3_2.errors.Forbidden(response: Response, result: Result | None)[source]

Signal that the server responded with 403 Forbidden.

exception aas_client3_2.errors.NotFound(response: Response, result: Result | None)[source]

Signal that the server responded with 404 Not Found.

exception aas_client3_2.errors.Conflict(response: Response, result: Result | None)[source]

Signal that the server responded with 409 Conflict.

exception aas_client3_2.errors.MethodNotAllowed(response: Response, result: Result | None)[source]

Signal that the server responded with 405 Method Not Allowed.

According to the specification, the request does not fit the requested resource, for example, /invoke on a submodel element which is not an operation.

exception aas_client3_2.errors.NotImplementedByServer(response: Response, result: Result | None)[source]

Signal that the server does not implement the operation.

The server responded with 501 Not Implemented.

exception aas_client3_2.errors.UnexpectedResponse(response: Response, error: Exception)[source]

Signal that the response of the server could not be de-serialized.

__init__(response: Response, error: Exception) → None[source]

Initialize with the given values.

response: Response

Raw response of the server

error: Exception

Error raised while de-serializing the response

aas_client3_2.errors.parse_result(response: Response) → Result | None[source]

Parse the body of an error response as a result with the server’s messages.

The body is parsed only if its Content-Type is JSON. The servers often send HTML or plain text as an error body, for example, from a reverse proxy; the error is still raised then, only without the messages.

Parameters:

response – error response

Returns:

the result, or None if the body is not a valid result in JSON

aas_client3_2.errors.error_from_response(response: Response) → HttpError[source]

Map the response with an unexpected status code to an error.

Parameters:

response – response of the server

Returns:

the error to be raised

aas_client3_2.errors.raise_for_unexpected_status(response: Response, expected: AbstractSet[int]) → None[source]

Raise the error mapped from response if its status is unexpected.

Parameters:
  • response – response of the server

  • expected – status codes which signal success

Raises:

HttpError if the status code is not in expected

aas_client3_2.errors.error_from_streamed_response(streamed: StreamedResponse, max_body_size: int = 1048576) → HttpError[source]

Read at most max_body_size bytes of the body, and map it to an error.

The rest of the body is not read. The caller releases the connection by exiting the context of the stream.

Parameters:
  • streamed – streamed response with an unexpected status code

  • max_body_size – maximum number of bytes read from the body

Returns:

the error to be raised