{"record":{"id":"4d54f2626d94cbdc","repo":"pydantic/monty","slug":"invalid-mode-char","errorCode":null,"errorMessage":"invalid mode: '${char}'","messagePattern":"invalid mode: '(.+?)'","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/monty-js/ts/types.ts","lineNumber":143,"sourceCode":"  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')\n  }\n}\n","sourceCodeStart":125,"sourceCodeEnd":159,"githubUrl":"https://github.com/pydantic/monty/blob/adc986b362e3961f407868cb118a99fe831b9e61/crates/monty-js/ts/types.ts#L125-L159","documentation":"The mode string contained a character that is not one of r/w/a/x/b/t/+. canonicalFileMode validates mode character-by-character and throws a TypeError naming the offending character for anything unrecognized.","triggerScenarios":"Passing modes with stray characters such as 'r w' (space), 're' (typo), 'R' (uppercase), or numerically-indexed characters from a bad config to MontyFileHandle's constructor or pushFileHandle.","commonSituations":"Typos in mode strings ('wt!' vs 'w'), uppercasing the whole mode ('W'), whitespace from template strings, or locale/formatting mistakes when building the mode dynamically.","solutions":["Check the quoted character in the message and remove it from the mode string","Use only characters from {r,w,a,b,t} (and note '+' and 'x' are separately rejected)","Trim/normalize the mode string before passing: `mode.trim().toLowerCase()`"],"exampleFix":"// before\nconst fh = new MontyFileHandle(path, ' r')\n// after\nconst fh = new MontyFileHandle(path, 'r')","handlingStrategy":"validation","validationCode":"if (typeof mode !== 'string' || !/^[rwabt+]*$/.test(mode)) throw new Error('mode contains unsupported characters')","typeGuard":"function isWellFormedMode(mode) { return typeof mode === 'string' && /^[rwabt+]*$/.test(mode) }","tryCatchPattern":"try { fh = new MontyFileHandle(path, mode) } catch (e) { if (e instanceof TypeError && e.message.startsWith(\"invalid mode: '\")) throw new Error(`bad mode config: ${mode}`) ; throw e }","preventionTips":["Trim and lowercase mode strings before use","Avoid building modes from templates that can inject whitespace","Validate user/config-supplied modes against a regex whitelist"],"tags":["typescript","file-mode","invalid-argument"],"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"}