Skip to content

Commit da404c0

Browse files
author
Hweinstock
committed
fix(tel): ensure commander errors recieve the command path before exiting
1 parent 5981167 commit da404c0

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/router/router.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,7 @@ function attachAction(
7777
const command = actionArgs[actionArgs.length - 1] as Command;
7878
const merged = command.optsWithGlobals();
7979

80-
const telemetryAttributesRecorder = ctx.value(TelemetryAttributesRecorderKey);
81-
const commandPath = ctx.require(PathKey);
82-
83-
telemetryAttributesRecorder?.record({
84-
command_path: commandPath,
85-
});
80+
recordCommandPath(ctx);
8681

8782
// Inherited group/global flags -> context (typed, read via ctx.value(key)).
8883
let leafCtx = ctx.withValue(CommandKey, command);
@@ -102,6 +97,11 @@ function globalFlagsOf(node: Handler): GlobalFlag[] {
10297
return node.flags().filter((f): f is GlobalFlag => "id" in f);
10398
}
10499

100+
/** Add the command path to active command run metric **/
101+
function recordCommandPath(ctx: Context): void {
102+
ctx.value(TelemetryAttributesRecorderKey)?.record({ command_path: ctx.value(PathKey) });
103+
}
104+
105105
// compile walks the Handler tree into a Commander Command tree.
106106
//
107107
// `stack` is the accumulated middleware declared by ancestors. A node's own
@@ -120,7 +120,7 @@ export function compile(
120120
stack: Middleware[] = [],
121121
inheritedGlobals: GlobalFlag[] = [],
122122
): Command {
123-
const c = new Command(node.name()).exitOverride();
123+
const c = new Command(node.name());
124124
c.description(node.description());
125125

126126
const ownFlags = node.flags();
@@ -141,6 +141,12 @@ export function compile(
141141
const newPath = `${path}/${node.name()}`;
142142
ctx = ctx.withValue(PathKey, newPath);
143143

144+
// commander may fail on invalid flags before we are able to record on the happy path, so we must record here as well
145+
c.exitOverride((e) => {
146+
recordCommandPath(ctx);
147+
throw e;
148+
});
149+
144150
const children = node.children();
145151
if (children.length > 0) {
146152
// attaching both children and subcommands leads to ambiguity.

0 commit comments

Comments
 (0)