Error handling
Catch ApiError and branch on stable error codes.
Non-2xx responses throw an ApiError with the parsed
error envelope:
import { ApiError } from '@jetronticket/api'
try {
await client.getEvent('missing-event')
} catch (err) {
if (err instanceof ApiError) {
err.status // 404: the HTTP status code
err.errorCode // stable machine-readable identifier
err.message // human-readable description
err.body // the raw parsed error body
err.response // the raw fetch Response
}
}Branch on errorCode (and status), never on message, which can change.
A typical checkout guard
import { ApiError } from '@jetronticket/api'
async function placeOrder() {
try {
return await client.createOrder('my-event', body, { deviceId })
} catch (err) {
if (!(err instanceof ApiError)) throw err // network failure, abort, etc.
switch (err.status) {
case 400:
// Validation failed or the hold expired: re-reserve and retry
return promptToReserveAgain()
case 409:
// An order for this reservation is already being processed
return showPaymentInProgress()
default:
return showGenericError(err.message)
}
}
}Non-HTTP failures
Only HTTP error responses become ApiError. Network failures and aborted
requests reject with the underlying fetch error (e.g. TypeError,
DOMException for aborts), so keep the instanceof check in place:
const controller = new AbortController()
try {
await client.listEvents({ signal: controller.signal })
} catch (err) {
if (err instanceof ApiError) handleApiError(err)
else if (err instanceof DOMException && err.name === 'AbortError') {
// request was cancelled
} else throw err
}
