Skip to content

Commit

Permalink
Check type of values
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Dec 19, 2024
1 parent b2af456 commit 62e4912
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions library/agent/context/markUnsafe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,19 @@ t.test("it works", async () => {
"markUnsafe(...) failed to serialize the data",
"markUnsafe(...) was called without any data.",
]);

runWithContext(createContext(), () => {
markUnsafe(1, true, null, undefined, () => {}, Symbol("test"));
});
t.same(logs, [
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
"markUnsafe(...) failed to serialize the data",
"markUnsafe(...) was called without any data.",
"markUnsafe(...) expects an object, array, or string. Received: number",
"markUnsafe(...) expects an object, array, or string. Received: boolean",
"markUnsafe(...) expects an object, array, or string. Received: null",
"markUnsafe(...) expects an object, array, or string. Received: undefined",
"markUnsafe(...) expects an object, array, or string. Received: function",
"markUnsafe(...) expects an object, array, or string. Received: symbol",
]);
});
14 changes: 14 additions & 0 deletions library/agent/context/markUnsafe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isPlainObject } from "../../helpers/isPlainObject";
import { getInstance } from "../AgentSingleton";
import { Context, updateContext } from "../Context";
import { ContextStorage } from "./ContextStorage";
Expand All @@ -22,6 +23,19 @@ export function markUnsafe(...data: unknown[]) {
}

for (const item of data) {
if (
!isPlainObject(item) &&
!Array.isArray(item) &&
typeof item !== "string"
) {
const type = item === null ? "null" : typeof item;
// eslint-disable-next-line no-console
console.warn(
`markUnsafe(...) expects an object, array, or string. Received: ${type}`
);
continue;
}

addPayloadToContext(context, item);
}
}
Expand Down

0 comments on commit 62e4912

Please sign in to comment.