{"id":"f59540496a235a1d","repo":"mongodb/node-mongodb-native","slug":"an-unexpected-error-type-typeof-error","errorCode":null,"errorMessage":"An unexpected error type: ${typeof error}","messagePattern":"An unexpected error type: (.+?)","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sdam/server.ts","lineNumber":457,"sourceCode":"        markServerUnknown(this, error);\n      } else if (connection) {\n        this.pool.clear({ serviceId: connection.serviceId });\n      }\n    }\n  }\n\n  /**\n   * Ensure that error is properly decorated and internal state is updated before throwing\n   * @internal\n   */\n  private decorateCommandError(\n    connection: Connection,\n    cmd: Document,\n    options: CommandOptions | GetMoreOptions | undefined,\n    error: unknown\n  ): Error {\n    if (typeof error !== 'object' || error == null || !('name' in error)) {\n      throw new MongoRuntimeError('An unexpected error type: ' + typeof error);\n    }\n\n    if (error.name === 'AbortError' && 'cause' in error && error.cause instanceof MongoError) {\n      error = error.cause;\n    }\n\n    if (!(error instanceof MongoError)) {\n      // Node.js or some other error we have not special handling for\n      return error as Error;\n    }\n\n    if (connectionIsStale(this.pool, connection)) {\n      return error;\n    }\n\n    const session = options?.session;\n    if (error instanceof MongoNetworkError) {\n      if (session && !session.hasEnded && session.serverSession) {","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/sdam/server.ts#L439-L475","documentation":"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.","triggerScenarios":"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.","commonSituations":"Third-party auth/transport code throwing primitives; an older driver version with an unfixed bug; corrupted/incomplete error objects from native addons.","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."],"exampleFix":"// before (custom transport)\nsocket.on('error', () => { throw 'broken pipe'; });\n// after\nsocket.on('error', (e) => { throw new Error('broken pipe: ' + e?.message); });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isErrorLike(e) {\n  return typeof e === 'object' && e !== null && 'name' in e;\n}","tryCatchPattern":"try {\n  await coll.findOne(filter);\n} catch (e) {\n  if (!isErrorLike(e)) {\n    // log and rethrow a normalized Error\n    throw new Error('Non-Error thrown from driver: ' + String(e));\n  }\n  throw e;\n}","preventionTips":["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')."],"tags":["error-handling","internal","runtime"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}