Skip to content

Commit 419b48b

Browse files
committed
Report startup & interceptor activation errors
1 parent 2319217 commit 419b48b

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/commands/start.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Import types only from TS
2-
import * as ErrorTrackingModule from '../error-tracking';
3-
import * as IndexTypeModule from '../index';
2+
type ErrorTrackingModule = typeof import('../error-tracking');
3+
type IndexTypeModule = typeof import('../index');
44

55
import { IS_PROD_BUILD } from '../util';
66

@@ -19,12 +19,12 @@ function maybeBundleImport<T>(moduleName: string): T {
1919
return require('../' + moduleName);
2020
}
2121
}
22-
const { initErrorTracking } = maybeBundleImport<typeof ErrorTrackingModule>('error-tracking');
22+
const { initErrorTracking, reportError } = maybeBundleImport<ErrorTrackingModule>('error-tracking');
2323
initErrorTracking();
2424

2525
import { Command, flags } from '@oclif/command'
2626

27-
const { runHTK } = maybeBundleImport<typeof IndexTypeModule>('index');
27+
const { runHTK } = maybeBundleImport<IndexTypeModule>('index');
2828

2929
class HttpToolkitServer extends Command {
3030
static description = 'start the HTTP Toolkit server'
@@ -39,7 +39,10 @@ class HttpToolkitServer extends Command {
3939
async run() {
4040
const { flags } = this.parse(HttpToolkitServer);
4141

42-
await runHTK({ configPath: flags.config });
42+
await runHTK({ configPath: flags.config }).catch(async (error) => {
43+
await reportError(error);
44+
throw error;
45+
});
4346
}
4447
}
4548

src/error-tracking.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function addBreadcrumb(message: string, data: Sentry.Breadcrumb) {
8282
Sentry.addBreadcrumb(Object.assign({ message }, data));
8383
}
8484

85-
export function reportError(error: Error | string) {
85+
export function reportError(error: Error | string): undefined | Promise<void> {
8686
console.warn(error);
8787
if (!sentryInitialized) return;
8888

@@ -91,4 +91,8 @@ export function reportError(error: Error | string) {
9191
} else {
9292
Sentry.captureException(error);
9393
}
94+
95+
return Sentry.flush(500).then((sentSuccessfully) => {
96+
if (sentSuccessfully === false) console.log('Error reporting timed out');
97+
});
9498
}

src/httptoolkit-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const buildResolvers = (
7575
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
7676

7777
await Promise.race([
78-
interceptor.activate(proxyPort, options),
78+
interceptor.activate(proxyPort, options).catch(reportError),
7979
delay(30000) // After 30s, we don't stop activating, but we do report failure
8080
]);
8181

@@ -95,7 +95,7 @@ const buildResolvers = (
9595
const interceptor = interceptors[id];
9696
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
9797

98-
await interceptor.deactivate(proxyPort, options);
98+
await interceptor.deactivate(proxyPort, options).catch(reportError);
9999
return !interceptor.isActive(proxyPort);
100100
},
101101
triggerUpdate: () => {

0 commit comments

Comments
 (0)