Handle and display error messages from API responses. Args: response: The response from the Plus API.
(self, response: httpx.Response)
| 38 | raise AuthenticationRequiredError from None |
| 39 | |
| 40 | def _validate_response(self, response: httpx.Response) -> None: |
| 41 | """Handle and display error messages from API responses. |
| 42 | |
| 43 | Args: |
| 44 | response: The response from the Plus API. |
| 45 | """ |
| 46 | try: |
| 47 | json_response = response.json() |
| 48 | except (json.JSONDecodeError, ValueError): |
| 49 | console.print( |
| 50 | "Failed to parse response from Enterprise API failed. Details:", |
| 51 | style="bold red", |
| 52 | ) |
| 53 | console.print(f"Status Code: {response.status_code}") |
| 54 | console.print( |
| 55 | f"Response:\n{response.content.decode('utf-8', errors='replace')}" |
| 56 | ) |
| 57 | raise SystemExit from None |
| 58 | |
| 59 | if response.status_code == 422: |
| 60 | console.print( |
| 61 | "Failed to complete operation. Please fix the following errors:", |
| 62 | style="bold red", |
| 63 | ) |
| 64 | for field, messages in json_response.items(): |
| 65 | for message in messages: |
| 66 | console.print( |
| 67 | f"* [bold red]{field.capitalize()}[/bold red] {message}" |
| 68 | ) |
| 69 | raise SystemExit |
| 70 | |
| 71 | if not response.is_success: |
| 72 | console.print( |
| 73 | "Request to Enterprise API failed. Details:", style="bold red" |
| 74 | ) |
| 75 | details = ( |
| 76 | json_response.get("error") |
| 77 | or json_response.get("message") |
| 78 | or response.content.decode("utf-8", errors="replace") |
| 79 | ) |
| 80 | console.print(f"{details}") |
| 81 | raise SystemExit |