Skip to content

Commit b38998b

Browse files
committed
Make interceptor queries resilient to exceptions in individual interceptors
1 parent 0528b75 commit b38998b

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/commands/start.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import * as Sentry from '@sentry/node';
2-
3-
const packageJson = require('../../package.json');
4-
let { SENTRY_DSN } = process.env;
5-
if (!SENTRY_DSN && process.env.HTTPTOOLKIT_SERVER_BINPATH) {
6-
// If we're a built binary, use the standard DSN automatically
7-
SENTRY_DSN = 'https://[email protected]/1371158';
8-
}
9-
10-
if (SENTRY_DSN) {
11-
Sentry.init({ dsn: SENTRY_DSN, release: packageJson.version });
12-
}
1+
import { initErrorTracking } from '../error-tracking';
2+
initErrorTracking();
133

144
import { Command, flags } from '@oclif/command'
155
import { runHTK } from '../index';

src/error-tracking.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as Sentry from '@sentry/node';
2+
3+
let sentryInitialized = false;
4+
5+
export function initErrorTracking() {
6+
const packageJson = require('../package.json');
7+
let { SENTRY_DSN } = process.env;
8+
if (!SENTRY_DSN && process.env.HTTPTOOLKIT_SERVER_BINPATH) {
9+
// If we're a built binary, use the standard DSN automatically
10+
SENTRY_DSN = 'https://[email protected]/1371158';
11+
}
12+
13+
if (SENTRY_DSN) {
14+
Sentry.init({ dsn: SENTRY_DSN, release: packageJson.version });
15+
sentryInitialized = true;
16+
}
17+
}
18+
19+
export function reportError(error: Error | string) {
20+
console.warn(error);
21+
if (!sentryInitialized) return;
22+
23+
if (typeof error === 'string') {
24+
Sentry.captureMessage(error);
25+
} else {
26+
Sentry.captureException(error);
27+
}
28+
}

src/httptoolkit-server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GraphQLServer } from 'graphql-yoga'
44
import { GraphQLScalarType } from 'graphql';
55

66
import { HtkConfig } from './config';
7+
import { reportError } from './error-tracking';
78
import { buildInterceptors, Interceptor } from './interceptors';
89

910
const packageJson = require('../package.json');
@@ -85,10 +86,18 @@ const buildResolvers = (
8586

8687
Interceptor: {
8788
isActivable: (interceptor: Interceptor) => {
88-
return interceptor.isActivable();
89+
return interceptor.isActivable().catch((e) => {
90+
reportError(e);
91+
return false;
92+
});
8993
},
9094
isActive: (interceptor: Interceptor, args: _.Dictionary<any>) => {
91-
return interceptor.isActive(args.proxyPort);
95+
try {
96+
return interceptor.isActive(args.proxyPort);
97+
} catch (e) {
98+
reportError(e);
99+
return false;
100+
}
92101
}
93102
},
94103

0 commit comments

Comments
 (0)