Skip to content

Commit 949d75d

Browse files
committed
Add codes & tags for intentional rule abort failures
1 parent efdd1e9 commit 949d75d

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/rules/passthrough-handling.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
CallbackRequestResult,
1919
CallbackResponseMessageResult
2020
} from './requests/request-handler-definitions';
21+
import { AbortError } from './requests/request-handlers';
2122
import {
2223
CADefinition,
2324
PassThroughLookupOptions
@@ -413,5 +414,9 @@ export function buildUpstreamErrorTags(e: ErrorLike) {
413414
tags.push('passthrough-tls-error:ssl-alert-' + tlsAlertMatch[1]);
414415
}
415416

417+
if (e instanceof AbortError) {
418+
tags.push('passthrough-error:mockttp-abort')
419+
}
420+
416421
return tags;
417422
}

src/rules/requests/request-handlers.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class AbortError extends TypedError {
132132

133133
constructor(
134134
message: string,
135-
readonly code?: string
135+
readonly code: string
136136
) {
137137
super(message);
138138
}
@@ -227,11 +227,11 @@ export class CallbackHandler extends CallbackHandlerDefinition {
227227

228228
if (outResponse === 'close') {
229229
(request as any).socket.end();
230-
throw new AbortError('Connection closed intentionally by rule');
230+
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_CB_CLOSE');
231231
} else if (outResponse === 'reset') {
232232
requireSocketResetSupport();
233233
resetOrDestroy(request);
234-
throw new AbortError('Connection reset intentionally by rule');
234+
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_CB_RESET');
235235
} else {
236236
await writeResponseFromCallback(outResponse, response);
237237
}
@@ -595,11 +595,11 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
595595
if (modifiedReq.response === 'close') {
596596
const socket: net.Socket = (clientReq as any).socket;
597597
socket.end();
598-
throw new AbortError('Connection closed intentionally by rule');
598+
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_BREQ_CLOSE');
599599
} else if (modifiedReq.response === 'reset') {
600600
requireSocketResetSupport();
601601
resetOrDestroy(clientReq);
602-
throw new AbortError('Connection reset intentionally by rule');
602+
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_BREQ_RESET');
603603
} else {
604604
// The callback has provided a full response: don't passthrough at all, just use it.
605605
await writeResponseFromCallback(modifiedReq.response, clientRes);
@@ -970,7 +970,8 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
970970
}
971971

972972
throw new AbortError(
973-
`Connection ${modifiedRes === 'close' ? 'closed' : 'reset'} intentionally by rule`
973+
`Connection ${modifiedRes === 'close' ? 'closed' : 'reset'} intentionally by rule`,
974+
`E_RULE_BRES_${modifiedRes.toUpperCase()}`
974975
);
975976
}
976977

@@ -1197,7 +1198,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
11971198
? e.errors.map(e => e.message).join(', ')
11981199
: (e.message ?? e.code ?? e);
11991200

1200-
throw new AbortError(`Upstream connection error: ${errorMessage}`, e.code);
1201+
throw new AbortError(`Upstream connection error: ${errorMessage}`, e.code || 'E_MIRRORED_FAILURE');
12011202
} else {
12021203
e.statusCode = 502;
12031204
e.statusMessage = 'Error communicating with upstream server';
@@ -1328,7 +1329,7 @@ export class CloseConnectionHandler extends CloseConnectionHandlerDefinition {
13281329
async handle(request: OngoingRequest) {
13291330
const socket: net.Socket = (request as any).socket;
13301331
socket.end();
1331-
throw new AbortError('Connection closed intentionally by rule');
1332+
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_CLOSE');
13321333
}
13331334
}
13341335

@@ -1341,7 +1342,7 @@ export class ResetConnectionHandler extends ResetConnectionHandlerDefinition {
13411342
async handle(request: OngoingRequest) {
13421343
requireSocketResetSupport();
13431344
resetOrDestroy(request);
1344-
throw new AbortError('Connection reset intentionally by rule');
1345+
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_RESET');
13451346
}
13461347

13471348
/**

0 commit comments

Comments
 (0)