@tldraw/utils
Version:
tldraw infinite canvas SDK (private utilities).
8 lines (7 loc) • 2.7 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/lib/error.ts"],
"sourcesContent": ["/** @public */\nexport interface ErrorAnnotations {\n\ttags: Record<string, number | string | boolean | bigint | symbol | null | undefined>\n\textras: Record<string, unknown>\n}\n\nconst annotationsByError = new WeakMap<object, ErrorAnnotations>()\n\n/**\n * Annotate an error with tags and additional data. Annotations won't overwrite existing ones.\n * Retrieve them with `getErrorAnnotations`.\n *\n * @param error - The error object to annotate\n * @param annotations - Partial annotations to add (tags and/or extras)\n * @returns void\n * @example\n * ```ts\n * const error = new Error('Something went wrong')\n * annotateError(error, {\n * tags: { userId: '123', operation: 'save' },\n * extras: { timestamp: Date.now() }\n * })\n * ```\n *\n * @internal\n */\nexport function annotateError(error: unknown, annotations: Partial<ErrorAnnotations>) {\n\tif (typeof error !== 'object' || error === null) return\n\n\tlet currentAnnotations = annotationsByError.get(error)\n\tif (!currentAnnotations) {\n\t\tcurrentAnnotations = { tags: {}, extras: {} }\n\t\tannotationsByError.set(error, currentAnnotations)\n\t}\n\n\tif (annotations.tags) {\n\t\tcurrentAnnotations.tags = {\n\t\t\t...currentAnnotations.tags,\n\t\t\t...annotations.tags,\n\t\t}\n\t}\n\tif (annotations.extras) {\n\t\tcurrentAnnotations.extras = {\n\t\t\t...currentAnnotations.extras,\n\t\t\t...annotations.extras,\n\t\t}\n\t}\n}\n\n/**\n * Retrieve annotations that have been added to an error object.\n *\n * @param error - The error object to get annotations from\n * @returns The error annotations (tags and extras) or empty objects if none exist\n * @example\n * ```ts\n * const error = new Error('Something went wrong')\n * annotateError(error, { tags: { userId: '123' } })\n * const annotations = getErrorAnnotations(error)\n * console.log(annotations.tags.userId) // '123'\n * ```\n *\n * @internal\n */\nexport function getErrorAnnotations(error: Error): ErrorAnnotations {\n\treturn annotationsByError.get(error) ?? { tags: {}, extras: {} }\n}\n"],
"mappings": "AAMA,MAAM,qBAAqB,oBAAI,QAAkC;AAoB1D,SAAS,cAAc,OAAgB,aAAwC;AACrF,MAAI,OAAO,UAAU,YAAY,UAAU,KAAM;AAEjD,MAAI,qBAAqB,mBAAmB,IAAI,KAAK;AACrD,MAAI,CAAC,oBAAoB;AACxB,yBAAqB,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,EAAE;AAC5C,uBAAmB,IAAI,OAAO,kBAAkB;AAAA,EACjD;AAEA,MAAI,YAAY,MAAM;AACrB,uBAAmB,OAAO;AAAA,MACzB,GAAG,mBAAmB;AAAA,MACtB,GAAG,YAAY;AAAA,IAChB;AAAA,EACD;AACA,MAAI,YAAY,QAAQ;AACvB,uBAAmB,SAAS;AAAA,MAC3B,GAAG,mBAAmB;AAAA,MACtB,GAAG,YAAY;AAAA,IAChB;AAAA,EACD;AACD;AAiBO,SAAS,oBAAoB,OAAgC;AACnE,SAAO,mBAAmB,IAAI,KAAK,KAAK,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,EAAE;AAChE;",
"names": []
}