Skip to content

Commit 200d212

Browse files
committed
fixes the e.replace is not a function error happening on production in Firefox during createInstance
in js, any object can be thrown. that is why the `catch` block `error` variable has type `unknown` in typescript. in optimizely, there are a few catch blocks where the error variable is directly logged as message. in the logger, the type of the public named logging functions is `string | Error` which is not correct since it could actually be any type. if it is an object that is not an instance of type Error, this will cause a throw in `internalLog -> format -> sprintf` because sprintf is calling `.replace` on the message, which throws with `e.replace is not a function`. we have seen this exact error a lot in production. so logging causes a throw which causes createInstance to fail, causing further issues. this PR fixes the issue by converting non-string and non-Error log messages to string first. fixes #719
1 parent ac7e86a commit 200d212

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

packages/logging/src/logger.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,25 @@ class OptimizelyLogger implements LoggerFacade {
264264
}
265265
}
266266

267-
private namedLog(level: LogLevel, message: string | Error, splat: any[]): void {
267+
private namedLog(level: LogLevel, message: unknown, splat: any[]): void {
268268
let error: Error | undefined
269269

270270
if (message instanceof Error) {
271271
error = message
272-
message = error.message
273272
this.internalLog(level, {
274273
error,
275-
message,
274+
message: error.message,
275+
splat,
276+
})
277+
return
278+
}
279+
280+
// sometimes values other than Error are thrown and logged with the optimizely logger,
281+
// we need to catch this case here to avoid a follow-up issue when calling format/sprintf with something else than a string for the message
282+
if (typeof message !== 'string') {
283+
this.internalLog(level, {
284+
error,
285+
message: `${message}`,
276286
splat,
277287
})
278288
return

0 commit comments

Comments
 (0)