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
Support error types #240
Support error types #240
Conversation
Codecov ReportBase: 100.00% // Head: 99.74% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #240 +/- ##
===========================================
- Coverage 100.00% 99.74% -0.26%
===========================================
Files 2 3 +1
Lines 356 386 +30
===========================================
+ Hits 356 385 +29
- Misses 0 1 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
@caarlos0 @jurajkulich please feel free to review and validate it Any comments appreciated Thanks |
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.
a few comments, but looking good so far!
thanks for the PR :D
error.go
Outdated
// ErrNotAStructPtr is returned if you pass something that is not a pointer to a Struct to Parse. | ||
type NotStructPtrError struct { | ||
msg string | ||
} | ||
|
||
func newNotStructPtrError() error { | ||
return NotStructPtrError{"expected a pointer to a Struct"} | ||
} | ||
|
||
func (e NotStructPtrError) Error() string { | ||
return e.msg | ||
} |
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.
seems like this can be simplified to something like:
var ErrNotAStructPtr = errors.New("expected a pointer to a Struct")
as the message is always the same, right?
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.
that's definitely true, but I did it deliberately to follow the same error pattern like other errors (struct + func to create and error) do you recommend to make it simpler?
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.
yes, simpler is usually better :)
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 came to realize we are loosing a possibility to check the error type using a errors.New(). But anyway, I have updated this error, can you please check
error.go
Outdated
type NoParserError struct { | ||
msg string | ||
} | ||
|
||
func newNoParserError(sf reflect.StructField) error { | ||
return NoParserError{fmt.Sprintf(`no parser found for field "%s" of type "%s"`, sf.Name, sf.Type)} | ||
} | ||
|
||
func (e NoParserError) Error() string { | ||
return e.msg | ||
} |
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 usually do something like this:
type NoParserError struct { | |
msg string | |
} | |
func newNoParserError(sf reflect.StructField) error { | |
return NoParserError{fmt.Sprintf(`no parser found for field "%s" of type "%s"`, sf.Name, sf.Type)} | |
} | |
func (e NoParserError) Error() string { | |
return e.msg | |
} | |
// NoParserError happens when etc... | |
type NoParserError struct { | |
Name string | |
Type string | |
} | |
func newNoParserError(sf reflect.StructField) error { | |
return NoParserError{sf.Name, sf.Type} | |
} | |
func (e NoParserError) Error() string { | |
return fmt.Sprintf("no parser found for field %q of type %q", e.Name, e.Type) | |
} |
That way, internally, we can still get the "unparsed" name and type...
I would do this, and the same with the other similar errors bellow...
Also, we should add a comment to the error type saying when it should be raised and what it means.
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.
fair comment, I wanted to make all errors generic: the same structure (msg only), but your comment makes sense:
- User converts error to specific type of the error
- User now has access to all error fields +message string
Let me improve it
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.
Updated as discussed
Hello @caarlos0. Can you please take a look at the changes when you have time |
Hello @caarlos0, any updates on this? Do you need my help to clarify/review it? |
sorry, got caught up in some personal issues, will review it when I get some peace time |
refs #240 Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This is an enhancement related to that discussion: #238
The purpose of this PR is to make a possibility convert an error to its specific type.
Since the errors aggregation has been added before (#233), user should convert an error result to AggregateError first and then iterate over accumulated errors, example: