Skip to content
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

Add defined errors to the package #44

Merged
merged 4 commits into from
Nov 17, 2019

Conversation

ghostlandr
Copy link
Contributor

@ghostlandr ghostlandr commented Nov 11, 2019

Hello! I have finally come back for #29 and solved it basically the way you suggested. I'm looking to try to get involved with more Go OSS and this is my first PR so far.

My biggest concern is that it's theoretically a breaking change, as if someone was depending on having error details in Error previously, those are now gone.

The reason this is so many changes is because as part of adding the ErrorDetails to Root I also changed the usages of Root to use labelled properties rather than specifying everything in each initialization.

Let me know if I should add an example to either the Readme or to the examples folder of this in use?

This will allow implementors to use these errors to detect
if an error has occurred, rather than using string comparison.

This also adds an ErrorDetails field to the Root object, which
is mostly for debugging.

Add some comments and update changelog
@anaskhan96
Copy link
Owner

Hi @gdholtslander

First of all, thank you for the PR. This is amazing! Breaking changes are okay, as this will be a separate release.

I do have an issue though.

  • Not sure if we should be including two error related fields in the Root struct. That could lead to two possible nil pointer dereference exceptions by a programmer instead of one on every success.
  • On top of that, there's actually a duplication of error explanation text taking place. Let's take ErrElementNotFound for example. It has two strings, on in Root.Error.Error() which is "element not found", and one in Root.ErrorDetails which is "element x with attributes y not found"

Either we dispose of the ErrorDetails, or if we really want to give an explanation from our side, we have Root.Error be a variable of our custom composite type of error, something like this

type SoupError struct {
    Type ErrorType // our enum with values `ErrUnableToParse`, `ErrElementNotFound`, etc.
    error
}

This way, we can get the type using Root.Error.Type and the detailed text using Root.Error.Error()

Let me know what you think.

Oh, and also unit test cases. Very important for an integral change like this :P

@ghostlandr
Copy link
Contributor Author

I like the second approach! I had that one essentially coded up but I was worried about backwards compatibility - essentially that people would already be checking for certain error strings and those wouldn't be there anymore. If it's still 1.x then there shouldn't be breaking changes... unless you meant we'd release it as a 2.0 if we added this. A bit extreme just for errors! 😂

I think if we implement Error() as the old message, it should still be backward compatible...

I'll keep working on this. Thanks for the feedback!

Exposes a soup.Error that can be inspected to see what kind of error
you are dealing with, then be able to further inspect the Error()
method to see if there is any more information for you.

Avoids breaking backwards compatibility by not changing the type of
Error in the Root object, so inspecting the returned err should
still work in other projects.
@ghostlandr
Copy link
Contributor Author

I didn't realize it has been almost a year since I made the issue. Whoops 😨

@ghostlandr
Copy link
Contributor Author

Second approach we talked about is implemented, and I added some tests. I also added an example of some error handling one could do, but it's quite basic.

Looking forward to hearing what you think about this now.

soup.go Show resolved Hide resolved
soup.go Show resolved Hide resolved
soup.go Show resolved Hide resolved
@anaskhan96
Copy link
Owner

Merging this, thank you again for the PR! :)

@anaskhan96 anaskhan96 merged commit 2dc0401 into anaskhan96:master Nov 17, 2019
@ghostlandr ghostlandr deleted the error-matching branch November 18, 2019 13: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.

None yet

2 participants