diff --git a/.changeset/catch-constructor-interception-rejection.md b/.changeset/catch-constructor-interception-rejection.md new file mode 100644 index 0000000..b1e0c49 --- /dev/null +++ b/.changeset/catch-constructor-interception-rejection.md @@ -0,0 +1,5 @@ +--- +'@cloudflare/containers': patch +--- + +Log outbound interception failures that happen while a running container is re-attached in the constructor, instead of leaving an unhandled promise rejection. diff --git a/src/lib/container.ts b/src/lib/container.ts index bde983c..769f1b9 100644 --- a/src/lib/container.ts +++ b/src/lib/container.ts @@ -559,7 +559,13 @@ export class Container extends DurableObject { } if (this.container.running) { + // Nothing awaits this promise, so observe a failure here to keep it from + // surfacing as an unhandledRejection. refreshOutboundInterception() re-applies + // and awaits its own copy, so callers on that path still see the error. this.applyOutboundInterceptionPromise = this.applyOutboundInterception(); + this.applyOutboundInterceptionPromise.catch(e => { + console.error(`Error applying outbound interception for container ${this.ctx.id}:`, e); + }); } }); diff --git a/src/tests/container.test.ts b/src/tests/container.test.ts index 08f48a7..c90c02c 100644 --- a/src/tests/container.test.ts +++ b/src/tests/container.test.ts @@ -581,6 +581,27 @@ describe('HTTPS interception runtime guard', () => { }); }); +describe('outbound interception on construction', () => { + test('logs a failure instead of leaving an unhandled rejection', async ({ mockCtx }) => { + class InterceptingContainer extends Container {} + InterceptingContainer.outbound = () => new Response('ok'); + mockCtx.container.running = true; + mockCtx.container.interceptAllOutboundHttp.mockRejectedValue(new Error('intercept failed')); + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); + + const container = new InterceptingContainer(mockCtx as never, {}); + await mockCtx.blockConcurrencyWhile.mock.results[0].value; + await new Promise(resolve => setTimeout(resolve, 0)); + + expect(errorSpy).toHaveBeenCalledWith( + expect.stringContaining('outbound interception'), + expect.any(Error) + ); + await expect(container.applyOutboundInterceptionPromise).rejects.toThrow('intercept failed'); + errorSpy.mockRestore(); + }); +}); + describe('getRandom', () => { baseTest('should return a container stub', async () => { const mockBinding = {