UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines • 3.91 kB
{"version":3,"file":"error-message.cjs","sources":["../../src/error-message.ts"],"sourcesContent":["// Production error messages. Not part of the public API: `index.ts`\n// re-exports `errors.ts`, so these helpers live in their own module.\n\n// Bundlers inline `process.env.NODE_ENV`. The pure probe supports unbundled\n// browsers; the literal comparison beside each use lets a production bundler\n// erase the full message text.\n/* @__NO_SIDE_EFFECTS__ */\nexport function devBuild(): boolean {\n try {\n return process.env.NODE_ENV !== `production`\n } catch {\n return false\n }\n}\n\n/**\n * One value as it appears in a production message, or `undefined` when it is\n * not shown. Only primitives reach `JSON.stringify`, so no `toJSON()` runs: a\n * plain object, at any depth, never contributes its contents.\n */\nfunction show(value: unknown, inArray: boolean): string | undefined {\n if (Array.isArray(value))\n return `[${value.map((item) => show(item, true) ?? `\"[object]\"`).join(`,`)}]`\n if (value instanceof Error)\n return inArray ? undefined : JSON.stringify(value.message)\n if (\n typeof value === `symbol` ||\n typeof value === `bigint` ||\n (inArray &&\n (value === undefined ||\n (typeof value === `number` && !Number.isFinite(value))))\n )\n return JSON.stringify(String(value))\n if (\n value === undefined ||\n (typeof value === `number` && !Number.isFinite(value))\n )\n return String(value)\n return value === null || typeof value !== `object`\n ? JSON.stringify(value)\n : undefined\n}\n\n/**\n * A production message: the code, the inputs it can show as JSON, and its docs\n * anchor. Symbols and bigints show as strings; `undefined`, `NaN`, and the\n * infinities show as themselves. Objects other than arrays and errors are not\n * shown.\n */\nexport function codedMessage(\n code: number,\n values: Record<string, unknown> = {},\n kind: `error` | `warning` = `error`,\n): string {\n const shown = Object.entries(values).flatMap(([name, value]) => {\n try {\n const text = show(value, false)\n return text === undefined ? [] : [`${name}=${text}`]\n } catch {\n return []\n }\n })\n // JSON leaves U+2028 and U+2029 unescaped; both end a line.\n const pairs = shown\n .join(`, `)\n .replace(\n /[\\u2028\\u2029]/g,\n (separator) => `\\\\u${separator.charCodeAt(0).toString(16)}`,\n )\n return `TanStack DB ${kind} ${code}${pairs && ` (${pairs})`}: https://tanstack.com/db/latest/docs/errors#${kind}-${code}`\n}\n\n/** The production form of a `console.warn` message: a warning, not an error. */\nexport function codedWarning(\n code: number,\n values?: Record<string, unknown>,\n): string {\n return codedMessage(code, values, `warning`)\n}\n"],"names":[],"mappings":";;;AAOO,SAAS,WAAoB;AAClC,MAAI;AACF,WAAO,QAAQ,IAAI,aAAa;AAAA,EAClC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAOA,SAAS,KAAK,OAAgB,SAAsC;AAClE,MAAI,MAAM,QAAQ,KAAK;AACrB,WAAO,IAAI,MAAM,IAAI,CAAC,SAAS,KAAK,MAAM,IAAI,KAAK,YAAY,EAAE,KAAK,GAAG,CAAC;AAC5E,MAAI,iBAAiB;AACnB,WAAO,UAAU,SAAY,KAAK,UAAU,MAAM,OAAO;AAC3D,MACE,OAAO,UAAU,YACjB,OAAO,UAAU,YAChB,YACE,UAAU,UACR,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK;AAExD,WAAO,KAAK,UAAU,OAAO,KAAK,CAAC;AACrC,MACE,UAAU,UACT,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK;AAEpD,WAAO,OAAO,KAAK;AACrB,SAAO,UAAU,QAAQ,OAAO,UAAU,WACtC,KAAK,UAAU,KAAK,IACpB;AACN;AAQO,SAAS,aACd,MACA,SAAkC,CAAA,GAClC,OAA4B,SACpB;AACR,QAAM,QAAQ,OAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC,MAAM,KAAK,MAAM;AAC9D,QAAI;AACF,YAAM,OAAO,KAAK,OAAO,KAAK;AAC9B,aAAO,SAAS,SAAY,KAAK,CAAC,GAAG,IAAI,IAAI,IAAI,EAAE;AAAA,IACrD,QAAQ;AACN,aAAO,CAAA;AAAA,IACT;AAAA,EACF,CAAC;AAED,QAAM,QAAQ,MACX,KAAK,IAAI,EACT;AAAA,IACC;AAAA,IACA,CAAC,cAAc,MAAM,UAAU,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC;AAAA,EAAA;AAE7D,SAAO,eAAe,IAAI,IAAI,IAAI,GAAG,SAAS,KAAK,KAAK,GAAG,gDAAgD,IAAI,IAAI,IAAI;AACzH;AAGO,SAAS,aACd,MACA,QACQ;AACR,SAAO,aAAa,MAAM,QAAQ,SAAS;AAC7C;;;;"}