|
5 | 5 | from typing import Any |
6 | 6 |
|
7 | 7 | import requests |
| 8 | +import json |
8 | 9 |
|
9 | 10 | from .const import ( |
10 | 11 | DEFAULT_UUID, |
|
26 | 27 | DexcomError, |
27 | 28 | SessionError, |
28 | 29 | SessionErrorEnum, |
| 30 | + ServerResponseError, |
| 31 | + ServerResponseErrorEnum, |
29 | 32 | ) |
30 | 33 | from .glucose_reading import GlucoseReading |
31 | 34 | from .util import _LOGGER, valid_uuid |
@@ -109,33 +112,36 @@ def _handle_response(self, response: requests.Response) -> DexcomError | None: |
109 | 112 |
|
110 | 113 | :param response: `requests.Response` to parse |
111 | 114 | """ |
112 | | - if response.json(): |
113 | | - _LOGGER.debug("%s", response.json()) |
114 | | - code = response.json().get("Code", None) |
115 | | - message = response.json().get("Message", None) |
116 | | - if code == "SessionIdNotFound": |
117 | | - error = SessionError(SessionErrorEnum.NOT_FOUND) |
118 | | - elif code == "SessionNotValid": |
119 | | - error = SessionError(SessionErrorEnum.INVALID) |
120 | | - elif code == "AccountPasswordInvalid": # defunct |
121 | | - error = AccountError(AccountErrorEnum.FAILED_AUTHENTICATION) |
122 | | - elif code == "SSO_AuthenticateMaxAttemptsExceeded": |
123 | | - error = AccountError(AccountErrorEnum.MAX_ATTEMPTS) |
124 | | - elif code == "SSO_InternalError": |
125 | | - if message and ( |
126 | | - "Cannot Authenticate by AccountName" in message |
127 | | - or "Cannot Authenticate by AccountId" in message |
128 | | - ): |
| 115 | + try: |
| 116 | + if response.json(): |
| 117 | + _LOGGER.debug("%s", response.json()) |
| 118 | + code = response.json().get("Code", None) |
| 119 | + message = response.json().get("Message", None) |
| 120 | + if code == "SessionIdNotFound": |
| 121 | + error = SessionError(SessionErrorEnum.NOT_FOUND) |
| 122 | + elif code == "SessionNotValid": |
| 123 | + error = SessionError(SessionErrorEnum.INVALID) |
| 124 | + elif code == "AccountPasswordInvalid": # defunct |
129 | 125 | error = AccountError(AccountErrorEnum.FAILED_AUTHENTICATION) |
130 | | - elif code == "InvalidArgument": |
131 | | - if message and "accountName" in message: |
132 | | - error = ArgumentError(ArgumentErrorEnum.USERNAME_INVALID) |
133 | | - elif message and "password" in message: |
134 | | - error = ArgumentError(ArgumentErrorEnum.PASSWORD_INVALID) |
135 | | - elif message and "UUID" in message: |
136 | | - error = ArgumentError(ArgumentErrorEnum.ACCOUNT_ID_INVALID) |
137 | | - elif code and message: |
138 | | - _LOGGER.debug("%s: %s", code, message) |
| 126 | + elif code == "SSO_AuthenticateMaxAttemptsExceeded": |
| 127 | + error = AccountError(AccountErrorEnum.MAX_ATTEMPTS) |
| 128 | + elif code == "SSO_InternalError": |
| 129 | + if message and ( |
| 130 | + "Cannot Authenticate by AccountName" in message |
| 131 | + or "Cannot Authenticate by AccountId" in message |
| 132 | + ): |
| 133 | + error = AccountError(AccountErrorEnum.FAILED_AUTHENTICATION) |
| 134 | + elif code == "InvalidArgument": |
| 135 | + if message and "accountName" in message: |
| 136 | + error = ArgumentError(ArgumentErrorEnum.USERNAME_INVALID) |
| 137 | + elif message and "password" in message: |
| 138 | + error = ArgumentError(ArgumentErrorEnum.PASSWORD_INVALID) |
| 139 | + elif message and "UUID" in message: |
| 140 | + error = ArgumentError(ArgumentErrorEnum.ACCOUNT_ID_INVALID) |
| 141 | + elif code and message: |
| 142 | + _LOGGER.debug("%s: %s", code, message) |
| 143 | + except json.JSONDecodeError: |
| 144 | + error = ServerResponseError(ServerResponseErrorEnum.INVALID_JSON) |
139 | 145 | return error |
140 | 146 |
|
141 | 147 | def _validate_region(self, region: Region) -> None: |
|
0 commit comments