{"record":{"id":"96e44913dcfcb04c","repo":"pydantic/monty","slug":"exclusive-creation-mode-is-not-supported","errorCode":null,"errorMessage":"exclusive creation mode is not supported","messagePattern":"exclusive creation mode is not supported","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/monty-js/ts/types.ts","lineNumber":133,"sourceCode":"    )\n  }\n}\n\n/** Canonicalizes the subset of Python file modes Monty supports. */\nexport function canonicalFileMode(mode: string): string {\n  if (mode.length === 0) {\n    throw new TypeError('Must have exactly one of create/read/write/append mode and at most one plus')\n  }\n\n  let action: string | undefined\n  let binary = false\n  let text = false\n  for (const char of mode) {\n    if (char === 'r' || char === 'w' || char === 'a') {\n      if (action !== undefined) throw new TypeError('must have exactly one of create/read/write/append mode')\n      action = char\n    } else if (char === 'x') {\n      throw new TypeError('exclusive creation mode is not supported')\n    } else if (char === 'b') {\n      if (binary) throw new TypeError('invalid mode: binary mode specified twice')\n      binary = true\n    } else if (char === 't') {\n      if (text) throw new TypeError('invalid mode: text mode specified twice')\n      text = true\n    } else if (char === '+') {\n      throw new TypeError(\"update modes ('+') are not yet supported\")\n    } else {\n      throw new TypeError(`invalid mode: '${char}'`)\n    }\n  }\n  if (binary && text) throw new TypeError(\"can't have text and binary mode at once\")\n  if (action === undefined) {\n    throw new TypeError('Must have exactly one of create/read/write/append mode and at most one plus')\n  }\n  return `${action}${binary ? 'b' : ''}`\n}","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty-js/ts/types.ts#L115-L151","documentation":"The file mode string passed to MontyFileHandle/open contained 'x', requesting exclusive creation (O_CREAT|O_EXCL). The JS bindings' file-mode subset does not implement exclusive creation, so canonicalFileMode rejects it with a TypeError before any host-side open happens.","triggerScenarios":"Calling MontyFileHandle's constructor or pushFileHandle with a mode string containing 'x', e.g. `new MontyFileHandle(path, 'x')`, `'wx'`, `'xb'`, or `'ax'`.","commonSituations":"Porting code that used Python's open(path, 'x') for create-if-not-exists semantics, or Node's fs open flag 'wx'; developers who want atomic file creation to avoid clobbering an existing file.","solutions":["Remove 'x' from the mode string and use 'w', 'r', or 'a' instead","Implement the existence check yourself: try opening/reading the file first and treat a file-not-found OsFunctionCall outcome as 'safe to create'","Wrap the open in a try/catch for TypeError and fall back to a non-exclusive mode"],"exampleFix":"// before\nconst fh = new MontyFileHandle('/mnt/data/lock', 'x')\n// after\nconst fh = new MontyFileHandle('/mnt/data/lock', 'w')","handlingStrategy":"validation","validationCode":"function isValidMode(mode) { return typeof mode === 'string' && /^[rwa][bt]?$/.test(mode) }","typeGuard":"function isSupportedFileMode(mode) { return typeof mode === 'string' && /^[rwa][bt]?$/.test(mode) }","tryCatchPattern":"try { fh = new MontyFileHandle(path, mode) } catch (e) { if (e instanceof TypeError && /exclusive creation/.test(e.message)) fh = new MontyFileHandle(path, mode.replace('x', 'w')) ; else throw e }","preventionTips":["Never use 'x' in modes for Monty file handles","Emulate create-if-not-exists by attempting a read first","Keep a whitelist of supported modes: r, w, a optionally with b or t"],"tags":["typescript","file-mode","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"adc986b362e3961f407868cb118a99fe831b9e61","analyzedAt":"2026-09-13T19:19:18.698Z","contentChangedAt":"2026-09-13T19:19:18.698Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}