Exceptions¶
Custom exceptions for the Affinity API client.
All exceptions inherit from AffinityError for easy catching of all library errors.
AffinityError
¶
Bases: Exception
Base exception for all Affinity API errors.
Source code in affinity/exceptions.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
AuthenticationError
¶
Bases: AffinityError
401 Unauthorized - Invalid or missing API key.
Your API key is invalid or was not provided.
Source code in affinity/exceptions.py
63 64 65 66 67 68 69 70 | |
AuthorizationError
¶
Bases: AffinityError
403 Forbidden - Insufficient permissions.
You don't have permission to access this resource or perform this action. This can happen with: - Private lists you don't have access to - Admin-only actions - Resource-level permission restrictions
Source code in affinity/exceptions.py
73 74 75 76 77 78 79 80 81 82 83 84 | |
BetaEndpointDisabledError
¶
Bases: UnsupportedOperationError
Attempted to call a beta endpoint without opt-in.
Source code in affinity/exceptions.py
368 369 370 371 | |
CompanyNotFoundError
¶
Bases: EntityNotFoundError
Company with the specified ID was not found.
Source code in affinity/exceptions.py
339 340 341 342 343 | |
ConfigurationError
¶
Bases: AffinityError
Configuration error - missing or invalid client configuration.
Check that you've provided: - A valid API key - Correct base URLs (if customizing)
Source code in affinity/exceptions.py
190 191 192 193 194 195 196 197 198 199 | |
ConflictError
¶
Bases: AffinityError
409 Conflict - Resource conflict.
The request conflicts with the current state of the resource. For example: - Trying to create a person with an email that already exists - Concurrent modification conflicts
Source code in affinity/exceptions.py
161 162 163 164 165 166 167 168 169 170 171 | |
DeprecationWarning
¶
Bases: AffinityError
Feature is deprecated and may be removed.
Source code in affinity/exceptions.py
419 420 421 422 423 424 | |
EntityNotFoundError
¶
Bases: NotFoundError
Specific entity not found.
Provides type-safe context about which entity type was not found.
Source code in affinity/exceptions.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
FieldNotFoundError
¶
Bases: NotFoundError
Field with the specified ID was not found.
Source code in affinity/exceptions.py
320 321 322 323 | |
ListNotFoundError
¶
Bases: NotFoundError
List with the specified ID was not found.
Source code in affinity/exceptions.py
326 327 328 329 | |
NetworkError
¶
Bases: AffinityError
Network-level error.
Failed to connect to the Affinity API. Check your internet connection and firewall settings.
Source code in affinity/exceptions.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
NotFoundError
¶
Bases: AffinityError
404 Not Found - Resource doesn't exist.
The requested resource (person, company, list, etc.) was not found. This could mean: - The ID is invalid - The resource was deleted - You don't have access to view it
Source code in affinity/exceptions.py
87 88 89 90 91 92 93 94 95 96 97 98 | |
OpportunityNotFoundError
¶
Bases: EntityNotFoundError
Opportunity with the specified ID was not found.
Source code in affinity/exceptions.py
346 347 348 349 350 | |
PersonNotFoundError
¶
Bases: EntityNotFoundError
Person with the specified ID was not found.
Source code in affinity/exceptions.py
332 333 334 335 336 | |
PolicyError
¶
Bases: AffinityError
Raised when a client policy blocks an attempted operation.
Source code in affinity/exceptions.py
239 240 241 242 | |
RateLimitError
¶
Bases: AffinityError
429 Too Many Requests - Rate limit exceeded.
You've exceeded the API rate limit. Wait before retrying. Check the response headers for rate limit info: - X-Ratelimit-Limit-User-Reset: Seconds until per-minute limit resets - X-Ratelimit-Limit-Org-Reset: Seconds until monthly limit resets
Source code in affinity/exceptions.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
ServerError
¶
Bases: AffinityError
500/503 Internal Server Error - Server-side problem.
Something went wrong on Affinity's servers. Try again later, and contact support if the problem persists.
Source code in affinity/exceptions.py
174 175 176 177 178 179 180 181 182 | |
TimeoutError
¶
Bases: AffinityError
Request timeout.
The request took too long to complete. This could be due to: - Network issues - Large data sets - Server overload
Source code in affinity/exceptions.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
TooManyResultsError
¶
Bases: AffinityError
Raised when .all() exceeds the limit.
The default limit is 100,000 items (approximately 100MB for typical Person objects). This protects against OOM errors when paginating large datasets.
To resolve:
- Use .pages() for streaming iteration (memory-efficient)
- Add filters to reduce result size
- Pass limit=None to .all() if you're certain you need all results
- Pass a custom limit=500_000 if you need more than the default
Source code in affinity/exceptions.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
UnsafeUrlError
¶
Bases: AffinityError
SDK blocked following a server-provided URL.
Raised when SafeFollowUrl policy rejects a URL (scheme/host/userinfo/redirect).
Source code in affinity/exceptions.py
281 282 283 284 285 286 287 288 289 290 291 292 293 | |
UnsupportedOperationError
¶
Bases: AffinityError
Operation not supported by the current API version.
Some operations are only available in V1 or V2.
Source code in affinity/exceptions.py
358 359 360 361 362 363 364 365 | |
ValidationError
¶
Bases: AffinityError
400/422 Bad Request/Unprocessable Entity - Invalid request data.
The request data is malformed or logically invalid. Check the error message for details about what's wrong.
Source code in affinity/exceptions.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
VersionCompatibilityError
¶
Bases: AffinityError
Response shape mismatch suggests API version incompatibility.
TR-015: Raised when the SDK detects response-shape mismatches that appear version-related. This typically means the API key's configured v2 Default API Version differs from what the SDK expects.
Guidance: 1. Check your API key's v2 Default API Version in the Affinity dashboard 2. Ensure it matches the expected_v2_version configured in the SDK 3. See: https://developer.affinity.co/#section/Getting-Started/Versioning
Source code in affinity/exceptions.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
WebhookInvalidJsonError
¶
Bases: WebhookParseError
Raised when a webhook payload cannot be decoded as JSON.
Source code in affinity/exceptions.py
570 571 572 573 | |
WebhookInvalidPayloadError
¶
Bases: WebhookParseError
Raised when a decoded webhook payload is not in the expected envelope shape.
Source code in affinity/exceptions.py
576 577 578 579 | |
WebhookInvalidSentAtError
¶
Bases: WebhookParseError
Raised when a webhook sent_at field is missing or invalid.
Source code in affinity/exceptions.py
590 591 592 593 | |
WebhookMissingKeyError
¶
Bases: WebhookParseError
Raised when a webhook payload is missing a required key.
Source code in affinity/exceptions.py
582 583 584 585 586 587 | |
WebhookParseError
¶
Bases: AffinityError
Base error for inbound webhook parsing/validation failures.
Source code in affinity/exceptions.py
564 565 566 567 | |
WriteNotAllowedError
¶
Bases: PolicyError
Raised when a write operation is attempted while the write policy denies writes.
Source code in affinity/exceptions.py
245 246 247 248 249 250 251 | |
error_from_response(status_code: int, response_body: Any, *, retry_after: int | None = None, diagnostics: ErrorDiagnostics | None = None) -> AffinityError
¶
Create the appropriate exception from an API error response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
HTTP status code |
required |
response_body
|
Any
|
Parsed response body (usually dict with 'errors') |
required |
retry_after
|
int | None
|
Retry-After header value for rate limits |
None
|
Returns:
| Type | Description |
|---|---|
AffinityError
|
Appropriate AffinityError subclass |
Source code in affinity/exceptions.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |