{"record":{"id":"c4ab145e7d56f854","repo":"mongodb/node-mongodb-native","slug":"profiling-level-must-be-one-of-enumtostring-pro","errorCode":null,"errorMessage":"Profiling level must be one of \"${enumToString(ProfilingLevel)}\"","messagePattern":"Profiling level must be one of \"(.+?)\"","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/operations/set_profiling_level.ts","lineNumber":61,"sourceCode":"        break;\n      default:\n        this.profile = 0;\n        break;\n    }\n\n    this.level = level;\n  }\n\n  override get commandName() {\n    return 'profile' as const;\n  }\n\n  override buildCommandDocument(_connection: Connection): Document {\n    const level = this.level;\n\n    if (!levelValues.has(level)) {\n      // TODO(NODE-3483): Determine error to put here\n      throw new MongoInvalidArgumentError(\n        `Profiling level must be one of \"${enumToString(ProfilingLevel)}\"`\n      );\n    }\n\n    return { profile: this.profile };\n  }\n\n  override handleOk(\n    _response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>\n  ): ProfilingLevel {\n    return this.level;\n  }\n}\n","sourceCodeStart":43,"sourceCodeEnd":75,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/operations/set_profiling_level.ts#L43-L75","documentation":"Thrown as a MongoInvalidArgumentError by SetProfilingLevelOperation.buildCommandDocument when the level string is not one of 'off', 'slow_only', or 'all' (the levelValues set). The valid values come from the ProfilingLevel const object; the message lists them via enumToString. The constructor's switch defaults unknown levels to profile 0, but buildCommandDocument re-validates and rejects before sending.","triggerScenarios":"Calling db.setProfilingLevel('off') with a typo or unsupported value, e.g. db.setProfilingLevel('slow'), db.setProfilingLevel('none'), or db.setProfilingLevel(1) (number instead of string). The check is `!levelValues.has(level)` at set_profiling_level.ts:59.","commonSituations":"Using legacy/abbreviated level names from older APIs; passing a numeric level directly; passing an untyped value from configuration that is neither off/slow_only/all.","solutions":["Use the exported ProfilingLevel constants: db.setProfilingLevel(ProfilingLevel.slowOnly).","Pass exactly one of the strings 'off', 'slow_only', or 'all'.","Normalize configuration input to one of the three valid strings before calling."],"exampleFix":"// before\nawait db.setProfilingLevel('slow'); // typo -> error\n\n// after\nconst { ProfilingLevel } = require('mongodb');\nawait db.setProfilingLevel(ProfilingLevel.slowOnly); // 'slow_only'","handlingStrategy":"validation","validationCode":"import { ProfilingLevel } from 'mongodb';\n\nconst VALID = new Set<string>(Object.values(ProfilingLevel)); // 'off','slow_only','all'\nif (!VALID.has(level)) {\n  throw new TypeError(`Profiling level must be one of: ${[...VALID].join(', ')}`);\n}\nawait db.setProfilingLevel(level as any);","typeGuard":"function isProfilingLevel(v: unknown): v is 'off' | 'slow_only' | 'all' {\n  return v === 'off' || v === 'slow_only' || v === 'all';\n}","tryCatchPattern":null,"preventionTips":["Use the ProfilingLevel constants (ProfilingLevel.off/slowOnly/all) rather than literal strings.","Normalize config input to one of the three valid values before calling setProfilingLevel.","Do not pass numeric levels; the API expects the string forms."],"tags":["validation","profiling","argument","configuration"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}