Skip to content

Commit

Permalink
improves error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Nov 20, 2024
1 parent 77a3aaf commit 47e8508
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
3 changes: 1 addition & 2 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ export async function getPlatformProxy<
server: {
port: 0,
},
logLevel: "none",
logLevel: "error",
liveReload: false,
watch: false,
persist:
typeof options.persist === "object"
? options.persist.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async function resolveDevConfig(
hostname: host ?? getInferredHost(routes),
},
liveReload: input.dev?.liveReload || false,
logLevel: input.dev?.logLevel,
testScheduled: input.dev?.testScheduled,
// absolute resolved path
persist: localPersistencePath,
Expand Down
98 changes: 51 additions & 47 deletions packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,53 +147,57 @@ export class LocalRuntimeController extends RuntimeController {
#cf?: Record<string, unknown>;

async getMiniflareInstance() {
if (this.#mf) {
return this.#mf;
}

await events.once(this, "reloadComplete");

if (!this.#mf) {
throw new Error("Miniflare instance is not available after reload");
await events.once(this, "reloadComplete");

if (!this.#mf) {
throw new Error("Miniflare instance is not available after reload");
}
}

return this.#mf;
}

async getBindings(): Promise<Record<string, unknown>> {
const mf = await this.getMiniflareInstance();

this.#bindings = await mf.getBindings();

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
async getBindings() {
// Initialize the bindings from the Miniflare instance, which will be updated on each reload
if (!this.#bindings) {
const mf = await this.getMiniflareInstance();
this.#bindings = await mf.getBindings();
}

return new Proxy(
{},
{
get(_, prop, receiver) {
return Reflect.get(self.#bindings ?? {}, prop, receiver);
},
}
);
return new Proxy({} as Record<string, unknown>, {
get: (_, prop, receiver) => {
try {
return Reflect.get(this.#bindings ?? {}, prop, receiver);
} catch (e) {
if (
e instanceof Error &&
e.message.startsWith("Attempted to use poisoned stub")
) {
throw new Error(
`The binding "${prop.toString()}" is not available; Please check the logs for more information.`,
{ cause: e }
);
}

throw e;
}
},
});
}

async getCf(): Promise<Record<string, unknown>> {
const mf = await this.getMiniflareInstance();

this.#cf = await mf.getCf();

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
async getCf() {
// Initialize the cf properties from the Miniflare instance, which will be updated on each reload
if (!this.#cf) {
const mf = await this.getMiniflareInstance();
this.#cf = await mf.getCf();
}

return new Proxy(
{},
{
get(_, prop, receiver) {
return Reflect.get(self.#cf ?? {}, prop, receiver);
},
}
);
return new Proxy({} as Record<string, unknown>, {
get: (_, prop, receiver) => {
return Reflect.get(this.#cf ?? {}, prop, receiver);
},
});
}

onBundleStart(_: BundleStartEvent) {
Expand All @@ -216,6 +220,16 @@ export class LocalRuntimeController extends RuntimeController {
logger.log(chalk.dim("⎔ Reloading local server..."));

await this.#mf.setOptions(options);

if (this.#bindings) {
// If the bindings were already fetched, re-fetch them to ensure they're up-to-date
this.#bindings = await this.#mf.getBindings();
}

if (this.#cf) {
// If the cf properties were already fetched, re-fetch them to ensure they're up-to-date
this.#cf = await this.#mf.getCf();
}
}
// All asynchronous `Miniflare` methods will wait for all `setOptions()`
// calls to complete before resolving. To ensure we get the `url` and
Expand All @@ -229,16 +243,6 @@ export class LocalRuntimeController extends RuntimeController {
return;
}

if (this.#bindings) {
// Re-fetch bindings to ensure they're up-to-date
this.#bindings = await this.#mf.getBindings();
}

if (this.#cf) {
// Re-fetch `CfProperties` to ensure they're up-to-date
this.#cf = await this.#mf.getCf();
}

// Get entrypoint addresses
const entrypointAddresses: WorkerEntrypointsDefinition = {};
for (const name of entrypointNames) {
Expand Down

0 comments on commit 47e8508

Please sign in to comment.