Skip to content

Commit

Permalink
fix(core): unhandled promise rejection in interceptors consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepster1 committed Jul 6, 2024
1 parent 6a791dc commit 821b080
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/core/interceptors/interceptors-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class InterceptorsConsumer {
return defer(AsyncResource.bind(() => this.transformDeferred(next)));
}
const handler: CallHandler = {
handle: () => fromPromise(nextFn(i + 1)).pipe(mergeAll()),
handle: () =>
defer(AsyncResource.bind(() => nextFn(i + 1))).pipe(mergeAll()),
};
return interceptors[i].intercept(context, handler);
};
Expand Down
47 changes: 46 additions & 1 deletion packages/core/test/interceptors/interceptors-consumer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
import { expect } from 'chai';
import { Observable, lastValueFrom, of, retry } from 'rxjs';
import { Observable, defer, lastValueFrom, merge, of, retry } from 'rxjs';
import * as sinon from 'sinon';
import { InterceptorsConsumer } from '../../interceptors/interceptors-consumer';

Expand Down Expand Up @@ -179,6 +179,51 @@ describe('InterceptorsConsumer', () => {
});
});
});
describe('deferred promise conversion', () => {
it('should convert promise to observable deferred', async () => {
class TestError extends Error {}
const testInterceptors = [
{
intercept: sinon.stub().callsFake(async (ctx, handler) => {
return merge(
handler.handle(),
defer(() => {
throw new TestError();
}),
);
}),
},
{
intercept: sinon
.stub()
.callsFake(async (ctx, handler) => handler.handle()),
},
{
intercept: sinon
.stub()
.callsFake(async (ctx, handler) => handler.handle()),
},
,
];

const observable = await consumer.intercept(
testInterceptors,
null,
{ constructor: null },
null,
async () => 1,
);

try {
await transformToResult(observable);
} catch (error) {
if (!(error instanceof TestError)) {
throw error;
}
}
expect(testInterceptors[2].intercept.called).to.be.false;
});
});
});

async function transformToResult(resultOrDeferred: any) {
Expand Down

0 comments on commit 821b080

Please sign in to comment.