Skip to content

Commit 2061cd5

Browse files
committed
Allow failed interceptor activation to return metadata
1 parent db45d29 commit 2061cd5

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/api-server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { generateSPKIFingerprint } from 'mockttp';
1212

1313
import { HtkConfig } from './config';
1414
import { reportError, addBreadcrumb } from './error-tracking';
15-
import { buildInterceptors, Interceptor } from './interceptors';
15+
import { buildInterceptors, Interceptor, ActivationError } from './interceptors';
1616
import { ALLOWED_ORIGINS } from './constants';
1717
import { delay } from './util';
1818

@@ -88,6 +88,8 @@ const withFallback = <R>(p: Promise<R>, timeoutMs: number, defaultValue: R) =>
8888
delay(timeoutMs).then(() => defaultValue)
8989
]);
9090

91+
const isActivationError = (value: any): value is ActivationError => _.isError(value);
92+
9193
const INTERCEPTOR_TIMEOUT = 1000;
9294

9395
const buildResolvers = (
@@ -128,9 +130,9 @@ const buildResolvers = (
128130
const result = await interceptor.activate(proxyPort, options).catch((e) => e);
129131
activationDone = true;
130132

131-
if (_.isError(result)) {
132-
reportError(result);
133-
return { success: false };
133+
if (isActivationError(result)) {
134+
if (result.reportable !== false) reportError(result);
135+
return { success: false, metadata: result.metadata };
134136
} else {
135137
addBreadcrumb(`Successfully activated ${id}`, { category: 'interceptor' });
136138
return { success: true, metadata: result };

src/interceptors/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface Interceptor {
3939
deactivateAll(): Promise<void | {}>;
4040
}
4141

42+
export interface ActivationError extends Error {
43+
metadata?: any; // Any extra metadata with the failure, e.g. if it could be retried
44+
reportable?: boolean; // Set to false to disable reporting this error, it's normal
45+
}
46+
4247
export function buildInterceptors(config: HtkConfig): _.Dictionary<Interceptor> {
4348
const interceptors = [
4449
new FreshChrome(config),

0 commit comments

Comments
 (0)