Skip to content

Commit 486aed1

Browse files
authored
fix: add custom error message when making a request to a backend which does not exist (#412)
1 parent fce173d commit 486aed1

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ jobs:
185185
- hello-world
186186
- includeBytes
187187
- log
188+
- missing-backend
188189
- multiple-set-cookie
189190
- object-store
190191
- random

c-dependencies/js-compute-runtime/error-numbers.msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ MSG_DEF(JSMSG_BACKEND_PORT_INVALID, 0, JSEXN_RANGEERR
9696
MSG_DEF(JSMSG_CACHE_OVERRIDE_MODE_INVALID, 1, JSEXN_TYPEERR, "CacheOverride constructor: 'mode' has to be \"none\", \"pass\", or \"override\", but got \"{0}\"")
9797
MSG_DEF(JSMSG_RESPONSE_VALUE_NOT_UINT8ARRAY, 0, JSEXN_TYPEERR, "Can't convert value to Uint8Array while consuming Body")
9898
MSG_DEF(JSMSG_RESPONSE_BODY_DISTURBED_OR_LOCKED, 0, JSEXN_TYPEERR, "Response body object should not be disturbed or locked")
99+
MSG_DEF(JSMSG_REQUEST_BACKEND_DOES_NOT_EXIST, 1, JSEXN_TYPEERR, "Requested backend named '{0}' does not exist")
99100
//clang-format on

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4811,7 +4811,12 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
48114811
}
48124812

48134813
if (!ok) {
4814-
HANDLE_ERROR(cx, err);
4814+
if (err == FASTLY_ERROR_GENERIC_ERROR || err == FASTLY_ERROR_INVALID_ARGUMENT) {
4815+
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
4816+
JSMSG_REQUEST_BACKEND_DOES_NOT_EXIST, backend_chars.get());
4817+
} else {
4818+
HANDLE_ERROR(cx, err);
4819+
}
48154820
return ReturnPromiseRejectedWithPendingError(cx, args);
48164821
}
48174822
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-env serviceworker */
2+
/* global ReadableStream ObjectStore ObjectStoreEntry */
3+
import { env } from 'fastly:env';
4+
import { pass, fail, assert, assertThrows, assertRejects, assertResolves } from "../../../assertions.js";
5+
6+
addEventListener("fetch", event => {
7+
event.respondWith(app(event))
8+
})
9+
/**
10+
* @param {FetchEvent} event
11+
* @returns {Response}
12+
*/
13+
async function app(event) {
14+
try {
15+
const path = (new URL(event.request.url)).pathname;
16+
console.log(`path: ${path}`)
17+
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`)
18+
if (routes.has(path)) {
19+
const routeHandler = routes.get(path);
20+
return await routeHandler()
21+
}
22+
return fail(`${path} endpoint does not exist`)
23+
} catch (error) {
24+
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack)
25+
}
26+
}
27+
28+
const routes = new Map();
29+
routes.set('/', () => {
30+
routes.delete('/');
31+
let test_routes = Array.from(routes.keys())
32+
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } });
33+
});
34+
35+
routes.set("/test", async () => {
36+
let error = await assertRejects(async () => fetch('https://example.com', {backend: 'missing'}), TypeError, `Requested backend named 'missing' does not exist`)
37+
if (error) { return error }
38+
return pass()
39+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 = "missing-backend"
9+
service_id = ""
10+
11+
[scripts]
12+
build = "node ../../../../js-compute-runtime-cli.js"
13+
14+
[local_server]
15+
16+
[local_server.backends]
17+
18+
[local_server.backends.httpbin]
19+
url = "https://httpbin.org/"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"GET /test": {
3+
"environments": ["viceroy", "c@e"],
4+
"downstream_request": {
5+
"method": "GET",
6+
"pathname": "/test"
7+
},
8+
"downstream_response": {
9+
"status": 200
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)