{"version":3,"sources":["errors.tsx"],"sourcesContent":["import { logger } from \"datex-core-legacy/datex_all.ts\";\nimport { app } from \"../app/app.ts\";\nimport { unsafeHTML } from \"../../uix-short.ts\";\nimport { HTTPError } from \"./http-error.ts\";\nimport { HTTPStatus } from \"./http-status.ts\";\nimport { bindToOrigin } from \"../app/datex-over-http.ts\";\nimport { client_type } from \"datex-core-legacy/utils/constants.ts\";\nimport { domUtils } from \"../app/dom-context.ts\";\n\nfunction createErrorMessageHTML(title: string, message?: string|Element, attachment?: Element, statusCode?: number, displayLocation?: string) {\n\n\treturn <div style=\"all: revert; font-size: 1rem; position: absolute; left: 0; top: 0; width:100%; height:100vh; background-color:#282828; display:flex; justify-content:center; align-items:center\">\n\t\t\n\t\t<div style=\"display:grid; padding:20px; margin: 15px; color: #ef7b7b; background-color:#4e3635; border-radius:10px; width: fit-content;max-width: 100%; max-height: 100%; overflow: auto;\">\n\t\t\t<div style=\"display:flex\">\n\t\t\t\t<svg style=\"margin-top: 2px;\" xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-x-circle-fill\" viewBox=\"0 0 16 16\">\n\t\t\t\t\t<path d=\"M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM5.354 4.646a.5.5 0 1 0-.708.708L7.293 8l-2.647 2.646a.5.5 0 0 0 .708.708L8 8.707l2.646 2.647a.5.5 0 0 0 .708-.708L8.707 8l2.647-2.646a.5.5 0 0 0-.708-.708L8 7.293 5.354 4.646z\"/>\n\t\t\t\t</svg>\n\t\t\t\t<pre style=\"all: revert;margin: 0; margin-left: 7px; text-wrap: wrap;\">\n\t\t\t\t\t<div style=\"display:flex;justify-content: space-between;margin-bottom: 5px\">\n\t\t\t\t\t\t<b style=\"color:#ef7b7b; margin-bottom: 7px; display: contents;\">{statusCode?statusCode + ' ' : ''}{title}</b>\n\t\t\t\t\t\t{displayLocation ? <div style=\"background: #776565; color: #4e3635; border-radius: 6px; display: flex; justify-content: center; align-items: center; padding: 0px 8px;margin-left: 15px;\">{displayLocation}</div> : undefined}\n\t\t\t\t\t</div>\n\t\t\t\t\t{message}\n\t\t\t\t</pre>\n\t\t\t</div>\n\t\t\t{attachment}\n\t\t</div>\n\t</div> as HTMLDivElement\n}\n\nconst matchURL = /\\b((https?|file):\\/\\/[^\\s]+(\\:\\d+)?(\\:\\d+)?\\b)/g;\n\nexport function createErrorHTML(title: string, error?: Error|number|HTTPStatus<number,string>|string) {\n\tconst isExplicitStatusCode = error instanceof HTTPError || error instanceof HTTPStatus || typeof error == \"number\";\n\tconst isBackend = client_type==\"deno\";\n\tconst displayLocation = isBackend ? \"Backend\" : \"Frontend\"\n\n\tlet errorMessage:string|Element|undefined = error instanceof HTTPStatus ? error.content?.toString() : (typeof error == \"string\" ? error : undefined);\n\tlet attachment = undefined\n\n\terror ??= HTTPStatus.INTERNAL_SERVER_ERROR;\n\n\t// just use error message as title if HTTPError\n\tif (error instanceof HTTPError) {\n\t\ttitle = error.message\n\t}\n\n\t// extract status code\n\tconst statusCode = error instanceof HTTPError ? \n\t\terror.statusCode : (\n\t\t\terror instanceof HTTPStatus ?\n\t\t\terror.code : (\n\t\t\t\t(error instanceof Error || typeof error == \"string\") ? \n\t\t\t\t\tHTTPStatus.INTERNAL_SERVER_ERROR.code : \n\t\t\t\t\terror\n\t\t\t)\n\t\t);\n\n\tif (error instanceof Error) {\n\t\tconst stackMessage = error.stack ?? error.message ?? \"\";\n\t\tconst lastURL = stackMessage.match(matchURL)?.[0];\n\t\tconst stack = domUtils.escapeHtml(stackMessage).replace(matchURL, `<a target=\"_blank\" onclick=\"event.preventDefault();document.getElementById('source-iframe').src=this.href\" style=\"color:#a4c1f3\" href=\"$&${isBackend ? ':source': ''}\">$&</a>`)\n\t\terrorMessage = unsafeHTML(`<div>${stack}</div>`);\n\t\tattachment = lastURL ? \n\t\t\t<iframe id=\"source-iframe\" onload:frontend={e => e.target.style.display=\"block\"} style=\"height: 250px;margin-top: 30px;width: 100%;border:none; border-radius:8px\" src={lastURL+(isBackend ? ':source': '')}></iframe> :\n\t\t\tundefined;\n\t}\n\n\n\tconst isProduction = app.stage !== \"dev\";\n\n\treturn [\n\t\tstatusCode,\n\t\tisProduction ? \n\t\t\tproductionErrorGenerator(\n\t\t\t\ttitle,\n\t\t\t\terrorMessage,\n\t\t\t\tattachment,\n\t\t\t\tisExplicitStatusCode ? statusCode : undefined,\n\t\t\t\tdisplayLocation\n\t\t\t) :\n\t\t\tcreateErrorMessageHTML(\n\t\t\t\ttitle,\n\t\t\t\terrorMessage,\n\t\t\t\tattachment,\n\t\t\t\tisExplicitStatusCode ? statusCode : undefined,\n\t\t\t\tdisplayLocation\n\t\t\t)\n\t] as const\n}\n\n/**\n * Display an error message in a browser window - display a modified error with different (less) information in non-dev stage\n * @param title \n * @param error\n * @returns \n */\nexport function displayError(title: string, error?: Error|number|HTTPStatus<number,string>|string) {\n\t// display if browser\n\tif (client_type === \"browser\") {\n\t\tconst [_status, html] = createErrorHTML(title, error);\n\t\tdocument.body.innerHTML = \"\";\n\t\tdocument.body.append(html);\n\t}\n\tlogger.error(title + '\\n' + (error ? error.toString() : ''))\n}\n\ntype errorGenerator = (title: string, message?: string|Element, attachment?: Element, statusCode?: number, displayLocation?: string) => Element;\n\n// default impl for prod mode generator: always omit message + attachment in prod mode\nlet productionErrorGenerator:errorGenerator = (title, message, attachment, statusCode, displayLocation) => {\n\treturn createErrorMessageHTML(title, undefined, undefined, statusCode)\n};\n\n\n/**\n * Define a generator function that generates errors for in production mode\n * @param generator callback that gets error information as input and returns a new Element\n */\nexport function setProductionErrorGenerator(generator: errorGenerator) {\n\tproductionErrorGenerator = generator;\n}\n\n"],"names":["logger","app","unsafeHTML","HTTPError","HTTPStatus","client_type","domUtils","createErrorMessageHTML","title","message","attachment","statusCode","displayLocation","div","style","svg","xmlns","width","height","fill","class","viewBox","path","d","pre","b","undefined","matchURL","createErrorHTML","error","isExplicitStatusCode","isBackend","errorMessage","content","toString","INTERNAL_SERVER_ERROR","code","Error","stackMessage","stack","lastURL","match","escapeHtml","replace","iframe","id","e","target","display","src","stage","productionErrorGenerator","displayError","_status","html","document","body","innerHTML","append","setProductionErrorGenerator","generator"],"mappings":";AAAA,SAASA,MAAM,QAAQ,iCAAiC;AACxD,SAASC,GAAG,QAAQ,gBAAgB;AACpC,SAASC,UAAU,QAAQ,qBAAqB;AAChD,SAASC,SAAS,QAAQ,kBAAkB;AAC5C,SAASC,UAAU,QAAQ,mBAAmB;AAE9C,SAASC,WAAW,QAAQ,uCAAuC;AACnE,SAASC,QAAQ,QAAQ,wBAAwB;AAEjD,SAASC,uBAAuBC,KAAa,EAAEC,OAAwB,EAAEC,UAAoB,EAAEC,UAAmB,EAAEC,eAAwB;IAE3I,qBAAO,KAACC;QAAIC,OAAM;kBAEjB,cAAA,MAACD;YAAIC,OAAM;;8BACV,MAACD;oBAAIC,OAAM;;sCACV,KAACC;4BAAID,OAAM;4BAAmBE,OAAM;4BAA6BC,OAAM;4BAAKC,QAAO;4BAAKC,MAAK;4BAAeC,OAAM;4BAAsBC,SAAQ;sCAC/I,cAAA,KAACC;gCAAKC,GAAE;;;sCAET,MAACC;4BAAIV,OAAM;;8CACV,MAACD;oCAAIC,OAAM;;sDACV,MAACW;4CAAEX,OAAM;;gDAAyDH,aAAWA,aAAa,MAAM;gDAAIH;;;wCACnGI,gCAAkB,KAACC;4CAAIC,OAAM;sDAA6JF;6CAAyBc,KAAAA;;;gCAEpNjB;;;;;gBAGFC;;;;AAGJ;AAEA,IAAMiB,WAAW;AAEjB,OAAO,SAASC,gBAAgBpB,KAAa,EAAEqB,KAAqD;QAM/FnB;IALJ,IAAMoB,uBAAuBD,iBAAiB1B,aAAa0B,iBAAiBzB,cAAc,AAAgB,YAAhB,OAAOyB,OAC3FE,YAAY1B,AAAa,UAAbA,aACZO,kBAAkBmB,YAAY,YAAY,YAE5CC,eAAwCH,iBAAiBzB,aAAayB,MAAMI,OAAO,EAAEC,aAAc,AAAgB,Y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