From aeb26aaccb6d7a88cbeda4229ac0fe867955c26e Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Thu, 17 Mar 2022 23:23:10 -0400 Subject: [PATCH] try catch --- HttpTrigger/index.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/HttpTrigger/index.ts b/HttpTrigger/index.ts index db515d9..48b1c47 100644 --- a/HttpTrigger/index.ts +++ b/HttpTrigger/index.ts @@ -4,7 +4,7 @@ import * as loadLanguages from 'prismjs/components/index.js'; loadLanguages(); const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise { - if(req.method === "GET") { + if (req.method === "GET") { context.res = { // status: 200, /* Defaults to 200 */ body: "Ok" @@ -12,7 +12,7 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe return; } const data = req.rawBody; - if(!data.startsWith('```')) { + if (!data.startsWith('```')) { context.res = { status: 200, body: req.rawBody @@ -21,23 +21,31 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe } const regex = data.match(/^```([a-zA-Z]*)$/m) const lang = regex[1] - if(!lang) { + if (!lang) { context.res = { status: 200, body: req.rawBody } return } - const highlighted = Prism.highlight(data.replace(/```[a-zA-Z]*/m, '').replace('```', ''), Prism.languages[lang], lang) - - // remove any backticks and the language - const highlightedCleaned = highlighted.trim() - context.res = { - status: 200, - body: `
${highlightedCleaned}
` + try { + const highlighted = Prism.highlight(data.replace(/```[a-zA-Z]*/m, '').replace('```', ''), Prism.languages[lang], lang) + + // remove any backticks and the language + const highlightedCleaned = highlighted.trim() + context.res = { + status: 200, + body: `
${highlightedCleaned}
` + } + } catch { + context.res = { + status: 200, + body: req.rawBody + } } return + }; export default httpTrigger; \ No newline at end of file