Skip to content

Allow customizing the error message #656

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

Closed
wants to merge 1 commit into from

Conversation

lugray
Copy link

@lugray lugray commented Feb 14, 2025

This package produces really nice error messages that helpfully point to a section of YAML that caused an error. When there's an error, the package has technical knowledge of what the problem is. The calling program may have more practical knowledge of what the problem is, allowing it to present a helpful message to the user. In this case, it can be useful to allow that calling program to modify the message, but keep the portion of the error that points at the YAML source. Adding this interface allows it to do so in a consistent way that's agnostic of the exact underlying error type.

func (o *SomeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
  ...
  if err := unmarshal(&var); err != nil {
    if e, ok := err.(yaml.ErrorWithSource); ok {
      return e.WithMessage("Options for this field are ...")
    }
    return err
  }
  ...
}

Before submitting your PR, please confirm the following.

  • Describe the purpose for which you created this PR.
  • Create test code that corresponds to the modification

This package produces really nice error messages that helpfully point to
a section of YAML that caused an error. When there's an error, the
package has technical knowledge of what the problem is. The calling
program may have more practical knowledge of what the problem is,
allowing it to present a helpful message to the user. In this case, it
can be useful to allow that calling program to modify the message, but
keep the portion of the error that points at the YAML source. Adding
this interface allows it to do so in a consistent way that's agnostic of
the exact underlying error type.

```go
func (o *SomeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
  ...
  if err := unmarshal(&var); err != nil {
    if e, ok := err.(yaml.ErrorWithSource); ok {
      return e.WithMessage("Options for this field are ...")
    }
    return err
  }
  ...
}
```
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.58%. Comparing base (2ac8cff) to head (cdd37e7).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #656   +/-   ##
=======================================
  Coverage   77.58%   77.58%           
=======================================
  Files          22       22           
  Lines        7840     7840           
=======================================
  Hits         6083     6083           
  Misses       1345     1345           
  Partials      412      412           

@lugray
Copy link
Author

lugray commented Mar 3, 2025

@goccy Is this something you think you might consider, or should we be figuring out how to do our own thing in our codebase?

@goccy
Copy link
Owner

goccy commented Mar 16, 2025

@lugray I've supported this feature myself with #677 . Can use this feature for your case ?

@lugray
Copy link
Author

lugray commented Mar 16, 2025

@lugray I've supported this feature myself with #677 . Can use this feature for your case ?

That looks great. It changed more about your internals than I felt comfortable doing, but absolutely satisfies what I'm looking for. Thanks for your work here and on this package in general!

@goccy
Copy link
Owner

goccy commented Mar 16, 2025

@lugray Thank you for your reviewing. I've merged that PR.

@lugray lugray closed this Mar 16, 2025
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.

3 participants