Throw ApiError with a code and a message:
TypeScript
import { ApiError } from 'arkapi/server'
export function setNickname(name: string): string {
if (isTaken(name)) throw new ApiError('CONFLICT', 'That nickname is taken')
return save(name)
}
The code decides the HTTP status:
| Code | Status |
|---|---|
BAD_REQUEST | 400 |
UNAUTHORIZED | 401 |
FORBIDDEN | 403 |
NOT_FOUND | 404 |
CONFLICT | 409 |
RATE_LIMITED | 429 |
INTERNAL | 500 |
Any other thrown error becomes INTERNAL, and its stack goes to the logs.
On the device, AsyncValue and LiveValue expose the same code as errorCode. Check the code, not the message text:
TypeScript
if (this.saved.errorCode === 'CONFLICT') {
// ask for another nickname
}
NETWORK means the device could not reach the backend. A successful result clears error and errorCode.