-
Notifications
You must be signed in to change notification settings - Fork 18
feat(client): Add nemoclient error hierarchy and fix streaming #539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
matthewgrossman
merged 4 commits into
main
from
mgrossman/GENERIC1-aircore-827-migrate-first-plugin-to-nemoclient-typed-http-client-files-
Jul 1, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/nemo_platform_plugin/src/nemo_platform_plugin/client/errors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """HTTP error hierarchy for the NemoClient. | ||
|
|
||
| Provides :class:`NemoHTTPError` and status-code-specific subclasses | ||
| (e.g. :class:`NotFoundError`, :class:`ConflictError`) raised by | ||
| :func:`raise_for_status` on non-2xx responses. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import httpx | ||
|
|
||
|
|
||
| class NemoHTTPError(Exception): | ||
| """Raised on non-2xx HTTP responses. | ||
|
|
||
| Attributes: | ||
| http_response: The raw httpx response. | ||
| status_code: The HTTP status code. | ||
| detail: A human-readable error message extracted from the response | ||
| body (``{"detail": "..."}`` convention used by FastAPI / NeMo | ||
| Platform), or the raw response text as a fallback. | ||
| body: The parsed JSON response body, or None. | ||
| """ | ||
|
|
||
| def __init__(self, http_response: httpx.Response) -> None: | ||
| self.http_response = http_response | ||
| self.status_code = http_response.status_code | ||
| self.detail = self._extract_detail(http_response) | ||
| self.body = self._extract_body(http_response) | ||
| super().__init__(f"HTTP {self.status_code}: {self.detail}") | ||
|
|
||
| @staticmethod | ||
| def _extract_body(resp: httpx.Response) -> object | None: | ||
| try: | ||
| return resp.json() | ||
| except Exception: | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def _extract_detail(resp: httpx.Response) -> str: | ||
| try: | ||
| body = resp.json() | ||
| if isinstance(body, dict) and isinstance(body.get("detail"), str): | ||
| return body["detail"] | ||
| except Exception: | ||
| pass | ||
| try: | ||
| return resp.text | ||
| except Exception: | ||
| return resp.reason_phrase or f"HTTP {resp.status_code}" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Status-code-specific errors | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class BadRequestError(NemoHTTPError): | ||
| """HTTP 400""" | ||
|
|
||
|
|
||
| class AuthenticationError(NemoHTTPError): | ||
| """HTTP 401""" | ||
|
|
||
|
|
||
| class PermissionDeniedError(NemoHTTPError): | ||
| """HTTP 403""" | ||
|
|
||
|
|
||
| class NotFoundError(NemoHTTPError): | ||
| """HTTP 404""" | ||
|
|
||
|
|
||
| class ConflictError(NemoHTTPError): | ||
| """HTTP 409""" | ||
|
|
||
|
|
||
| class UnprocessableEntityError(NemoHTTPError): | ||
| """HTTP 422""" | ||
|
|
||
|
|
||
| class RateLimitError(NemoHTTPError): | ||
| """HTTP 429""" | ||
|
|
||
|
|
||
| class InternalServerError(NemoHTTPError): | ||
| """HTTP 500+""" | ||
|
|
||
|
|
||
| _STATUS_CODE_TO_ERROR: dict[int, type[NemoHTTPError]] = { | ||
| 400: BadRequestError, | ||
| 401: AuthenticationError, | ||
| 403: PermissionDeniedError, | ||
| 404: NotFoundError, | ||
| 409: ConflictError, | ||
| 422: UnprocessableEntityError, | ||
| 429: RateLimitError, | ||
| 500: InternalServerError, | ||
| } | ||
|
|
||
|
|
||
| def raise_for_status(http_response: httpx.Response) -> None: | ||
| """Raise status-code-specific NemoHTTPError subclass for non-2xx responses.""" | ||
| if 200 <= http_response.status_code < 300: | ||
| return | ||
| error_cls = _STATUS_CODE_TO_ERROR.get(http_response.status_code, NemoHTTPError) | ||
| if error_cls is NemoHTTPError and http_response.status_code >= 500: | ||
| error_cls = InternalServerError | ||
| raise error_cls(http_response) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.