error handling

This commit is contained in:
JJTech0130 2023-10-22 17:54:20 -04:00
parent 290164af8e
commit 6a3bee35d3
No known key found for this signature in database
GPG key ID: 23C92EBCCF8F93D6

View file

@ -4,6 +4,8 @@ import random
import typing import typing
import uuid import uuid
from typing import Literal from typing import Literal
from io import BytesIO
import requests import requests
@ -126,7 +128,26 @@ class CloudKitContainer:
verify=False, verify=False,
) )
print(r.content) _parse_response(r.content) # Will raise an exception if the response is an error
def _parse_response(response: bytes):
from io import BytesIO
length, read = _utils.ULEB128.decode_reader(BytesIO(response))
if length + read < len(response):
logger.warning(f"Response is longer than expected: {length + read} < {len(response)} (multiple messages?)")
response = response[read:length+read]
try:
r = cloudkit_pb2.ResponseOperation.FromString(response)
except Exception as e:
logger.warning(f"Failed to parse response: {e} {response.hex()}")
raise
if r.result.code != cloudkit_pb2.ResponseOperation.Result.Code.SUCCESS:
if r.result.code == cloudkit_pb2.ResponseOperation.Result.Code.FAILURE:
raise Exception(f"CloudKit request failed: {r.result.error.errorDescription}")
else:
raise Exception("Unknown CloudKit error")
def _build_record_save_request( def _build_record_save_request(