-
Notifications
You must be signed in to change notification settings - Fork 36
Migrate to typed errors #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
| type ParserError = libyaml.ParserError | ||
| type ScannerError = libyaml.ScannerError | ||
| type ReaderError = libyaml.ReaderError | ||
| type WriterError = libyaml.WriterError |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
Signed-off-by: Tim Ramlot <[email protected]>
Signed-off-by: Tim Ramlot <[email protected]>
3deb4aa to
2b0e6ed
Compare
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.