Skip to content

Commit

Permalink
Merge pull request #13762 from sheepster1/fix-promise-conversion-in-i…
Browse files Browse the repository at this point in the history
…nterceptor-consumer

fix(core): unhandled promise rejection in interceptors consumer
  • Loading branch information
kamilmysliwiec authored Aug 12, 2024
2 parents e5c439d + 821b080 commit e5e65c2
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 e5e65c2

Please sign in to comment.