{"record":{"id":"67002ab60c657c9d","repo":"sickn33/agentic-awesome-skills","slug":"age-cannot-be-negative","errorCode":null,"errorMessage":"Age cannot be negative","messagePattern":"Age cannot be negative","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"skills/fp-pragmatic/SKILL.md","lineNumber":114,"sourceCode":"\n**Plain language translation:**\n- `O.fromNullable(x)` = \"wrap this value, treating null/undefined as 'nothing'\"\n- `O.flatMap(fn)` = \"if we have something, apply this function\"\n- `O.getOrElse(() => default)` = \"unwrap, or use this default if nothing\"\n\n### 3. Either: Make Errors Explicit\n\nStop throwing exceptions for expected failures. Return errors as values.\n\n```typescript\nimport * as E from 'fp-ts/Either'\nimport { pipe } from 'fp-ts/function'\n\n// Before: Hidden failure mode\nfunction parseAge(input: string): number {\n  const age = parseInt(input, 10)\n  if (isNaN(age)) throw new Error('Invalid age')\n  if (age < 0) throw new Error('Age cannot be negative')\n  return age\n}\n\n// After: Errors are visible in the type\nfunction parseAge(input: string): E.Either<string, number> {\n  const age = parseInt(input, 10)\n  if (isNaN(age)) return E.left('Invalid age')\n  if (age < 0) return E.left('Age cannot be negative')\n  return E.right(age)\n}\n\n// Using it\nconst result = parseAge(userInput)\nif (E.isRight(result)) {\n  console.log(`Age is ${result.right}`)\n} else {\n  console.log(`Error: ${result.left}`)\n}","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/sickn33/agentic-awesome-skills/blob/58d857988fcfac6986206bca2b2fe223aa437e4b/skills/fp-pragmatic/SKILL.md#L96-L132","documentation":"Second guard in the same fp-pragmatic parseAge example (skills/fp-pragmatic/SKILL.md:114): thrown when the input parses to a number but that number is negative, i.e. a semantically invalid age that survives the NaN check.","triggerScenarios":"parseAge with -5 or any input whose parsed integer is below zero; also decimal strings that parseInt truncates but that still yield a value of -1 or less.","commonSituations":"Users typing a minus sign or negative numbers in age fields; sign errors in upstream data feeds; test fixtures using sentinel negative values.","solutions":["Add domain constraints to the Either version: chain a non-negative check that returns E.left with a cannot-be-negative message","Constrain at the input layer: number input with min=0 in forms, schema validation with a nonnegative rule","Audit data sources if negatives appear unexpectedly (sign flips, unit confusion)"],"exampleFix":"// before\nif (age < 0) throw new Error('Age cannot be negative')\nreturn age\n\n// after\nreturn age < 0\n  ? E.left('Age cannot be negative')\n  : E.right(age)","handlingStrategy":"validation","validationCode":"const age = Number(input)\nif (!Number.isInteger(age) || age < 0) {\n  return badRequest('age must be a non-negative integer')\n}","typeGuard":"const isNonNegativeInt = (n: unknown): n is number =>\n  typeof n === 'number' && Number.isInteger(n) && n >= 0","tryCatchPattern":null,"preventionTips":["Encode domain bounds (min/max) in a schema, not ad-hoc ifs","Use number inputs with min=0 on forms","Chain range checks in the Either version so all errors surface together"],"tags":["validation","domain-rules","parsing"],"backgroundTag":"out-of-range-value","analyzedSha":"58d857988fcfac6986206bca2b2fe223aa437e4b","analyzedAt":"2026-08-26T11:55:59.350Z","schemaVersion":2},"datasetVersion":"2026-08-26T14:46:13.012Z"}