mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-08 21:09:23 +08:00
chore: centralizing warning filters
This commit is contained in:
25
openclaw.mjs
25
openclaw.mjs
@@ -11,13 +11,36 @@ if (module.enableCompileCache && !process.env.NODE_DISABLE_COMPILE_CACHE) {
|
||||
}
|
||||
}
|
||||
|
||||
const isModuleNotFoundError = (err) =>
|
||||
err && typeof err === "object" && "code" in err && err.code === "ERR_MODULE_NOT_FOUND";
|
||||
|
||||
const installProcessWarningFilter = async () => {
|
||||
// Keep bootstrap warnings consistent with the TypeScript runtime.
|
||||
for (const specifier of ["./dist/warning-filter.js", "./dist/warning-filter.mjs"]) {
|
||||
try {
|
||||
const mod = await import(specifier);
|
||||
if (typeof mod.installProcessWarningFilter === "function") {
|
||||
mod.installProcessWarningFilter();
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
if (isModuleNotFoundError(err)) {
|
||||
continue;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await installProcessWarningFilter();
|
||||
|
||||
const tryImport = async (specifier) => {
|
||||
try {
|
||||
await import(specifier);
|
||||
return true;
|
||||
} catch (err) {
|
||||
// Only swallow missing-module errors; rethrow real runtime errors.
|
||||
if (err && typeof err === "object" && "code" in err && err.code === "ERR_MODULE_NOT_FOUND") {
|
||||
if (isModuleNotFoundError(err)) {
|
||||
return false;
|
||||
}
|
||||
throw err;
|
||||
|
||||
89
src/infra/warning-filter.test.ts
Normal file
89
src/infra/warning-filter.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { installProcessWarningFilter, shouldIgnoreWarning } from "./warning-filter.js";
|
||||
|
||||
const warningFilterKey = Symbol.for("openclaw.warning-filter");
|
||||
|
||||
function resetWarningFilterInstallState(): void {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[warningFilterKey]?: { installed: boolean };
|
||||
};
|
||||
delete globalState[warningFilterKey];
|
||||
}
|
||||
|
||||
describe("warning filter", () => {
|
||||
beforeEach(() => {
|
||||
resetWarningFilterInstallState();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
resetWarningFilterInstallState();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("suppresses known deprecation and experimental warning signatures", () => {
|
||||
expect(
|
||||
shouldIgnoreWarning({
|
||||
name: "DeprecationWarning",
|
||||
code: "DEP0040",
|
||||
message: "The punycode module is deprecated.",
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
shouldIgnoreWarning({
|
||||
name: "DeprecationWarning",
|
||||
code: "DEP0060",
|
||||
message: "The `util._extend` API is deprecated.",
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
shouldIgnoreWarning({
|
||||
name: "ExperimentalWarning",
|
||||
message: "SQLite is an experimental feature and might change at any time",
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps unknown warnings visible", () => {
|
||||
expect(
|
||||
shouldIgnoreWarning({
|
||||
name: "DeprecationWarning",
|
||||
code: "DEP9999",
|
||||
message: "Totally new warning",
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("installs once and only writes unsuppressed warnings", () => {
|
||||
let warningHandler: ((warning: Error & { code?: string; message?: string }) => void) | null =
|
||||
null;
|
||||
const onSpy = vi.spyOn(process, "on").mockImplementation(((event, handler) => {
|
||||
if (event === "warning") {
|
||||
warningHandler = handler as (warning: Error & { code?: string; message?: string }) => void;
|
||||
}
|
||||
return process;
|
||||
}) as typeof process.on);
|
||||
const writeSpy = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
|
||||
|
||||
installProcessWarningFilter();
|
||||
installProcessWarningFilter();
|
||||
|
||||
expect(onSpy).toHaveBeenCalledTimes(1);
|
||||
expect(warningHandler).not.toBeNull();
|
||||
|
||||
warningHandler?.({
|
||||
name: "DeprecationWarning",
|
||||
code: "DEP0060",
|
||||
message: "The `util._extend` API is deprecated.",
|
||||
toString: () => "suppressed",
|
||||
} as Error & { code?: string; message?: string });
|
||||
expect(writeSpy).not.toHaveBeenCalled();
|
||||
|
||||
warningHandler?.({
|
||||
name: "Warning",
|
||||
message: "Visible warning",
|
||||
stack: "Warning: visible",
|
||||
toString: () => "visible",
|
||||
} as Error & { code?: string; message?: string });
|
||||
expect(writeSpy).toHaveBeenCalledWith("Warning: visible\n");
|
||||
});
|
||||
});
|
||||
40
src/infra/warning-filter.ts
Normal file
40
src/infra/warning-filter.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
const warningFilterKey = Symbol.for("openclaw.warning-filter");
|
||||
|
||||
export type ProcessWarning = Error & {
|
||||
code?: string;
|
||||
name?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export function shouldIgnoreWarning(warning: ProcessWarning): boolean {
|
||||
if (warning.code === "DEP0040" && warning.message?.includes("punycode")) {
|
||||
return true;
|
||||
}
|
||||
if (warning.code === "DEP0060" && warning.message?.includes("util._extend")) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
warning.name === "ExperimentalWarning" &&
|
||||
warning.message?.includes("SQLite is an experimental feature")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function installProcessWarningFilter(): void {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[warningFilterKey]?: { installed: boolean };
|
||||
};
|
||||
if (globalState[warningFilterKey]?.installed) {
|
||||
return;
|
||||
}
|
||||
globalState[warningFilterKey] = { installed: true };
|
||||
|
||||
process.on("warning", (warning: ProcessWarning) => {
|
||||
if (shouldIgnoreWarning(warning)) {
|
||||
return;
|
||||
}
|
||||
process.stderr.write(`${warning.stack ?? warning.toString()}\n`);
|
||||
});
|
||||
}
|
||||
@@ -1,40 +1 @@
|
||||
const warningFilterKey = Symbol.for("openclaw.warning-filter");
|
||||
|
||||
type Warning = Error & {
|
||||
code?: string;
|
||||
name?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
function shouldIgnoreWarning(warning: Warning): boolean {
|
||||
if (warning.code === "DEP0040" && warning.message?.includes("punycode")) {
|
||||
return true;
|
||||
}
|
||||
if (warning.code === "DEP0060" && warning.message?.includes("util._extend")) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
warning.name === "ExperimentalWarning" &&
|
||||
warning.message?.includes("SQLite is an experimental feature")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function installProcessWarningFilter(): void {
|
||||
const globalState = globalThis as typeof globalThis & {
|
||||
[warningFilterKey]?: { installed: boolean };
|
||||
};
|
||||
if (globalState[warningFilterKey]?.installed) {
|
||||
return;
|
||||
}
|
||||
globalState[warningFilterKey] = { installed: true };
|
||||
|
||||
process.on("warning", (warning: Warning) => {
|
||||
if (shouldIgnoreWarning(warning)) {
|
||||
return;
|
||||
}
|
||||
process.stderr.write(`${warning.stack ?? warning.toString()}\n`);
|
||||
});
|
||||
}
|
||||
export { installProcessWarningFilter } from "./warning-filter.js";
|
||||
|
||||
@@ -17,6 +17,12 @@ export default defineConfig([
|
||||
fixedExtension: false,
|
||||
platform: "node",
|
||||
},
|
||||
{
|
||||
entry: "src/infra/warning-filter.ts",
|
||||
env,
|
||||
fixedExtension: false,
|
||||
platform: "node",
|
||||
},
|
||||
{
|
||||
entry: "src/plugin-sdk/index.ts",
|
||||
outDir: "dist/plugin-sdk",
|
||||
|
||||
Reference in New Issue
Block a user