Error Handling
Both SDKs map every non-2xx HTTP response to the same four categories, via the identical status-code rules described in Java Exceptions and Node.js Exceptions.
Category → recommended action
| Category | Status codes | Recommended action |
|---|---|---|
| Auth | 401, 403 | Don't retry as-is. Check the key/secret you configured — see Authentication. |
| Validation | Other 4xx | Don't retry as-is. Fix the request based on the response body. |
| Rate limit | 429 | Retry after a delay, ideally with exponential backoff. |
| Server | 5xx | Safe to retry with backoff — the failure is likely transient. |
| Network / local | N/A (status code 0) | Retry with backoff; this covers connection failures and local serialization errors. |
Example: retry with backoff
- Java
- Node.js
int maxAttempts = 3;
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
client.notifications().send(request);
break;
} catch (NotificationHubException.NotificationHubRateLimitException
| NotificationHubException.NotificationHubServerException e) {
if (attempt == maxAttempts) throw e;
Thread.sleep((long) Math.pow(2, attempt) * 1000);
}
}
import { NotificationHubRateLimitError, NotificationHubServerError } from '@sumitshresht/notificationhub-sdk';
const maxAttempts = 3;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await client.notifications().send(request);
break;
} catch (error) {
const retryable = error instanceof NotificationHubRateLimitError || error instanceof NotificationHubServerError;
if (!retryable || attempt === maxAttempts) throw error;
await new Promise((resolve) => setTimeout(resolve, 2 ** attempt * 1000));
}
}
Idempotency and retries
When retrying a send, pass the same idempotency key on every attempt (see sendWithIdempotency) so a retry after a network timeout can't result in a duplicate notification if the original request actually succeeded server-side.