{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/sdam/server.ts#L439-L475","documentation":"Thrown by Server.decorateCommandError() (src/sdam/server.ts:457) as MongoRuntimeError when the error passed in is not an object, is null, or lacks a 'name' property — i.e. an error value that violates the expected Error shape. This is an internal invariant: the driver expects any caught failure to be an Error-like object. Encountering it usually indicates a bug in the driver, a thrown primitive, or an unusual runtime/host environment.","triggerScenarios":"A connection or wire-protocol handler receives a thrown string, number, or null instead of an Error object — possible from native bindings, a misbehaving custom auth plugin, or a driver bug.","commonSituations":"Native module (kerberos, zstd, client-encryption) throwing a non-Error value, Promise rejection with a primitive, or driver internals mishandling an edge case.","solutions":["Report the issue to the driver maintainers with a reproduction; this is typically not caller-fixable.","Ensure any custom auth/compression plugins throw real Error objects.","Upgrade the driver and native bindings to the latest versions.","Wrap throw sites in your own code to emit Error instances."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isErrorLike(v): v is Error {\n  return v != null && typeof v === 'object' && 'name' in v;\n}","tryCatchPattern":"try {\n  await collection.findOne({});\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /unexpected error type/.test(e.message)) {\n    // log full context; this is likely a driver/native bug\n  }\n  throw e;\n}","preventionTips":["Keep native bindings (kerberos, zstd, client-encryption) at versions compatible with the driver.","Ensure custom plugins always throw Error objects, not primitives.","Report reproductions of this error to the driver maintainers."],"tags":["internal","runtime","invariant","error-handling"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}