mongodb/node-mongodb-native · error · MongoRuntimeError
An unexpected error type: ${typeof error}
Error message
An unexpected error type: ${typeof error} What it means
Thrown inside `Server.decorateCommandError` when a command fails with a value that is not an object, is null, or has no `name` property (server.ts:456). The driver expects errors to be Error-like objects so it can decorate them with retry/label metadata; a primitive (string/number/boolean) or null error cannot be processed.
Source
Thrown at src/sdam/server.ts:457
markServerUnknown(this, error);
} else if (connection) {
this.pool.clear({ serviceId: connection.serviceId });
}
}
}
/**
* Ensure that error is properly decorated and internal state is updated before throwing
* @internal
*/
private decorateCommandError(
connection: Connection,
cmd: Document,
options: CommandOptions | GetMoreOptions | undefined,
error: unknown
): Error {
if (typeof error !== 'object' || error == null || !('name' in error)) {
throw new MongoRuntimeError('An unexpected error type: ' + typeof error);
}
if (error.name === 'AbortError' && 'cause' in error && error.cause instanceof MongoError) {
error = error.cause;
}
if (!(error instanceof MongoError)) {
// Node.js or some other error we have not special handling for
return error as Error;
}
if (connectionIsStale(this.pool, connection)) {
return error;
}
const session = options?.session;
if (error instanceof MongoNetworkError) {
if (session && !session.hasEnded && session.serverSession) {View on GitHub (pinned to 3366c21a63)
Solutions
- Update to the latest driver patch release; many such paths were hardened.
- Audit any custom auth, monitoring, or stream code you inject for non-Error rejections and wrap them in Error instances.
- Report the issue with the command and stack trace if it reproduces on the latest version.
Example fix
// before (custom transport)
socket.on('error', () => { throw 'broken pipe'; });
// after
socket.on('error', (e) => { throw new Error('broken pipe: ' + e?.message); }); Defensive patterns
Strategy: try-catch
Type guard
function isErrorLike(e) {
return typeof e === 'object' && e !== null && 'name' in e;
} Try / catch
try {
await coll.findOne(filter);
} catch (e) {
if (!isErrorLike(e)) {
// log and rethrow a normalized Error
throw new Error('Non-Error thrown from driver: ' + String(e));
}
throw e;
} Prevention
- Always reject/throw Error instances from custom code that touches the driver.
- Keep the driver updated to benefit from hardened error paths.
- Log non-Error rejections globally via process.on('unhandledRejection').
When it happens
Trigger: A promise rejection deep in the connection or auth layer with a non-Error value (`throw 'timeout'`, `reject(42)`); a custom auth plugin or stream transform that rejects with a string; a null error propagated from a closed socket handler.
Common situations: Third-party auth/transport code throwing primitives; an older driver version with an unfixed bug; corrupted/incomplete error objects from native addons.
Related errors
- Unexpected HostAddress ${JSON.stringify(hostAddress)}
- ServerSessionPool requires a MongoClient
- Unrecognized options
- Unreachable. If you are seeing this error, please file a tic
- Attempted illegal state transition from [${this.state}] to [
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/f59540496a235a1d.json.
Report an issue: GitHub.