Throw and handle errors
error(code, message) throws an error JavaScript receives with its code. Errors from JavaScript and the SDK arrive the same way.
import { error, errorCode } from "lucent:core";
export type Config = { name: string; retries: number };
export function parseConfig(text: string): Config { if (text.trim().length === 0) throw error("E_EMPTY", "The config is empty"); return JSON.parse(text) as Config;}
export function retriesOr(text: string, fallback: number): number { try { return parseConfig(text).retries; } catch (e) { if (e instanceof Error && errorCode(e) === "E_EMPTY") return fallback; throw e; }}try { parseConfig("");} catch (e) { if ((e as { code?: string }).code === "E_EMPTY") showDefaults(); else throw e;}error(code, message)fromlucent:coremakes anErrorwith acode. Test the code, not the message.- JavaScript receives an
Error,TypeErrororRangeError, with the samename,messageandcode. Its stack starts at the line that made the error. - Only
Errorvalues can be thrown (LUCENT1006). Subclasses reach JavaScript as plain errors, with their name, message and code. - An async export's error rejects its promise. A JS callback's error, or a rejected promise you
await, is thrown in Lucent, with itscode. - SDK errors arrive the same way: a Java exception's
codeis its class, and anNSError's isdomain:code.
An error that nothing catches in an SDK callback is logged: [lucent] uncaught exception in …, in the unified log on iOS and logcat on Android.