Chrome DevTools for coding agents
chore: add sanity check to error enums (#2059)
yulunz3efd8c019416
diff --git a/scripts/update_metrics.ts b/scripts/update_metrics.tsindex 10c1da42..f6bb5ef0 100644--- a/scripts/update_metrics.ts+++ b/scripts/update_metrics.ts@@ -11,6 +11,7 @@ import {cliOptions,parseArguments,} from '../build/src/bin/chrome-devtools-mcp-cli-options.js';+import {ErrorCode} from '../build/src/telemetry/errors.js';import {getPossibleFlagMetrics,type FlagMetric,@@ -104,7 +105,34 @@ function writeFlagUsageMetrics() {);}+function validateErrorCodes(): void {+ // The compiled JavaScript object has both forward and backward mappings of the enum,+ // for example: { '0': 'ERROR_CODE_UNSPECIFIED', 'ERROR_CODE_UNSPECIFIED': 0 }.+ // This filters out the numeric keys.+ const stringKeysOnly = Object.entries(ErrorCode).filter(([key]) =>+ isNaN(Number(key)),+ );++ let expectedIndex = 0;+ for (const [key, index] of stringKeysOnly) {+ if (index !== expectedIndex) {+ throw new Error(+ `Error: ErrorCode enums must be sequentially numbered from 0.`,+ );+ }+ if (/_\d/.test(key)) {+ throw new Error(+ `Error: ErrorCode enum ${key} is invalid. No numbers should be preceded with an underscore.`,+ );+ }+ expectedIndex++;+ }++ console.log(`Successfully validated ${expectedIndex} ErrorCode enums.`);+}+function main() {+ validateErrorCodes();writeToolCallMetricsConfig();writeFlagUsageMetrics();}diff --git a/src/telemetry/errors.ts b/src/telemetry/errors.tsindex a91addf1..f1bd7970 100644--- a/src/telemetry/errors.ts+++ b/src/telemetry/errors.ts@@ -13,5 +13,5 @@export enum ErrorCode {ERROR_CODE_UNSPECIFIED = 0,ERROR_CODE_PERSISTENCE_FILE_READ_FAILED = 1,- ERROR_CODE_PERSISTENCE_FILE_SAVE_FAILED = 3,+ ERROR_CODE_PERSISTENCE_FILE_SAVE_FAILED = 2,}