Skip to content

Commit

Permalink
test(google-cloud): Replace error handling using node domain (getsent…
Browse files Browse the repository at this point in the history
…ry#14769)

Replace error handling using deprecated node:domain in google-cloud-serverless tests

Fixes getsentry#14769
  • Loading branch information
GrizliK1988 committed Jan 4, 2025
1 parent 8dd8ac5 commit b36c00e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as domain from 'domain';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';

import { wrapCloudEventFunction } from '../../src/gcpfunction/cloud_events';
Expand Down Expand Up @@ -44,21 +43,21 @@ describe('wrapCloudEventFunction', () => {

function handleCloudEvent(fn: CloudEventFunctionWithCallback): Promise<any> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line deprecation/deprecation
const d = domain.create();
const context = {
type: 'event.type',
};
d.on('error', reject);
d.run(() =>
process.nextTick(fn, context, (err: any, result: any) => {

try {
fn(context, (err: any, result: any) => {
if (err != null || err != undefined) {
reject(err);
} else {
resolve(result);
}
}),
);
});
} catch (error) {
reject(error);
}
});
}

Expand Down
27 changes: 14 additions & 13 deletions packages/google-cloud-serverless/test/gcpfunction/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as domain from 'domain';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';

import type { Event } from '@sentry/core';
Expand Down Expand Up @@ -45,22 +44,24 @@ describe('wrapEventFunction', () => {

function handleEvent(fn: EventFunctionWithCallback): Promise<any> {
return new Promise((resolve, reject) => {
// eslint-disable-next-line deprecation/deprecation
const d = domain.create();
const context = {
eventType: 'event.type',
resource: 'some.resource',
};
d.on('error', reject);
d.run(() =>
process.nextTick(fn, {}, context, (err: any, result: any) => {
if (err != null || err != undefined) {
reject(err);
} else {
resolve(result);
}
}),
);

(async () => {
try {
await fn({}, context, (err: any, result: any) => {
if (err != null || err != undefined) {
reject(err);
} else {
resolve(result);
}
});
} catch (error) {
reject(error);
}
})();
});
}

Expand Down
12 changes: 6 additions & 6 deletions packages/google-cloud-serverless/test/gcpfunction/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as domain from 'domain';

import type { Integration } from '@sentry/core';

import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core';
Expand Down Expand Up @@ -58,17 +56,19 @@ describe('GCPFunction', () => {
headers = { ...headers, ...trace_headers };
}
return new Promise((resolve, _reject) => {
// eslint-disable-next-line deprecation/deprecation
const d = domain.create();
const req = {
method: 'POST',
url: '/path?q=query',
headers: headers,
body: { foo: 'bar' },
} as Request;
const res = { end: resolve } as Response;
d.on('error', () => res.end());
d.run(() => process.nextTick(fn, req, res));

try {
fn(req, res);
} catch (error) {
res.end()
}
});
}

Expand Down

0 comments on commit b36c00e

Please sign in to comment.