Somewhere in most front-end codebases there is a line like this:
logger.error('checkout failed', JSON.stringify({ err }))
And somewhere in the log store there are a few hundred entries that read checkout failed {"err":{}}. Nobody notices until the day a customer says the payment page broke, and the one record of what happened says nothing at all.
Why an Error serialises to nothing
JSON.stringify only writes an object's own enumerable properties. The ECMAScript specification's Error constructor creates the message with CreateNonEnumerableDataPropertyOrThrow(obj, "message", messageString), and cause is installed the same way. Non-enumerable, by design, so the message is invisible to JSON.stringify, to Object.keys and to spread. stack is not in the specification at all, and every engine puts it somewhere different. That last part is where the usual fix comes apart.
The fix everybody copies
The answer you find first is to pass the property names explicitly:
JSON.stringify(err, Object.getOwnPropertyNames(err))
I ran it in Chrome 157, Firefox 146 and the JavaScriptCore engine that ships with macOS, on new Error('Payment failed', { cause: { code: 402 } }). Three different results:
-
Chrome:
stack,message,cause. The stack survives. -
JavaScriptCore:
message,cause,line,column,sourceURL,stack. The stack survives. -
Firefox:
fileName,lineNumber,columnNumber,message,cause. No stack. In Firefox,stackis an accessor onError.prototype, not a property of the error, sogetOwnPropertyNamesnever lists it.
So the same line gives you a stack trace for Chrome users and a file name and line number for Firefox users, and whichever browser you test in decides which one you think you have.
Two more holes in it
The cause comes back empty in every engine. In all three, the cause above serialised as {}. A replacer array is not a list of keys for the top-level object. The specification's SerializeJSONObject uses it as the key list at every level, so the nested object is filtered to keys named stack, message and so on, and { code: 402 } has none of them. The one field that said why the payment failed is the one that disappears.
A DOMException gives you nothing at all. AbortError, NotAllowedError, QuotaExceededError and the rest are DOMExceptions, and their name and message are Web IDL attributes, which live as accessors on the prototype. On new DOMException('The user aborted a request.', 'AbortError'), getOwnPropertyNames returned an empty array in both Chrome and Firefox, and the "fixed" stringify returned {}. In Firefox, the abort reason from a real AbortController did the same. Chrome's own abort reason carries an own stack, so there you get the stack text and nothing else.
That is the class of failure most worth reading in a front-end log: storage quota, a permission refused, a request cancelled. They are the ones serialised to {}.
What to log instead
Read the properties by name. A getter works fine when you ask for it directly; it is only enumeration that skips it.
function describeError(err, depth = 0) {
if (err === null || typeof err !== 'object') {
return { thrown: String(err) } // someone threw a string
}
if (typeof err.message !== 'string') {
return err // a plain object as cause: keep it as is
}
const out = { name: err.name, message: err.message, stack: err.stack }
if (err.code !== undefined) out.code = err.code
if (err.cause !== undefined && depth < 3) {
out.cause = describeError(err.cause, depth + 1)
}
return out
}
logger.error('checkout failed', JSON.stringify(describeError(err)))
On the same three inputs this gives the stack in Firefox, the { code: 402 } cause intact, and AbortError with its message for the DOMException. The depth limit is there because a cause chain can point back at itself.
The general version
A log line is a bug report written by your code instead of a person, and it fails the same way a person's does: it records what was easy to record, not what you will need. The time to find out that it says {} is before the incident, so pick one catch block today, throw a DOMException through it in Firefox, and read what lands in the log.
Top comments (0)