From a6aa6eee99de0bec42111e09bb1c5c67d35981fa Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Wed, 24 Aug 2022 11:18:23 +0200 Subject: [PATCH 1/8] feat: getting error page to edge functions --- src/lib/functions/synchronous.js | 5 +++-- src/utils/proxy.js | 36 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index e203fbf3a2c..e7c32814284 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -50,6 +50,7 @@ const formatLambdaLocalError = (err, acceptsHtml) => let errorTemplateFile const renderErrorTemplate = async (errString) => { + console.log('rendererrortemplate') const regexPattern = //g const templatePath = './templates/function-error.html' @@ -64,7 +65,7 @@ const renderErrorTemplate = async (errString) => { const processRenderedResponse = async (err, request) => { const acceptsHtml = request.headers && request.headers.accept && request.headers.accept.includes('text/html') const errorString = typeof err === 'string' ? err : formatLambdaLocalError(err, acceptsHtml) - + // I think this is where we show errors? return acceptsHtml ? await renderErrorTemplate(errorString) : errorString } @@ -96,4 +97,4 @@ const validateLambdaResponse = (lambdaResponse) => { return {} } -module.exports = { handleSynchronousFunction } +module.exports = { handleSynchronousFunction, renderErrorTemplate } diff --git a/src/utils/proxy.js b/src/utils/proxy.js index 122f9b43e6d..c41e63b9db6 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -4,6 +4,7 @@ const { readFile } = require('fs').promises const http = require('http') const https = require('https') const path = require('path') +const { join } = require('path') const contentType = require('content-type') const cookie = require('cookie') @@ -20,6 +21,7 @@ const toReadableStream = require('to-readable-stream') const edgeFunctions = require('../lib/edge-functions') const { fileExistsAsync, isFileAsync } = require('../lib/fs') +// const { renderErrorTemplate } = require('../lib/functions/synchronous') const { NETLIFYDEVLOG, NETLIFYDEVWARN } = require('./command-helpers') const { createStreamPromise } = require('./create-stream-promise') @@ -359,10 +361,11 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir const headersRules = headersForPath(headers, requestURL.pathname) proxyRes.on('data', function onData(data) { + console.log('we get here?') responseData.push(data) }) - proxyRes.on('end', function onEnd() { + proxyRes.on('end', async function onEnd() { const responseBody = Buffer.concat(responseData) let responseStatus = req.proxyOptions.status || proxyRes.statusCode @@ -385,11 +388,21 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir Object.entries(headersRules).forEach(([key, val]) => { res.setHeader(key, val) }) - res.writeHead(responseStatus, proxyRes.headers) + console.log({ responseBody }) + console.log(Buffer.from(responseBody).toString()) + + if (responseStatus === 500 && proxyRes.headers['x-nf-uncaught-error'] === '1') { + const acceptsHtml = req.headers && req.headers.accept && req.headers.accept.includes('text/html') + console.log('something went wrong', acceptsHtml) + const errorResponse = acceptsHtml ? await renderErrorTemplate(responseBody) : responseBody + console.log({ errorResponse: typeof errorResponse, body: typeof responseBody }) + res.write(errorResponse) + } + console.log('got here?') if (responseStatus !== 304) { - res.write(responseBody) + // res.write(responseBody) } res.end() @@ -411,6 +424,23 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir return handlers } +let errorTemplateFile + +const renderErrorTemplate = async (errString) => { + console.log('rendererrortemplate') + const regexPattern = //g + const templatePath = '../lib/functions/templates/function-error.html' + console.log({ templatePath }) + try { + console.log('in try', errString) + errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) + return errorTemplateFile.replace(regexPattern, errString) + } catch { + console.log('catching') + return errString + } +} + const onRequest = async ({ addonsUrls, edgeFunctionsProxy, functionsServer, proxy, rewriter, settings }, req, res) => { req.originalBody = ['GET', 'OPTIONS', 'HEAD'].includes(req.method) ? null From 89599efa6bdaabea3aff91a5b826ee34f27ef475 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Fri, 9 Sep 2022 14:27:53 +0200 Subject: [PATCH 2/8] feat: set content length to have correct headers --- src/lib/functions/synchronous.js | 5 ++- src/utils/proxy.js | 52 +++++++++++++++++--------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index e7c32814284..e203fbf3a2c 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -50,7 +50,6 @@ const formatLambdaLocalError = (err, acceptsHtml) => let errorTemplateFile const renderErrorTemplate = async (errString) => { - console.log('rendererrortemplate') const regexPattern = //g const templatePath = './templates/function-error.html' @@ -65,7 +64,7 @@ const renderErrorTemplate = async (errString) => { const processRenderedResponse = async (err, request) => { const acceptsHtml = request.headers && request.headers.accept && request.headers.accept.includes('text/html') const errorString = typeof err === 'string' ? err : formatLambdaLocalError(err, acceptsHtml) - // I think this is where we show errors? + return acceptsHtml ? await renderErrorTemplate(errorString) : errorString } @@ -97,4 +96,4 @@ const validateLambdaResponse = (lambdaResponse) => { return {} } -module.exports = { handleSynchronousFunction, renderErrorTemplate } +module.exports = { handleSynchronousFunction } diff --git a/src/utils/proxy.js b/src/utils/proxy.js index c41e63b9db6..0d19555c477 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -30,6 +30,23 @@ const { createRewriter, onChanges } = require('./rules-proxy') const shouldGenerateETag = Symbol('Internal: response should generate ETag') +let errorTemplateFile + +const renderErrorTemplate = async (errString) => { + console.log('rendererrortemplate') + const regexPattern = //g + const templatePath = '../lib/functions/templates/function-error.html' + console.log({ templatePath }) + try { + console.log('in try', errString) + errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) + return errorTemplateFile.replace(regexPattern, errString) + } catch { + console.log('catching') + return errString + } +} + const isInternal = function (url) { return url.startsWith('/.netlify/') } @@ -388,21 +405,23 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir Object.entries(headersRules).forEach(([key, val]) => { res.setHeader(key, val) }) - res.writeHead(responseStatus, proxyRes.headers) - console.log({ responseBody }) - console.log(Buffer.from(responseBody).toString()) - if (responseStatus === 500 && proxyRes.headers['x-nf-uncaught-error'] === '1') { + const isUncaughtError = responseStatus === 500 && proxyRes.headers['x-nf-uncaught-error'] === '1' + + if (edgeFunctions.isEdgeFunctionsRequest(req) && isUncaughtError) { const acceptsHtml = req.headers && req.headers.accept && req.headers.accept.includes('text/html') - console.log('something went wrong', acceptsHtml) const errorResponse = acceptsHtml ? await renderErrorTemplate(responseBody) : responseBody - console.log({ errorResponse: typeof errorResponse, body: typeof responseBody }) + const contentLength = Buffer.from(errorResponse, 'utf8').byteLength + + res.setHeader('content-length', contentLength) res.write(errorResponse) + return res.end() } - console.log('got here?') + + res.writeHead(responseStatus, proxyRes.headers) if (responseStatus !== 304) { - // res.write(responseBody) + res.write(responseBody) } res.end() @@ -424,23 +443,6 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir return handlers } -let errorTemplateFile - -const renderErrorTemplate = async (errString) => { - console.log('rendererrortemplate') - const regexPattern = //g - const templatePath = '../lib/functions/templates/function-error.html' - console.log({ templatePath }) - try { - console.log('in try', errString) - errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) - return errorTemplateFile.replace(regexPattern, errString) - } catch { - console.log('catching') - return errString - } -} - const onRequest = async ({ addonsUrls, edgeFunctionsProxy, functionsServer, proxy, rewriter, settings }, req, res) => { req.originalBody = ['GET', 'OPTIONS', 'HEAD'].includes(req.method) ? null From 75748aaf338a587edc41873cf877803be1c041aa Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Fri, 9 Sep 2022 15:02:05 +0200 Subject: [PATCH 3/8] feat: move render-error-template to own file --- src/lib/functions/render-error-remplate.js | 18 ++++++++++++++++++ src/lib/functions/synchronous.js | 17 +---------------- src/utils/proxy.js | 21 +-------------------- 3 files changed, 20 insertions(+), 36 deletions(-) create mode 100644 src/lib/functions/render-error-remplate.js diff --git a/src/lib/functions/render-error-remplate.js b/src/lib/functions/render-error-remplate.js new file mode 100644 index 00000000000..a2422cd6b54 --- /dev/null +++ b/src/lib/functions/render-error-remplate.js @@ -0,0 +1,18 @@ +const { readFile } = require('fs').promises +const { join } = require('path') + +let errorTemplateFile + +const renderErrorTemplate = async (errString) => { + const regexPattern = //g + const templatePath = './templates/function-error.html' + + try { + errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) + return errorTemplateFile.replace(regexPattern, errString) + } catch { + return errString + } +} + +module.exports = renderErrorTemplate diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index e203fbf3a2c..9d1f0c6a7df 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -1,10 +1,9 @@ // @ts-check const { Buffer } = require('buffer') -const { readFile } = require('fs').promises -const { join } = require('path') const { NETLIFYDEVERR } = require('../../utils') +const renderErrorTemplate = require('./render-error-remplate') const { detectAwsSdkError } = require('./utils') const addHeaders = (headers, response) => { @@ -47,20 +46,6 @@ const formatLambdaLocalError = (err, acceptsHtml) => }) : `${err.errorType}: ${err.errorMessage}\n ${err.stackTrace.join('\n ')}` -let errorTemplateFile - -const renderErrorTemplate = async (errString) => { - const regexPattern = //g - const templatePath = './templates/function-error.html' - - try { - errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) - return errorTemplateFile.replace(regexPattern, errString) - } catch { - return errString - } -} - const processRenderedResponse = async (err, request) => { const acceptsHtml = request.headers && request.headers.accept && request.headers.accept.includes('text/html') const errorString = typeof err === 'string' ? err : formatLambdaLocalError(err, acceptsHtml) diff --git a/src/utils/proxy.js b/src/utils/proxy.js index 0d19555c477..35732c57742 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -4,7 +4,6 @@ const { readFile } = require('fs').promises const http = require('http') const https = require('https') const path = require('path') -const { join } = require('path') const contentType = require('content-type') const cookie = require('cookie') @@ -21,7 +20,7 @@ const toReadableStream = require('to-readable-stream') const edgeFunctions = require('../lib/edge-functions') const { fileExistsAsync, isFileAsync } = require('../lib/fs') -// const { renderErrorTemplate } = require('../lib/functions/synchronous') +const renderErrorTemplate = require('../lib/functions/render-error-remplate') const { NETLIFYDEVLOG, NETLIFYDEVWARN } = require('./command-helpers') const { createStreamPromise } = require('./create-stream-promise') @@ -30,23 +29,6 @@ const { createRewriter, onChanges } = require('./rules-proxy') const shouldGenerateETag = Symbol('Internal: response should generate ETag') -let errorTemplateFile - -const renderErrorTemplate = async (errString) => { - console.log('rendererrortemplate') - const regexPattern = //g - const templatePath = '../lib/functions/templates/function-error.html' - console.log({ templatePath }) - try { - console.log('in try', errString) - errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) - return errorTemplateFile.replace(regexPattern, errString) - } catch { - console.log('catching') - return errString - } -} - const isInternal = function (url) { return url.startsWith('/.netlify/') } @@ -378,7 +360,6 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir const headersRules = headersForPath(headers, requestURL.pathname) proxyRes.on('data', function onData(data) { - console.log('we get here?') responseData.push(data) }) From 07e53de7f2a98bce229154e48d35f1f7201f4044 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Thu, 22 Sep 2022 13:22:20 +0200 Subject: [PATCH 4/8] feat: render edge function error in its own template --- src/lib/functions/render-error-remplate.js | 3 +- .../templates/edge-function-error.html | 289 ++++++++++++++++++ .../functions/templates/function-error.html | 1 + src/utils/proxy.js | 12 +- 4 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 src/lib/functions/templates/edge-function-error.html diff --git a/src/lib/functions/render-error-remplate.js b/src/lib/functions/render-error-remplate.js index a2422cd6b54..fb9b573b549 100644 --- a/src/lib/functions/render-error-remplate.js +++ b/src/lib/functions/render-error-remplate.js @@ -3,9 +3,8 @@ const { join } = require('path') let errorTemplateFile -const renderErrorTemplate = async (errString) => { +const renderErrorTemplate = async (errString, templatePath = './templates/function-error.html') => { const regexPattern = //g - const templatePath = './templates/function-error.html' try { errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) diff --git a/src/lib/functions/templates/edge-function-error.html b/src/lib/functions/templates/edge-function-error.html new file mode 100644 index 00000000000..6187cd6846c --- /dev/null +++ b/src/lib/functions/templates/edge-function-error.html @@ -0,0 +1,289 @@ + + + + Invocation Failed + + + + + + + +
+
+
+

+ + This edge function has crashed +

+

An unhandled error in the edge function code triggered the following message:

+ +
+ +
+ + +
+ + +
+ + diff --git a/src/lib/functions/templates/function-error.html b/src/lib/functions/templates/function-error.html index 480dba0c4ec..81e8c0bfb78 100644 --- a/src/lib/functions/templates/function-error.html +++ b/src/lib/functions/templates/function-error.html @@ -24,6 +24,7 @@ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; color: var(--colorDefaultTextColor); + margin: 0; } main { diff --git a/src/utils/proxy.js b/src/utils/proxy.js index 35732c57742..0cbd2d44854 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -4,6 +4,8 @@ const { readFile } = require('fs').promises const http = require('http') const https = require('https') const path = require('path') +const util = require('util') +const zlib = require('zlib') const contentType = require('content-type') const cookie = require('cookie') @@ -27,6 +29,7 @@ const { createStreamPromise } = require('./create-stream-promise') const { headersForPath, parseHeaders } = require('./headers') const { createRewriter, onChanges } = require('./rules-proxy') +const decompress = util.promisify(zlib.gunzip) const shouldGenerateETag = Symbol('Internal: response should generate ETag') const isInternal = function (url) { @@ -391,7 +394,14 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir if (edgeFunctions.isEdgeFunctionsRequest(req) && isUncaughtError) { const acceptsHtml = req.headers && req.headers.accept && req.headers.accept.includes('text/html') - const errorResponse = acceptsHtml ? await renderErrorTemplate(responseBody) : responseBody + const decompressedBody = await decompress(responseBody) + .then((decompressed) => decompressed.toString()) + .catch(() => { + throw new Error('Could not decompress gzip') + }) + const errorResponse = acceptsHtml + ? await renderErrorTemplate(decompressedBody, './templates/edge-function-error.html') + : decompressedBody const contentLength = Buffer.from(errorResponse, 'utf8').byteLength res.setHeader('content-length', contentLength) From 00ca94aaba03d58e2be240b7486decd980b10600 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Thu, 22 Sep 2022 18:03:21 +0200 Subject: [PATCH 5/8] feat: parse error in proxy file --- src/lib/functions/synchronous.js | 2 +- .../{functions => }/render-error-remplate.js | 0 .../templates/edge-function-error.html | 0 .../templates/function-error.html | 0 src/utils/proxy.js | 23 +++++++++++++------ 5 files changed, 17 insertions(+), 8 deletions(-) rename src/lib/{functions => }/render-error-remplate.js (100%) rename src/lib/{functions => }/templates/edge-function-error.html (100%) rename src/lib/{functions => }/templates/function-error.html (100%) diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index 9d1f0c6a7df..8d4743b862f 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -2,8 +2,8 @@ const { Buffer } = require('buffer') const { NETLIFYDEVERR } = require('../../utils') +const renderErrorTemplate = require('../render-error-remplate') -const renderErrorTemplate = require('./render-error-remplate') const { detectAwsSdkError } = require('./utils') const addHeaders = (headers, response) => { diff --git a/src/lib/functions/render-error-remplate.js b/src/lib/render-error-remplate.js similarity index 100% rename from src/lib/functions/render-error-remplate.js rename to src/lib/render-error-remplate.js diff --git a/src/lib/functions/templates/edge-function-error.html b/src/lib/templates/edge-function-error.html similarity index 100% rename from src/lib/functions/templates/edge-function-error.html rename to src/lib/templates/edge-function-error.html diff --git a/src/lib/functions/templates/function-error.html b/src/lib/templates/function-error.html similarity index 100% rename from src/lib/functions/templates/function-error.html rename to src/lib/templates/function-error.html diff --git a/src/utils/proxy.js b/src/utils/proxy.js index 0cbd2d44854..61c275770ee 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -22,7 +22,7 @@ const toReadableStream = require('to-readable-stream') const edgeFunctions = require('../lib/edge-functions') const { fileExistsAsync, isFileAsync } = require('../lib/fs') -const renderErrorTemplate = require('../lib/functions/render-error-remplate') +const renderErrorTemplate = require('../lib/render-error-remplate') const { NETLIFYDEVLOG, NETLIFYDEVWARN } = require('./command-helpers') const { createStreamPromise } = require('./create-stream-promise') @@ -32,6 +32,18 @@ const { createRewriter, onChanges } = require('./rules-proxy') const decompress = util.promisify(zlib.gunzip) const shouldGenerateETag = Symbol('Internal: response should generate ETag') +const formatEdgeFunctionError = (errorBuffer, acceptsHtml) => { + const parsedError = JSON.parse(errorBuffer.toString()) + + return acceptsHtml + ? JSON.stringify({ + errorType: parsedError.name, + errorMessage: parsedError.message, + trace: parsedError.stack.split('\\n'), + }) + : `${parsedError.name}: ${parsedError.message}\n ${parsedError.stack}` +} + const isInternal = function (url) { return url.startsWith('/.netlify/') } @@ -395,13 +407,10 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir if (edgeFunctions.isEdgeFunctionsRequest(req) && isUncaughtError) { const acceptsHtml = req.headers && req.headers.accept && req.headers.accept.includes('text/html') const decompressedBody = await decompress(responseBody) - .then((decompressed) => decompressed.toString()) - .catch(() => { - throw new Error('Could not decompress gzip') - }) + const formattedBody = formatEdgeFunctionError(decompressedBody, acceptsHtml) const errorResponse = acceptsHtml - ? await renderErrorTemplate(decompressedBody, './templates/edge-function-error.html') - : decompressedBody + ? await renderErrorTemplate(formattedBody, './templates/edge-function-error.html') + : formattedBody const contentLength = Buffer.from(errorResponse, 'utf8').byteLength res.setHeader('content-length', contentLength) From c4bdb998bac2ce02effcedede7d166237d6e19a1 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Fri, 23 Sep 2022 12:42:04 +0200 Subject: [PATCH 6/8] feat: change parsing in formatEdgeFunctionError, remove default fallback in renderer --- src/lib/functions/synchronous.js | 2 +- src/lib/render-error-remplate.js | 2 +- src/utils/proxy.js | 20 ++++++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index 8d4743b862f..530a00dd6fd 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -50,7 +50,7 @@ const processRenderedResponse = async (err, request) => { const acceptsHtml = request.headers && request.headers.accept && request.headers.accept.includes('text/html') const errorString = typeof err === 'string' ? err : formatLambdaLocalError(err, acceptsHtml) - return acceptsHtml ? await renderErrorTemplate(errorString) : errorString + return acceptsHtml ? await renderErrorTemplate(errorString, './templates/function-error.html') : errorString } const handleErr = async (err, request, response) => { diff --git a/src/lib/render-error-remplate.js b/src/lib/render-error-remplate.js index fb9b573b549..09c96a0a7d8 100644 --- a/src/lib/render-error-remplate.js +++ b/src/lib/render-error-remplate.js @@ -3,7 +3,7 @@ const { join } = require('path') let errorTemplateFile -const renderErrorTemplate = async (errString, templatePath = './templates/function-error.html') => { +const renderErrorTemplate = async (errString, templatePath) => { const regexPattern = //g try { diff --git a/src/utils/proxy.js b/src/utils/proxy.js index 61c275770ee..a0aa3376b00 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -33,15 +33,19 @@ const decompress = util.promisify(zlib.gunzip) const shouldGenerateETag = Symbol('Internal: response should generate ETag') const formatEdgeFunctionError = (errorBuffer, acceptsHtml) => { - const parsedError = JSON.parse(errorBuffer.toString()) + const { + error: { message, name, stack }, + } = JSON.parse(errorBuffer.toString()) - return acceptsHtml - ? JSON.stringify({ - errorType: parsedError.name, - errorMessage: parsedError.message, - trace: parsedError.stack.split('\\n'), - }) - : `${parsedError.name}: ${parsedError.message}\n ${parsedError.stack}` + if (!acceptsHtml) { + return `${name}: ${message}\n ${stack}` + } + + return JSON.stringify({ + errorType: name, + errorMessage: message, + trace: stack.split('\\n'), + }) } const isInternal = function (url) { From 84884f17d584d34bf7076ecc1040d65779936c74 Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Fri, 23 Sep 2022 13:09:03 +0200 Subject: [PATCH 7/8] feat: remove check on 500 status code --- src/utils/proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/proxy.js b/src/utils/proxy.js index a0aa3376b00..bdebb7a92c0 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -406,7 +406,7 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir res.setHeader(key, val) }) - const isUncaughtError = responseStatus === 500 && proxyRes.headers['x-nf-uncaught-error'] === '1' + const isUncaughtError = proxyRes.headers['x-nf-uncaught-error'] === '1' if (edgeFunctions.isEdgeFunctionsRequest(req) && isUncaughtError) { const acceptsHtml = req.headers && req.headers.accept && req.headers.accept.includes('text/html') From 906fe6db8d2d7662e0c441fc87302c3dd3f66e6d Mon Sep 17 00:00:00 2001 From: khen <30577427+khendrikse@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:05:03 +0200 Subject: [PATCH 8/8] feat: remove redundant function error template and clean old one --- src/lib/functions/synchronous.js | 4 +- src/lib/render-error-remplate.js | 8 +- src/lib/templates/edge-function-error.html | 289 --------------------- src/lib/templates/function-error.html | 55 +--- src/utils/proxy.js | 2 +- 5 files changed, 11 insertions(+), 347 deletions(-) delete mode 100644 src/lib/templates/edge-function-error.html diff --git a/src/lib/functions/synchronous.js b/src/lib/functions/synchronous.js index 530a00dd6fd..2af21d4d5af 100644 --- a/src/lib/functions/synchronous.js +++ b/src/lib/functions/synchronous.js @@ -50,7 +50,9 @@ const processRenderedResponse = async (err, request) => { const acceptsHtml = request.headers && request.headers.accept && request.headers.accept.includes('text/html') const errorString = typeof err === 'string' ? err : formatLambdaLocalError(err, acceptsHtml) - return acceptsHtml ? await renderErrorTemplate(errorString, './templates/function-error.html') : errorString + return acceptsHtml + ? await renderErrorTemplate(errorString, './templates/function-error.html', 'function') + : errorString } const handleErr = async (err, request, response) => { diff --git a/src/lib/render-error-remplate.js b/src/lib/render-error-remplate.js index 09c96a0a7d8..766f153b666 100644 --- a/src/lib/render-error-remplate.js +++ b/src/lib/render-error-remplate.js @@ -3,12 +3,12 @@ const { join } = require('path') let errorTemplateFile -const renderErrorTemplate = async (errString, templatePath) => { - const regexPattern = //g - +const renderErrorTemplate = async (errString, templatePath, functionType) => { + const errorDetailsRegex = //g + const functionTypeRegex = //g try { errorTemplateFile = errorTemplateFile || (await readFile(join(__dirname, templatePath), 'utf-8')) - return errorTemplateFile.replace(regexPattern, errString) + return errorTemplateFile.replace(errorDetailsRegex, errString).replace(functionTypeRegex, functionType) } catch { return errString } diff --git a/src/lib/templates/edge-function-error.html b/src/lib/templates/edge-function-error.html deleted file mode 100644 index 6187cd6846c..00000000000 --- a/src/lib/templates/edge-function-error.html +++ /dev/null @@ -1,289 +0,0 @@ - - - - Invocation Failed - - - - - - - -
-
-
-

- - This edge function has crashed -

-

An unhandled error in the edge function code triggered the following message:

- -
- -
- - -
- - -
- - diff --git a/src/lib/templates/function-error.html b/src/lib/templates/function-error.html index 81e8c0bfb78..b32ee4231b1 100644 --- a/src/lib/templates/function-error.html +++ b/src/lib/templates/function-error.html @@ -197,7 +197,9 @@

fill="#900B31" /> - This function has crashed + This + + has crashed

An unhandled error in the function code triggered the following message:

Stack trace

- - - - -
-

Next steps

-
- - diff --git a/src/utils/proxy.js b/src/utils/proxy.js index bdebb7a92c0..36487b3d654 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -413,7 +413,7 @@ const initializeProxy = async function ({ configPath, distDir, port, projectDir const decompressedBody = await decompress(responseBody) const formattedBody = formatEdgeFunctionError(decompressedBody, acceptsHtml) const errorResponse = acceptsHtml - ? await renderErrorTemplate(formattedBody, './templates/edge-function-error.html') + ? await renderErrorTemplate(formattedBody, './templates/function-error.html', 'edge function') : formattedBody const contentLength = Buffer.from(errorResponse, 'utf8').byteLength