Skip to content

Conversation

@inteon
Copy link
Contributor

@inteon inteon commented Nov 6, 2025

While working on #152, I got some feedback asking to improve the returned errors.

However, the errors can be improved separately from that PR. In this PR, I try to do that. This PR introduces typed errors and tries to make the error messages more useful by adding all the context that was previously missing.

Copilot AI review requested due to automatic review settings November 6, 2025 14:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors error handling in the YAML library by introducing structured error types with better position information and error wrapping support. The changes consolidate error handling logic from the parser into dedicated error types in the libyaml package.

Key Changes

  • Introduced structured error types (ParserError, ScannerError, ReaderError, WriterError, EmitterError) in internal/libyaml/errors.go
  • Added Mark.String() method to format position information consistently
  • Replaced raw error strings with structured error types that include context and position information
  • Updated all tests to match the new, more detailed error message formats

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/libyaml/errors.go New file defining structured error types with position and context information
internal/libyaml/yaml.go Added Mark.String() method and removed old ErrorType fields from Parser
internal/libyaml/parser.go Updated to construct ParserError instances instead of setting error fields
internal/libyaml/scanner.go Updated to construct ScannerError instances with proper line numbering
internal/libyaml/reader.go Updated to construct ReaderError instances with error wrapping
internal/libyaml/writer.go Updated to return WriterError with wrapped errors
internal/libyaml/emitter.go Replaced errors.New calls with EmitterError instances
yaml.go Replaced custom ParserError with type alias to libyaml.ParserError, added type aliases for other errors
decode.go Simplified error handling to use parser.Error directly
cmd/go-yaml/parser.go Updated to use parser.Error with proper error wrapping
decode_test.go Updated expected error messages and changed assertions from ErrorMatches to Equal
limit_test.go Updated expected error messages with position information

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

yaml.go Outdated
Comment on lines 321 to 324
type ParserError = libyaml.ParserError
type ScannerError = libyaml.ScannerError
type ReaderError = libyaml.ReaderError
type WriterError = libyaml.WriterError
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit annoyed because you are exporting all out internal type of error.

It could limit us in refactoring later.

I don't think user need to have access to every field of our error.

I would have defined sentinel errors, and return them by adding an ErrorIs on each type.

It's better for us and for users. They can simply catch 4 sentinel errors, and maybe we could wrap another common error witg them.

Copy link
Contributor Author

@inteon inteon Nov 26, 2025

Choose a reason for hiding this comment

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

This was the easiest way I found to keep the TestParserErrorDecoder test working. Feel free to make any changes to my PR (I don't think I fully understand what you are suggesting).

@inteon inteon force-pushed the add_errors branch 2 times, most recently from 3deb4aa to 2b0e6ed Compare November 26, 2025 09:38
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.

2 participants