{"record":{"id":"bfd1ec41805f7733","repo":"pydantic/monty","slug":"invalid-mode-text-mode-specified-twice","errorCode":null,"errorMessage":"invalid mode: text mode specified twice","messagePattern":"invalid mode: text mode specified twice","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/monty-js/ts/types.ts","lineNumber":138,"sourceCode":"export 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}\n\n/** Validates that a file position can cross the JavaScript boundary exactly. */\nexport function validateFilePosition(position: unknown): asserts position is number {\n  if (typeof position !== 'number' || !Number.isSafeInteger(position) || position < 0) {\n    throw new TypeError('MontyFileHandle position must be a non-negative safe integer')","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty-js/ts/types.ts#L120-L156","documentation":"The mode string contained 't' more than once (e.g. 'rtt'). Text mode can only be specified once, and canonicalFileMode rejects duplicates with a TypeError, matching CPython's validation of mode strings.","triggerScenarios":"Passing a mode like 'rt', 'wt' where 't' appears twice ('rtt', 'wtt') to MontyFileHandle's constructor or pushFileHandle.","commonSituations":"Programmatic mode assembly that appends 't' as default text mode even when the user mode already included it.","solutions":["Ensure 't' appears at most once in the mode string","Drop 't' entirely — text is the default when 'b' is absent","Normalize the mode before passing it: strip duplicate 't' characters"],"exampleFix":"// before\nconst mode = userMode + 't' // userMode was already 'rt'\n// after\nconst mode = userMode.includes('t') || userMode.includes('b') ? userMode : userMode + 't'","handlingStrategy":"validation","validationCode":"if (typeof mode === 'string' && (mode.match(/t/g) || []).length > 1) throw new Error('mode has duplicate t')","typeGuard":"function hasSingleT(mode) { return typeof mode === 'string' && (mode.match(/t/g) || []).length <= 1 }","tryCatchPattern":"try { fh = new MontyFileHandle(path, mode) } catch (e) { if (e instanceof TypeError && e.message.includes(\"text mode specified twice\")) fh = new MontyFileHandle(path, mode.replace(/t(?=.*t)/, '')) ; else throw e }","preventionTips":["Remember 't' is optional — text is the default","Only append 't' when neither 'b' nor 't' is already present","Centralize mode construction in one helper function"],"tags":["typescript","file-mode","duplicate-flag"],"backgroundTag":"invalid-argument-format","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"}