Skip to content

fix(yaml): export YAMLException type#73

Open
terminalchai wants to merge 2 commits into
unjs:mainfrom
terminalchai:fix/export-yaml-exception
Open

fix(yaml): export YAMLException type#73
terminalchai wants to merge 2 commits into
unjs:mainfrom
terminalchai:fix/export-yaml-exception

Conversation

@terminalchai

@terminalchai terminalchai commented Mar 14, 2026

Copy link
Copy Markdown

Closes #17

Problem

YAMLException is the error thrown by js-yaml when YAML parsing fails, and it's already referenced in the YAMLParseOptions.onWarning callback signature. However, it was declared locally via a private declare class YAMLException without an export keyword, making it impossible for users to import or catch the type explicitly:

import { YAMLException } from 'confbox/yaml' // Error: not exported

Fix

  • Import YAMLException from js-yaml (already a dev dependency via @types/js-yaml)
  • Re-export it as a proper type from confbox/yaml
  • Re-export it from the main confbox entry point as well
  • Remove the now-redundant local declare class YAMLException and its supporting interface Mark

After this change, users can catch YAML parse errors with full type safety:

import { parseYAML, YAMLException } from 'confbox'
// or
import { parseYAML, YAMLException } from 'confbox/yaml'

try {
  parseYAML(input)
} catch (e) {
  if (e instanceof YAMLException) {
    console.error(e.reason, e.mark)
  }
}

Summary by CodeRabbit

  • New Features
    • YAMLException is now part of the public API, so apps can reference and handle YAML parsing exceptions directly.
  • Behavior Changes
    • Warning callback now receives detailed exception information when YAML parsing issues occur, enabling richer diagnostics.

@coderabbitai

coderabbitai Bot commented Mar 14, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds YAMLException to the package public API by importing it from js-yaml, re-exporting it from src/yaml.ts, and including it in src/index.ts exports; internal YAMLException and Mark declarations were removed and YAMLParseOptions.onWarning now accepts YAMLException.

Changes

Cohort / File(s) Summary
Public API exports
src/index.ts
Added YAMLException to the named exports alongside parseYAML and stringifyYAML.
YAML module & types
src/yaml.ts
Imported YAMLException from js-yaml, re-exported its type, removed internal YAMLException class and Mark interface, and updated YAMLParseOptions.onWarning to accept YAMLException.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I found a type beneath a log,
I dusted it off through dew and fog.
Now YAML errors hop into view,
Re-exported neat — a clearer cue.
Hooray! 🥕✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(yaml): export YAMLException type' clearly and concisely summarizes the main change: exporting the YAMLException type from the yaml module.
Linked Issues check ✅ Passed The PR fully addresses issue #17 by importing YAMLException from js-yaml and re-exporting it from both confbox/yaml and the main entry point, enabling consumers to import and use the exception type for typed error handling.
Out of Scope Changes check ✅ Passed All changes are directly related to exporting YAMLException: importing from js-yaml, re-exporting from yaml.ts and index.ts, updating YAMLParseOptions, and removing local declarations. No unrelated modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/index.ts`:
- Around line 7-8: The current barrel re-exports `YAMLException` only as a type
which breaks runtime instanceof checks; change the exports so parseYAML and
stringifyYAML remain value exports, keep YAMLParseOptions and
YAMLStringifyOptions as type-only exports, and add a value export for the
YAMLException class (while optionally also exporting its type if desired) so
consumers can use `instanceof YAMLException`; update the export line referencing
YAMLException accordingly.

In `@src/yaml.ts`:
- Around line 51-52: The current line exports only the type alias, so the
runtime constructor for YAMLException is not exported and instanceof checks
fail; replace the type-only export with a value export of the class (i.e.,
export the YAMLException constructor itself rather than using "export type"), or
re-export the class from its defining module so the runtime value YAMLException
is available for instanceof checks; ensure the exported symbol name is
YAMLException to match callers.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8bee9159-14b2-420c-872d-f99c61be5a9f

📥 Commits

Reviewing files that changed from the base of the PR and between e9b08c0 and f0869ce.

📒 Files selected for processing (2)
  • src/index.ts
  • src/yaml.ts

Comment thread src/index.ts Outdated
Comment thread src/yaml.ts Outdated
@terminalchai

Copy link
Copy Markdown
Author

Good catch @coderabbitai — applied both suggestions in caebf91. Changed \export type { YAMLException }\ → \export { YAMLException }\ in \yaml.ts, and split the barrel export in \index.ts\ so \YAMLException\ ships as a value export alongside the existing value exports. \instanceof YAMLException\ will now work correctly at runtime.

@coderabbitai

coderabbitai Bot commented Mar 14, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@terminalchai: That's exactly right — switching to export { YAMLException } (value export) ensures the class constructor is present in the emitted JavaScript, so instanceof YAMLException works correctly at runtime. The split barrel export in index.ts carries that through to the main entry point as well. Great fix! 🎉


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/yaml.ts`:
- Line 1: The project currently imports and re-exports YAMLException from
js-yaml (see the import line "import { load, dump, YAMLException } from
'js-yaml';" and the export of YAMLException later), but js-yaml is only in
devDependencies; update package.json to move js-yaml from devDependencies to
dependencies so consumers can import the re-exported YAMLException at runtime,
then run npm/yarn install to update lockfile.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c3020f44-d9bb-433a-8ae4-3d82e30940c1

📥 Commits

Reviewing files that changed from the base of the PR and between f0869ce and caebf91.

📒 Files selected for processing (2)
  • src/index.ts
  • src/yaml.ts

Comment thread src/yaml.ts
@@ -1,4 +1,4 @@
import { load, dump } from "js-yaml";
import { load, dump, YAMLException } from "js-yaml";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

PKG_FILE="$(fd '^package\.json$' | head -n 1)"
echo "Inspecting: ${PKG_FILE}"

jq -r '
{
  dependencies_js_yaml: .dependencies["js-yaml"],
  peerDependencies_js_yaml: .peerDependencies["js-yaml"],
  devDependencies_js_yaml: .devDependencies["js-yaml"]
}
' "${PKG_FILE}"

Repository: unjs/confbox

Length of output: 190


Move js-yaml from devDependencies to dependencies in package.json.

The code imports YAMLException as a value at Line 1 and re-exports it at Line 51. Since consumer code will use this export at runtime, js-yaml must be in dependencies, not devDependencies. Currently it is only in devDependencies.

Also applies to: 51-51

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/yaml.ts` at line 1, The project currently imports and re-exports
YAMLException from js-yaml (see the import line "import { load, dump,
YAMLException } from 'js-yaml';" and the export of YAMLException later), but
js-yaml is only in devDependencies; update package.json to move js-yaml from
devDependencies to dependencies so consumers can import the re-exported
YAMLException at runtime, then run npm/yarn install to update lockfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inability to use parse exception types

1 participant