For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/examples/parse-package-json.md.
  • English
  • Parse package.json

    This example reads the package.json file in the current directory, validates its contents, and prints the package name and version. Every step that can fail returns a Result, and each kind of failure is reported with its own message.

    What This Example Shows

    • Wrapping Node.js APIs: readFile wraps fs.readFile with Result.fn and converts file system errors into a ReadFileError.
    • Parsing and validation: parsePackageJson parses the text with Result.try and validates it against a Zod schema with Result.parse. Any failure is converted into a ParsePackageJsonError with Result.mapError.
    • Composing steps: The entry point chains both steps with Result.pipe and Result.andThen.
    • Handling the outcome: Result.inspect prints the result, while Result.inspectError uses ts-pattern to print a message for each error type and sets the exit code.
    • Testing: The tests assert results with the matchers from @praha/byethrow-testing.