Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 0f8b7af

Browse files
committed
Don't throw loud error on Request#cf fetch failure, closes #224
1 parent dc5cdbe commit 0f8b7af

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

packages/http-server/src/plugin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SetupResult,
1414
defaultClock,
1515
} from "@miniflare/shared";
16+
import { dim } from "kleur/colors";
1617
import type { Attributes, Options } from "selfsigned";
1718
import { RequestInfo, fetch } from "undici";
1819
import { getAccessibleHosts } from "./helpers";
@@ -304,9 +305,13 @@ export class HTTPPlugin extends Plugin<HTTPOptions> implements HTTPOptions {
304305
// Write cf so we can reuse it later
305306
await fs.mkdir(path.dirname(cfPath), { recursive: true });
306307
await fs.writeFile(cfPath, cfText, "utf8");
307-
this.ctx.log.info("Updated Request cf object cache!");
308+
this.ctx.log.info("Updated `Request.cf` object cache!");
308309
} catch (e: any) {
309-
this.ctx.log.error(e);
310+
this.ctx.log.warn(
311+
"Unable to fetch the `Request.cf` object! Falling back to a default placeholder...\n" +
312+
"To always use the placeholder, set the `--no-cf-fetch`/`cfFetch: false` option.\n" +
313+
dim(e.cause ? e.cause.stack : e.stack)
314+
);
310315
}
311316
}
312317

packages/http-server/test/plugin.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ test("HTTPPlugin: setupCf: cf fetch caches cf.json at default location", async (
236236
});
237237
const cf = await fs.readFile(cfPath, "utf8");
238238
t.is(cf, '{"colo": "LHR"}');
239-
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated Request cf object cache!");
239+
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated `Request.cf` object cache!");
240240
});
241241
test("HTTPPlugin: setupCf: cf fetch caches cf.json at custom location", async (t) => {
242242
const tmp = await useTmp(t);
@@ -258,7 +258,7 @@ test("HTTPPlugin: setupCf: cf fetch caches cf.json at custom location", async (t
258258
t.false(existsSync(defaultCfPath));
259259
const cf = await fs.readFile(customCfPath, "utf8");
260260
t.is(cf, '{"colo": "LHR"}');
261-
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated Request cf object cache!");
261+
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated `Request.cf` object cache!");
262262
});
263263
test("HTTPPlugin: setupCf: cf fetch reuses cf.json", async (t) => {
264264
const tmp = await useTmp(t);
@@ -300,7 +300,7 @@ test("HTTPPlugin: setupCf: cf fetch refetches cf.json if stale", async (t) => {
300300
});
301301
const cf = await fs.readFile(cfPath, "utf8");
302302
t.is(cf, '{"colo": "MAN"}');
303-
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated Request cf object cache!");
303+
t.is(log.logsAtLevel(LogLevel.INFO)[0], "Updated `Request.cf` object cache!");
304304
});
305305

306306
test("HTTPPlugin: setupHttps: httpsOptions undefined if https disabled", async (t) => {

0 commit comments

Comments
 (0)