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 Err() function to Response for detailed errors #246

Merged
merged 11 commits into from
Mar 22, 2023

Conversation

Jakob3xD
Copy link
Collaborator

@Jakob3xD Jakob3xD commented Mar 3, 2023

Description

Add Err() function to Response and a new Error Type for a better and more detailed error handling.

Issues Resolved

Related #220

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@Jakob3xD Jakob3xD force-pushed the api-response-error branch 5 times, most recently from 0a39d49 to 1a28767 Compare March 3, 2023 15:10
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I like this a lot! Let's add something to USER_GUIDE too?

opensearchapi/opensearchapi.response.go Outdated Show resolved Hide resolved
@Jakob3xD Jakob3xD force-pushed the api-response-error branch 2 times, most recently from 6ada302 to a9137e0 Compare March 3, 2023 17:37
USER_GUIDE.md Show resolved Hide resolved
@Jakob3xD Jakob3xD force-pushed the api-response-error branch from a9137e0 to 531d416 Compare March 15, 2023 08:53
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I think we're close, but something feels wrong about having to now check for error twice. WDYT?

os.Exit(1)
}
if err := searchResponse.Err(); err != nil {
fmt.Println(fmt.Sprintf("failed to search document: %s", err))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think this is net worse than what we had - we now have to check for error twice? Shouldn't err the first time be what we put into searchResponse.Err()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes and nope.
When doing the request the .Do function returns the response and an error. For example this Cat request. The returned error from .Do is most likely a network error or something with the request it self.

Compared to the Err() function which checks the actual returned response for errors. If you wanted do this in an single error check, you would need to use the .Err() function in each .Do() function, as right now the .Do functions does not care about the actual response.

Copy link
Member

Choose a reason for hiding this comment

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

That's what I am saying, shouldn't we make .Do care about the actual response?

As a user I want to write this:

createIndexResponse, err := createIndex.Do(context.Background(), client)
if err != nil {
  // err here is either a network error, or the error from the response
  fmt.Println(fmt.Sprintf("failed to create index: %s", err))
  os.Exit(1)
}

WDYT?

Copy link
Collaborator Author

@Jakob3xD Jakob3xD Mar 16, 2023

Choose a reason for hiding this comment

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

Depends on what the lib maintainers wants.
Pro:

  • The lib has a standard way on handling response errors, which prevent other users from doing it by them self
  • You only need to check one error
  • Adds the possibility to have a custom error type for some requests
  • Would make it easier for beginners to use the lib as the don´t need to handle the errors by them self

Cons:

  • Existing users with their own implementation needs to check the returned error. For example if the status code 404 is accepted. Currently this would be an error. Otherwise the question turns up, if the accepted response code should be a config setting to each Do request?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

main...Jakob3xD:opensearch-go:opensearchapi-response-error

Still missing some stuff but this is how it could look like.

Copy link
Member

Choose a reason for hiding this comment

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

I like the pros and I like the implementation. The fact that it's easier for beginners is huge.

Is this (the con) a breaking change for existing users? If so we'll need to major increment the version of the client.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IMO this would be a braking change as users that did not care about the response code will now get errors they need to handle.

I will drop the current commits and cherry-pick the commits of my other branch into this after some more USER_GUIDE improvements. I want/need to add an example of the error handling.

@Jakob3xD Jakob3xD force-pushed the api-response-error branch 2 times, most recently from 835b0dc to b028a9a Compare March 17, 2023 22:01
@Jakob3xD Jakob3xD marked this pull request as ready for review March 20, 2023 17:58
dblock
dblock previously approved these changes Mar 20, 2023
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

This is so much better!

Let's add an UPGRADING.md like in #237?

opensearchapi/opensearchapi.error.go Outdated Show resolved Hide resolved
Jakob3xD added 10 commits March 21, 2023 11:00
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
@dblock dblock merged commit 8d3cf4d into opensearch-project:main Mar 22, 2023
@Jakob3xD Jakob3xD deleted the api-response-error branch March 23, 2023 08:41
@dblock
Copy link
Member

dblock commented Mar 23, 2023

Reading https://github.com/opensearch-project/opensearch-go/blob/main/UPGRADING.md#opensearchapi-error-handling I think we didn't do the best job explaining how before/after for error handling changed. You had to do isError() before and now you don't. Care to add something to that @Jakob3xD?

@dblock
Copy link
Member

dblock commented Mar 29, 2023

@Jakob3xD I tried sending you an e-mail about something, maybe it went to spam? :) mind dropping me a note to dblock[at]dblock[dot]org or dblock[at]amazon[dot]com? Thaaanks!

@Jakob3xD
Copy link
Collaborator Author

Jakob3xD commented Apr 6, 2023

Closes #220

@Jakob3xD Jakob3xD changed the title Draft: Add Err() function to Response for detailed errors Add Err() function to Response for detailed errors Apr 6, 2023
@wbeckler
Copy link

wbeckler commented May 9, 2023

There might have been something amiss in the way we updated the user guide. A user has run into a compilation issue with the Error type: https://forum.opensearch.org/t/ide-doesnt-see-error-type-in-opensearch-go-example/14224

@dblock
Copy link
Member

dblock commented May 9, 2023

Might just be an older client version @wbeckler.

VachaShah added a commit to VachaShah/opensearch-go that referenced this pull request May 18, 2023
VachaShah added a commit to VachaShah/opensearch-go that referenced this pull request May 18, 2023
…ensearch-project#246)"

This reverts commit 8d3cf4d.

Signed-off-by: Vacha Shah <vachshah@amazon.com>
dblock pushed a commit that referenced this pull request May 22, 2023
#322)

* Revert "Draft: Add Err() function to Response for detailed errors (#246)"

This reverts commit 8d3cf4d.

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Fixing PIT APIs introduced in PR #253 since they use response.Err()

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Removing opensearchapi.Error usage from snapshot tests introduced in PR #237

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Resolve Changelog conflicts

Signed-off-by: Vacha Shah <vachshah@amazon.com>

---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
@dblock
Copy link
Member

dblock commented Jun 12, 2023

@Xe ranted about the ES client in https://xeiaso.net/blog/elasticsearch - I think we fixed the error handling part fully in this PR, @Jakob3xD care to take a look at the blog post? anything I've missed?

@Jakob3xD
Copy link
Collaborator Author

I wouldn't say we cover it fully. In Go it is common to have pre-defined error types that you can match on. So for example you have the error type ErrIndexNotFound which the user can then use to match against his error. But as this is hard to maintain our implementation is more generic but at least existing 😅. We do the json to go part for the user and return one error so you don't need to check the response for errors your self.

Beside that I can understand his anger. That's why I want to rework the lib to make it more idiomatic.

@dblock
Copy link
Member

dblock commented Jun 12, 2023

Beside that I can understand his anger. That's why I want to rework the lib to make it more idiomatic.

Please!

@MaxKsyunz
Copy link

Was this PR reverted? Not seeing it in main or the 2.3 release.

@dblock
Copy link
Member

dblock commented Jul 26, 2023

Yeah, but I don't know what happened here, @VachaShah?

@VachaShah
Copy link
Collaborator

Yeah, but I don't know what happened here, @VachaShah?

Thats weird, I don't see this commit but no revert happened for this as well. Taking a deeper look.

@VachaShah
Copy link
Collaborator

Was this supposed to be a breaking change?

@dblock
Copy link
Member

dblock commented Jul 26, 2023

2f58bf6 but that's on 2.3 - I guess because it was a breaking change

@dblock
Copy link
Member

dblock commented Jul 26, 2023

Was this PR reverted? Not seeing it in main or the 2.3 release.

It's on main though, but was reverted from 2.3 as breaking?

@VachaShah
Copy link
Collaborator

Yeah I reverted before 2.3 release since this was a breaking change to do the next 2.x release.

VachaShah added a commit to VachaShah/opensearch-go that referenced this pull request Oct 3, 2023
opensearch-project#322)

* Revert "Draft: Add Err() function to Response for detailed errors (opensearch-project#246)"

This reverts commit 8d3cf4d.

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Fixing PIT APIs introduced in PR opensearch-project#253 since they use response.Err()

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Removing opensearchapi.Error usage from snapshot tests introduced in PR opensearch-project#237

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Resolve Changelog conflicts

Signed-off-by: Vacha Shah <vachshah@amazon.com>

---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
dblock pushed a commit that referenced this pull request Oct 4, 2023
#322) (#384)

* Revert "Draft: Add Err() function to Response for detailed errors (#246)"

This reverts commit 8d3cf4d.



* Fixing PIT APIs introduced in PR #253 since they use response.Err()



* Removing opensearchapi.Error usage from snapshot tests introduced in PR #237



* Resolve Changelog conflicts



---------

Signed-off-by: Vacha Shah <vachshah@amazon.com>
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.

6 participants