Skip to content

Commit e5982d8

Browse files
Jake ChampionJakeChampion
Jake Champion
authored andcommitted
fix: respond with 500 Internal Server Error when an unhandled error has occured and no response has already been sent to the client
1 parent 7c9648b commit e5982d8

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

.github/workflows/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ jobs:
244244
- e2e-async-select
245245
- e2e-byte-repeater
246246
- e2e-edge-dictionary
247+
- e2e-error
247248
- e2e-geoip
248249
- e2e-hello-world
249250
- e2e-object-store
@@ -419,6 +420,17 @@ jobs:
419420
with:
420421
fixture: 'edge-dictionary'
421422
fastly-api-token: ${{ secrets.FASTLY_API_TOKEN }}
423+
e2e-error:
424+
runs-on: ubuntu-latest
425+
needs: [build]
426+
steps:
427+
- uses: actions/checkout@v2
428+
with:
429+
submodules: false
430+
- uses: ./.github/actions/e2e
431+
with:
432+
fixture: 'error'
433+
fastly-api-token: ${{ secrets.FASTLY_API_TOKEN }}
422434
e2e-geoip:
423435
runs-on: ubuntu-latest
424436
needs: [build]

c-dependencies/js-compute-runtime/js-compute-runtime.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,11 @@ static void dispatch_fetch_event(JSContext *cx, HandleObject event, double *tota
418418
handler.setObject(*(*FETCH_HANDLERS)[i]);
419419
if (!JS_CallFunctionValue(cx, GLOBAL, handler, argsv, &rval)) {
420420
DumpPendingException(cx, "dispatching FetchEvent\n");
421-
JS_ClearPendingException(cx);
421+
break;
422422
}
423-
if (FetchEvent::state(event) != FetchEvent::State::unhandled)
423+
if (FetchEvent::state(event) != FetchEvent::State::unhandled) {
424424
break;
425+
}
425426
}
426427

427428
FetchEvent::stop_dispatching(event);
@@ -528,8 +529,17 @@ int main(int argc, const char *argv[]) {
528529

529530
// Respond with status `500` if any promise rejections were left unhandled
530531
// and no response was ever sent.
531-
if (!FetchEvent::response_started(fetch_event))
532+
if (!FetchEvent::response_started(fetch_event)) {
533+
FetchEvent::respondWithError(cx, fetch_event);
534+
}
535+
}
536+
537+
// Respond with status `500` if an exception is pending
538+
// and no response was ever sent.
539+
if (JS_IsExceptionPending(cx)) {
540+
if (!FetchEvent::response_started(fetch_event)) {
532541
FetchEvent::respondWithError(cx, fetch_event);
542+
}
533543
}
534544

535545
auto end = system_clock::now();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* global fastly */
2+
3+
addEventListener("fetch", ()=>{
4+
console.log(1)
5+
})
6+
7+
addEventListener("fetch", event => {
8+
event.respondWith(app(event))
9+
})
10+
11+
/**
12+
* @param {FetchEvent} event
13+
* @returns {Response}
14+
*/
15+
async function app(event) {
16+
try {
17+
const path = (new URL(event.request.url)).pathname;
18+
console.log(`path: ${path}`)
19+
console.log(`FASTLY_SERVICE_VERSION: ${fastly.env.get('FASTLY_SERVICE_VERSION')}`)
20+
if (routes.has(path)) {
21+
const routeHandler = routes.get(path);
22+
return await routeHandler()
23+
}
24+
return fail(`${path} endpoint does not exist`)
25+
} catch (error) {
26+
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack)
27+
}
28+
}
29+
30+
const routes = new Map();
31+
routes.set('/', () => {
32+
routes.delete('/');
33+
let test_routes = Array.from(routes.keys())
34+
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } });
35+
});
36+
routes.set("/error", async () => {
37+
throw new Error('uh oh')
38+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file describes a Fastly Compute@Edge package. To learn more visit:
2+
# https://developer.fastly.com/reference/fastly-toml/
3+
4+
authors = ["[email protected]"]
5+
description = ""
6+
language = "other"
7+
manifest_version = 2
8+
name = "empty"
9+
service_id = ""
10+
11+
[scripts]
12+
build = "../../../../target/release/js-compute-runtime"
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"GET /error": {
3+
"environments": ["viceroy", "c@e"],
4+
"downstream_request": {
5+
"method": "GET",
6+
"pathname": "/error"
7+
},
8+
"downstream_response": {
9+
"status": 500
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)