{"version":3,"sources":["error-handling.ts"],"sourcesContent":["import { ESCAPE_SEQUENCES, Logger } from \"./logger.ts\";\nimport DATEX_VERSION from \"../VERSION.ts\"\nimport { sendReport } from \"./error-reporting.ts\";\n\nconst errorLogger = new Logger(\"Error\");\n\n/**\n * Represents an error that the UIX developer community knows about.\n * The user might be able to fix it or ask for help.\n */\nexport class KnownError extends Error {\n\tconstructor(message: string, public solutions: string[] = [], public quickFixes: {description: string, fix: () => void}[] = []) {\n\t\tsuper(message);\n\t}\n}\n\n/**\n * Formats and prints an error in a fashion that's readable and informative\n * for the user. If `exit` is enabled, the process exits afterwards.\n * @param error The error object to handle\n * @param logger Logger to print the information to\n * @param [exit=true] Specifies whether the process should exit, defaults to `true`\n * @param [exitCode=1] Code to exit with if `exit` is set to true, defaults to `1`\n */\nexport async function handleError(error: Error|string, logger = errorLogger, exit = true, exitCode = 1) {\n\tconsole.log();\n\tif (typeof error === \"string\" || error instanceof String) {\n\t\tlogger.error(error);\n\t} else if (error instanceof KnownError) {\n\t\tlogger.error(error.message);\n\t\tif (error.solutions.length > 0) {\n\t\t\tlogger.info(`Suggested Problem Solutions:\\n${error.solutions.map(s => `- ${s}`).join(\"\\n\")}\\n`);\n\t\t}\n\t\tif (error.quickFixes) {\n\t\t\tfor (const fix of error.quickFixes) {\n\t\t\t\tconsole.log();\n\t\t\t\tconst doFix = confirm(`${fix.description}`);\n\t\t\t\tif (doFix) fix.fix();\n\t\t\t}\n\t\t}\n\t} else {\n\t\tlet details;\n\t\tif (error.stack) {\n\t\t\tconst stack = error.stack.split(\"\\n\");\n\t\t\tstack[0] = `${ESCAPE_SEQUENCES.UNDERLINE}${stack[0]}${ESCAPE_SEQUENCES.RESET_UNDERLINE}`;\n\t\t\tdetails = stack.join(\"\\n\");\n\t\t} else details = error.toString();\n\n\t\tlogger.error(`An unexpected error occured.\\n${ESCAPE_SEQUENCES.BOLD}DATEX${ESCAPE_SEQUENCES.DEFAULT} Version: ${DATEX_VERSION}\\n${ESCAPE_SEQUENCES.BOLD}Deno${ESCAPE_SEQUENCES.DEFAULT} Version: ${Deno.version.deno}${(Deno as any).uix ? ' (Deno for UIX)' : ''}\\n\\n${details}`);\n\t\tawait sendReport(\"unexpectederror\", {\n\t\t\tname: error.name,\n\t\t\tmessage: error.message,\n\t\t\tstack: error.stack\n\t\t});\n\t}\n\n\tif (exit && globalThis.Deno) Deno.exit(exitCode);\n}\n\n\nlet unhandledRejectionHandlerEnabled = false;\n\n/**\n * Enables the unhandled rejection handler, which logs unhandled promise rejections\n * and prevents the program from crashing.\n * @param customLogger a custom logger to use for logging unhandled rejections\n */\nexport function enableUnhandledRejectionHandler(customLogger = errorLogger) {\n\tif (unhandledRejectionHandlerEnabled) return;\n\tunhandledRejectionHandlerEnabled = true;\n\t// clean error presentation\n\tglobalThis.addEventListener(\"unhandledrejection\", async (event) => {\n\t\ttry {\n\t\t\tevent.preventDefault();\n\t\t\tconst error = await event.promise.catch(error => error);\n\t\t\tawait handleError(error, customLogger);\n\t\t}\n\t\tcatch (e) {\n\t\t\tconsole.error(e);\n\t\t}\n\t});\n}"],"names":["ESCAPE_SEQUENCES","Logger","DATEX_VERSION","sendReport","errorLogger","KnownError","Error","constructor","message","solutions","quickFixes","handleError","error","logger","exit","exitCode","console","log","String","length","info","map","s","join","fix","confirm","description","details","stack","split","UNDERLINE","RESET_UNDERLINE","toString","BOLD","DEFAULT","Deno","version","deno","uix","name","globalThis","unhandledRejectionHandlerEnabled","enableUnhandledRejectionHandler","customLogger","addEventListener","event","preventDefault","promise","catch","e"],"mappings":"AAAA,SAASA,gBAAgB,EAAEC,MAAM,QAAQ,cAAc;AACvD,OAAOC,mBAAmB,gBAAe;AACzC,SAASC,UAAU,QAAQ,uBAAuB;AAElD,IAAMC,cAAc,IAAIH,OAAO;AAM/B,OAAO,MAAMI,mBAAmBC;;;IAC/BC,YAAYC,OAAe,EAAE,AAAOC,YAAsB,EAAE,EAAE,AAAOC,aAAuD,EAAE,CAAE;QAC/H,KAA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