Skip to content

Commit c3b1bb5

Browse files
authored
fix: Correctly handle pg.Cursor in pg query method (#3567)
1 parent 95fe018 commit c3b1bb5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/tracing/src/integrations/postgres.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hub } from '@sentry/hub';
22
import { EventProcessor, Integration } from '@sentry/types';
3-
import { fill, loadModule, logger } from '@sentry/utils';
3+
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
44

55
interface PgClient {
66
prototype: {
@@ -36,6 +36,7 @@ export class Postgres implements Integration {
3636
* function (query, params, callback) => void
3737
* function (query) => Promise
3838
* function (query, params) => Promise
39+
* function (pg.Cursor) => pg.Cursor
3940
*/
4041
fill(pkg.Client.prototype, 'query', function(orig: () => void | Promise<unknown>) {
4142
return function(this: unknown, config: unknown, values: unknown, callback: unknown) {
@@ -60,10 +61,17 @@ export class Postgres implements Integration {
6061
});
6162
}
6263

63-
return (orig.call(this, config, values) as Promise<unknown>).then((res: unknown) => {
64-
span?.finish();
65-
return res;
66-
});
64+
const rv = typeof values !== 'undefined' ? orig.call(this, config, values) : orig.call(this, config);
65+
66+
if (isThenable(rv)) {
67+
return (rv as Promise<unknown>).then((res: unknown) => {
68+
span?.finish();
69+
return res;
70+
});
71+
}
72+
73+
span?.finish();
74+
return rv;
6775
};
6876
});
6977
}

0 commit comments

Comments
 (0)