Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: respond with 500 Internal Server Error when an unhandled error h…
…as occured and no response has already been sent to the client
- Loading branch information
1 parent
7c9648b
commit e5982d8
Showing
5 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* global fastly */ | ||
|
||
addEventListener("fetch", ()=>{ | ||
console.log(1) | ||
}) | ||
|
||
addEventListener("fetch", event => { | ||
event.respondWith(app(event)) | ||
}) | ||
|
||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Response} | ||
*/ | ||
async function app(event) { | ||
try { | ||
const path = (new URL(event.request.url)).pathname; | ||
console.log(`path: ${path}`) | ||
console.log(`FASTLY_SERVICE_VERSION: ${fastly.env.get('FASTLY_SERVICE_VERSION')}`) | ||
if (routes.has(path)) { | ||
const routeHandler = routes.get(path); | ||
return await routeHandler() | ||
} | ||
return fail(`${path} endpoint does not exist`) | ||
} catch (error) { | ||
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack) | ||
} | ||
} | ||
|
||
const routes = new Map(); | ||
routes.set('/', () => { | ||
routes.delete('/'); | ||
let test_routes = Array.from(routes.keys()) | ||
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } }); | ||
}); | ||
routes.set("/error", async () => { | ||
throw new Error('uh oh') | ||
}); |
13 changes: 13 additions & 0 deletions
13
integration-tests/js-compute/fixtures/error/fastly.toml.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# This file describes a Fastly Compute@Edge package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["jchampion@fastly.com"] | ||
description = "" | ||
language = "other" | ||
manifest_version = 2 | ||
name = "empty" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "../../../../target/release/js-compute-runtime" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"GET /error": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/error" | ||
}, | ||
"downstream_response": { | ||
"status": 500 | ||
} | ||
} | ||
} |