Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add custom error message when making a request to a backend whic…
…h does not exist (#412)
- Loading branch information
1 parent
fce173d
commit 486aed1
Showing
6 changed files
with
78 additions
and
1 deletion.
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
39 changes: 39 additions & 0 deletions
39
integration-tests/js-compute/fixtures/missing-backend/bin/index.js
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,39 @@ | ||
/* eslint-env serviceworker */ | ||
/* global ReadableStream ObjectStore ObjectStoreEntry */ | ||
import { env } from 'fastly:env'; | ||
import { pass, fail, assert, assertThrows, assertRejects, assertResolves } from "../../../assertions.js"; | ||
|
||
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: ${env('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("/test", async () => { | ||
let error = await assertRejects(async () => fetch('https://example.com', {backend: 'missing'}), TypeError, `Requested backend named 'missing' does not exist`) | ||
if (error) { return error } | ||
return pass() | ||
}); |
19 changes: 19 additions & 0 deletions
19
integration-tests/js-compute/fixtures/missing-backend/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,19 @@ | ||
# 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 = "missing-backend" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "node ../../../../js-compute-runtime-cli.js" | ||
|
||
[local_server] | ||
|
||
[local_server.backends] | ||
|
||
[local_server.backends.httpbin] | ||
url = "https://httpbin.org/" |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/missing-backend/tests.json
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 /test": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/test" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
} | ||
} |