feat: generate Exception subclass per API error type#737
Draft
feat: generate Exception subclass per API error type#737
Conversation
Addresses #423. Each `ErrorType` enum member from the OpenAPI spec now has a matching `ApifyApiError` subclass (e.g. `RecordNotFoundError`) generated into `_generated_errors.py`. `ApifyApiError.__new__` dispatches to the right subclass based on the response's `error.type`, so `except ApifyApiError` keeps working while `except RecordNotFoundError` becomes possible. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #737 +/- ##
==========================================
+ Coverage 95.26% 95.91% +0.64%
==========================================
Files 45 46 +1
Lines 5115 5918 +803
==========================================
+ Hits 4873 5676 +803
Misses 242 242
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…d __init__ `__new__` used to parse `response.json()` for dispatch and `__init__` parsed it again for attribute assignment. Now `__new__` stashes the parsed `error` dict on the instance and `__init__` reads it, so each raised error parses the body once. Also trims the stale star-import comment. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Let me guys know whether you think this is worth it. Currently, I'm slightly more inclined not to do it and keep the status quo. Since the API type errors are just general strings, they can often change. |
Contributor
Arguments in favor
Arguments against
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Addresses #423. Each
ErrorTypeenum member from the OpenAPI spec now has a matchingApifyApiErrorsubclass (e.g.RecordNotFoundError,ActorNotFoundError,InsufficientPermissionsError, ...) auto-generated intosrc/apify_client/_generated_errors.pyby the existing model-postprocess step.ApifyApiError.__new__dispatches to the right subclass based on the response'serror.type. Unknown types and unparsable bodies fall through to the baseApifyApiError. Direct subclass instantiation bypasses dispatch.except ApifyApiErrorhandlers keep working because every generated class inherits fromApifyApiError.Naming
RECORD_NOT_FOUND→RecordNotFoundErrorBILLING_SYSTEM_ERROR→BillingSystemError(trailing_ERRORstripped)FIELD_3D_SECURE_AUTH_FAILED→Field3DSecureAuthFailedError(digit parts preserved)NOT_IMPLEMENTED→ApiNotImplementedError(Apiprefix to avoid shadowing the builtin)SCHEMA_VALIDATION/SCHEMA_VALIDATION_ERROR→SchemaValidationError/SchemaValidationErrorError(collision fallback)Usage
Migration note
Minor behavior change for 3.0 scope: constructing
ApifyApiError(response, ...)directly now returns an instance of the matching subclass rather than alwaysApifyApiErroritself.except ApifyApiErroris unaffected; tests constructing the base class and assertingtype(exc) is ApifyApiErrorwould need updates.