{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/set_profiling_level.ts#L43-L75","documentation":"Thrown by SetProfilingLevelOperation.buildCommandDocument when the level string is not one of 'off', 'slow_only', 'all' (the exact values of the ProfilingLevel const). The constructor's switch falls through to profile=0 for unknown levels, and buildCommandDocument then rejects the unknown level with a MongoInvalidArgumentError before issuing the command. Note the values use snake_case ('slow_only'), not 'slow' or camelCase.","triggerScenarios":"Calling db.setProfilingLevel('slow'), db.setProfilingLevel('on'), db.setProfilingLevel(1), or any string not in the allowed set. Passing the numeric 0/1/2 instead of the string level. Passing a typo like 'alll' or 'of'.","commonSituations":"Developers assuming 'slow' or numeric levels work; copy-paste from shell examples that use numbers; confusion between the public string API and the internal numeric profile value.","solutions":["Use the exported ProfilingLevel constants: ProfilingLevel.off, ProfilingLevel.slowOnly, ProfilingLevel.all (which resolve to 'off'/'slow_only'/'all').","Pass exactly one of the strings 'off', 'slow_only', 'all'.","Do not pass numbers - the public API takes the string level, not 0/1/2.","Validate the value against the allowed set before calling in dynamic/config-driven code."],"exampleFix":"// before\nawait db.setProfilingLevel('slow'); // or 1\n// after\nimport { ProfilingLevel } from 'mongodb';\nawait db.setProfilingLevel(ProfilingLevel.slowOnly); // 'slow_only'","handlingStrategy":"validation","validationCode":"import { ProfilingLevel } from 'mongodb';\nconst allowed = new Set<string>(Object.values(ProfilingLevel)); // 'off','slow_only','all'\nif (!allowed.has(level)) throw new Error(`level must be one of ${[...allowed].join(',')}`);\nawait db.setProfilingLevel(level as any);","typeGuard":"import { ProfilingLevel } from 'mongodb';\nfunction isProfilingLevel(v: unknown): v is (typeof ProfilingLevel)[keyof typeof ProfilingLevel] {\n  return typeof v === 'string' && Object.values(ProfilingLevel).includes(v as any);\n}","tryCatchPattern":null,"preventionTips":["Always use the ProfilingLevel constants instead of string literals or numbers.","Validate config-supplied level strings against the enum values.","Remember the API takes strings ('slow_only'), not numbers."],"tags":["profiling","admin","validation","argument-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}