fullstack-build/tslog

Bug: Crashes on `AbortController` aborts in Node

prescience-data opened this issue · 0 comments

Describe the bug
When the logger attempts to report an error thrown by a native Node AbortController, it appears to crash expecting a DOMException.

To Reproduce
Steps to reproduce the behavior:

const send = async <T>(request: T): Response => {
	const timeout = 10000 // 10s.
	const abort = new AbortController()
	
	// Abort request after timeout.
	const timer = setTimeout(() => {
	  abort.abort(`OpenAI request timed out.`)
	}, timeout)
	
	// Abortable request.
	return await fetch(
	  "api/with/long/timeout",
	  {
	    method: "POST",
	    body: JSON.stringify(request),
	    signal: abort.signal
	  }
	)
}

In the above case, TSLog will fatally error attempting to handle the uncaught abort signal.

However, if the abortable function is wrapped in a try/catch block with a native Node error, it appears to function correctly:

try {
	await send({ foo: true })
} catch(error) {
	logger.error(new Error(`Request error`))
}

Expected behavior
Without digging in to see if this is somewhat intentional or not, I would expect the logger to handle this type of error without fatal error (ie how other errors are handled successfully).

Error stack

node:internal/per_context/domexception:17
 const err = new Base();
             ^

TypeError: Value of "this" must be of DOMException
   at throwInvalidThisError (node:internal/per_context/domexception:17:15)
   at get name [as name] (node:internal/per_context/domexception:82:7)
   at formatError (node:internal/util/inspect:1362:20)
   at formatRaw (node:internal/util/inspect:986:14)
   at formatValue (node:internal/util/inspect:841:10)
   at inspect (node:internal/util/inspect:365:10)
   at formatWithOptionsInternal (node:internal/util/inspect:2273:40)
   at formatWithOptions (node:internal/util/inspect:2135:10)
   at transportFormatted (/home/<app>/node_modules/.pnpm/tslog@4.8.2/node_modules/tslog/dist/cjs/runtime/nodejs/index.js:98:44)
   at Logger.log (/home/<app>/node_modules/.pnpm/tslog@4.8.2/node_modules/tslog/dist/cjs/BaseLogger.js:121:34)
   at Logger.error (/home/<app>/node_modules/.pnpm/tslog@4.8.2/node_modules/tslog/dist/cjs/index.js:29:22)
   at <anonymous> (/home/<app>/src/run.ts:71:10)
   at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
 code: 'ERR_INVALID_THIS'
}


Node.js Version
v18.16.0

OS incl. Version
Ubuntu 22.04