mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 16:28:26 +00:00
refactor: consolidate duplicate utility functions (#12439)
* refactor: consolidate duplicate utility functions - Add escapeRegExp to src/utils.ts and remove 10 local duplicates - Rename bash-tools clampNumber to clampWithDefault (different signature) - Centralize formatError calls to use formatErrorMessage from infra/errors.ts - Re-export formatErrorMessage from cli/cli-utils.ts to preserve API * refactor: consolidate remaining escapeRegExp duplicates * refactor: consolidate sleep, stripAnsi, and clamp duplicates
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { SystemPresence } from "../infra/system-presence.js";
|
||||
import { formatErrorMessage } from "../infra/errors.js";
|
||||
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
|
||||
import { GatewayClient } from "./client.js";
|
||||
|
||||
@@ -26,13 +27,6 @@ export type GatewayProbeResult = {
|
||||
configSnapshot: unknown;
|
||||
};
|
||||
|
||||
function formatError(err: unknown): string {
|
||||
if (err instanceof Error) {
|
||||
return err.message;
|
||||
}
|
||||
return String(err);
|
||||
}
|
||||
|
||||
export async function probeGateway(opts: {
|
||||
url: string;
|
||||
auth?: GatewayProbeAuth;
|
||||
@@ -65,7 +59,7 @@ export async function probeGateway(opts: {
|
||||
mode: GATEWAY_CLIENT_MODES.PROBE,
|
||||
instanceId,
|
||||
onConnectError: (err) => {
|
||||
connectError = formatError(err);
|
||||
connectError = formatErrorMessage(err);
|
||||
},
|
||||
onClose: (code, reason) => {
|
||||
close = { code, reason };
|
||||
@@ -93,7 +87,7 @@ export async function probeGateway(opts: {
|
||||
settle({
|
||||
ok: false,
|
||||
connectLatencyMs,
|
||||
error: formatError(err),
|
||||
error: formatErrorMessage(err),
|
||||
close,
|
||||
health: null,
|
||||
status: null,
|
||||
|
||||
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { GatewayRequestHandlers } from "./types.js";
|
||||
import { getResolvedLoggerSettings } from "../../logging.js";
|
||||
import { clamp } from "../../utils.js";
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
@@ -15,10 +16,6 @@ const MAX_LIMIT = 5000;
|
||||
const MAX_BYTES = 1_000_000;
|
||||
const ROLLING_LOG_RE = /^openclaw-\d{4}-\d{2}-\d{2}\.log$/;
|
||||
|
||||
function clamp(value: number, min: number, max: number) {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
|
||||
function isRollingLogFile(file: string): boolean {
|
||||
return ROLLING_LOG_RE.test(path.basename(file));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user