Skip to content

Commit

Permalink
remove unnecessary try-catch for the poisoned stub error
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Dec 23, 2024
1 parent 3246d33 commit 726b0e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
7 changes: 5 additions & 2 deletions packages/wrangler/src/api/startDevWorker/DevEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,13 @@ function createWorkerObject(devEnv: DevEnv): Worker {
);

if (this.config.dev.remote || !local) {
throw new Error("local only");
throw new Error("The platform proxy is only available in local mode");
}

const [env, cf] = await Promise.all([local.getBindings(), local.getCf()]);
const [env, cf] = await Promise.all([
local.getBindingsProxy(),
local.getCfProxy(),
]);

return {
env,
Expand Down
24 changes: 5 additions & 19 deletions packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class LocalRuntimeController extends RuntimeController {
return this.#mf;
}

async getBindings() {
async getBindingsProxy() {
// Initialize the bindings from the Miniflare instance, which will be updated on each reload
if (!this.#bindings) {
const mf = await this.getMiniflareInstance();
Expand All @@ -166,26 +166,12 @@ export class LocalRuntimeController extends RuntimeController {

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;
}
return Reflect.get(this.#bindings ?? {}, prop, receiver);
},
});
}

async getCf() {
async getCfProxy() {
// Initialize the cf properties from the Miniflare instance, which will be updated on each reload
if (!this.#cf) {
const mf = await this.getMiniflareInstance();
Expand Down Expand Up @@ -220,13 +206,13 @@ export class LocalRuntimeController extends RuntimeController {

await this.#mf.setOptions(options);

// If the bindings were fetched, ensure they're up-to-date
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 the cf properties were fetched, ensure they're up-to-date
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();
}
}
Expand Down

0 comments on commit 726b0e4

Please sign in to comment.