Errors
Failed calls reject with a RasterApiError. Branch on its code and show the user toUserMessage(error).
Every failed call, sign-in included, rejects with a RasterApiError. React
hooks report the same error as error, or as { status: 'error', error } in
sign-in, upload, and promote states. If your credential store fails to save,
the call rejects with the store's error instead.
import { RasterApiError, toUserMessage } from '@raster-app/sdk'
try {
await raster.assets.list({ organizationId: 'acme', libraryId: 'lib-1' })
} catch (error) {
if (error instanceof RasterApiError && error.code === 'NOT_SIGNED_IN') {
// Show your sign-in screen.
} else {
alert(toUserMessage(error))
}
}RasterApiError
RasterApiError extends Error, with name set to 'RasterApiError'.
| Property | Description |
|---|---|
code | The code to branch on. |
status | The HTTP status, or 0 when there is none. |
message | A description of the failure. |
API errors keep the API's code, message, and status (see
Error codes). A failed sign-in carries the
OAuth error as its code.
Codes the SDK raises
| Code | Status | When |
|---|---|---|
ABORTED | 0 | The call's signal was aborted, or a sign-in was cancelled. |
TIMEOUT | 0 | An AbortSignal.timeout() signal on a library, asset, or search method ran out. |
NETWORK_ERROR | 0 | The request couldn't reach Raster, for example when offline or blocked by a network policy. |
NOT_SIGNED_IN | 401 | No credential is stored. No request was sent. |
SESSION_EXPIRED | 401 | The sign-in ended, for example because the user revoked it. The client signs out. |
API_KEY_REQUIRED | 400 | auth.connect got an empty API key. |
ORGANIZATION_MISMATCH | 403 | A sign-in bound to an organizationId was granted another organization or the whole account. |
access_denied | The response's | The user denied access. |
expired_token | The response's | The sign-in code expired before approval. |
HTTP_ERROR | The response's | A request failed with no error in the body. |
MALFORMED_RESPONSE | The response's | A response had an unexpected shape. |
UNKNOWN | 0, or the response's | A non-RasterApiError passed to toRasterApiError, or an API error with no code. |
toUserMessage(error)
toUserMessage(error) returns a sentence to show the user. It accepts any
thrown value.
It has its own wording for UNAUTHENTICATED, INVALID_API_KEY,
RESOURCE_NOT_FOUND, NOT_FOUND, API_KEY_NOT_AUTHORIZED_FOR_LIBRARY,
NETWORK_ERROR, TIMEOUT, NOT_SIGNED_IN, SESSION_EXPIRED,
access_denied, and expired_token. Other codes return the error's
message.
Helpers
| Function | Returns |
|---|---|
isAbortError(error) | true for ABORTED, a cancel your app asked for. See Cancelling a call. |
toRasterApiError(caught) | Any thrown value as a RasterApiError, with UNKNOWN for non-RasterApiError values. |