Skip to content

Commit

Permalink
Add codes & tags for intentional rule abort failures
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Feb 24, 2025
1 parent efdd1e9 commit 949d75d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/rules/passthrough-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
CallbackRequestResult,
CallbackResponseMessageResult
} from './requests/request-handler-definitions';
import { AbortError } from './requests/request-handlers';
import {
CADefinition,
PassThroughLookupOptions
Expand Down Expand Up @@ -413,5 +414,9 @@ export function buildUpstreamErrorTags(e: ErrorLike) {
tags.push('passthrough-tls-error:ssl-alert-' + tlsAlertMatch[1]);
}

if (e instanceof AbortError) {
tags.push('passthrough-error:mockttp-abort')
}

return tags;
}
19 changes: 10 additions & 9 deletions src/rules/requests/request-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class AbortError extends TypedError {

constructor(
message: string,
readonly code?: string
readonly code: string
) {
super(message);
}
Expand Down Expand Up @@ -227,11 +227,11 @@ export class CallbackHandler extends CallbackHandlerDefinition {

if (outResponse === 'close') {
(request as any).socket.end();
throw new AbortError('Connection closed intentionally by rule');
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_CB_CLOSE');
} else if (outResponse === 'reset') {
requireSocketResetSupport();
resetOrDestroy(request);
throw new AbortError('Connection reset intentionally by rule');
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_CB_RESET');
} else {
await writeResponseFromCallback(outResponse, response);
}
Expand Down Expand Up @@ -595,11 +595,11 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
if (modifiedReq.response === 'close') {
const socket: net.Socket = (clientReq as any).socket;
socket.end();
throw new AbortError('Connection closed intentionally by rule');
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_BREQ_CLOSE');
} else if (modifiedReq.response === 'reset') {
requireSocketResetSupport();
resetOrDestroy(clientReq);
throw new AbortError('Connection reset intentionally by rule');
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_BREQ_RESET');
} else {
// The callback has provided a full response: don't passthrough at all, just use it.
await writeResponseFromCallback(modifiedReq.response, clientRes);
Expand Down Expand Up @@ -970,7 +970,8 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
}

throw new AbortError(
`Connection ${modifiedRes === 'close' ? 'closed' : 'reset'} intentionally by rule`
`Connection ${modifiedRes === 'close' ? 'closed' : 'reset'} intentionally by rule`,
`E_RULE_BRES_${modifiedRes.toUpperCase()}`
);
}

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

throw new AbortError(`Upstream connection error: ${errorMessage}`, e.code);
throw new AbortError(`Upstream connection error: ${errorMessage}`, e.code || 'E_MIRRORED_FAILURE');
} else {
e.statusCode = 502;
e.statusMessage = 'Error communicating with upstream server';
Expand Down Expand Up @@ -1328,7 +1329,7 @@ export class CloseConnectionHandler extends CloseConnectionHandlerDefinition {
async handle(request: OngoingRequest) {
const socket: net.Socket = (request as any).socket;
socket.end();
throw new AbortError('Connection closed intentionally by rule');
throw new AbortError('Connection closed intentionally by rule', 'E_RULE_CLOSE');
}
}

Expand All @@ -1341,7 +1342,7 @@ export class ResetConnectionHandler extends ResetConnectionHandlerDefinition {
async handle(request: OngoingRequest) {
requireSocketResetSupport();
resetOrDestroy(request);
throw new AbortError('Connection reset intentionally by rule');
throw new AbortError('Connection reset intentionally by rule', 'E_RULE_RESET');
}

/**
Expand Down

0 comments on commit 949d75d

Please sign in to comment.