-
Notifications
You must be signed in to change notification settings - Fork 104
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
[BUG] opensearchapi.Client.Document.Delete bugged error handling (v4) #582
Comments
Thanks for reporting it. Want to try to write a (failing) unit test for this and maybe a fix? |
Hi @yeswecanfixit |
@imvtsl Go for it! |
Thank you! That would be much appreciated |
@dblock @yeswecanfixit |
I reproduced the issue here. When there is error returned from the server, Go client in parseError, tries to parse the response body as testResp struct This works well when there are failure while creating index, deleting index, creating document with existing id, etc. This is because all these responses are in same format. See all responses at the end. However, in case of failure in delete document API, the response body received from the server is completely different. I believe the best practice is for error responses to be consistent across all APIs. Therefore, in an ideal case we should get the error response for delete API changed at the server side. I will create an issue with OpenSearch and update issue number here. Sample ResponsesIndex create failed{ Index delete failed{ Document create failed{ Document delete failed{ |
Created this issue with Opensearch. |
Thank you for investigating this issue. I am thinking if we maybe want to return the reponse body to the client as error if we can't parse the returned error into any struct. On the other hand we are returning the response even on error, so the client can access the reponse body already by calling Something like this: resp, err := client.Document.Delete(ctx, <someRequest>)
if err != nil {
if !errors.Is(err, opensearch.ErrJSONUnmarshalBody) || (errors.Is(err, opensearch.ErrJSONUnmarshalBody) && resp != nil && resp.Inspect().Response.StatusCode != 404) {
return err
}
} |
Thanks for pointing this out. The discussion thread of this issue doesn't seem active. I am willing to pitch in and contribute to opensearch server to make error responses consistent for all APIs.
This way the error will be of type For the workaround, until the issue is fixed at Opensearch, I was thinking of parsing it into
The advantage here is that it is generic for all status code in delete API. The users of the library get one type of response ( Thoughts? |
That would be much appreciated but note that for backwards compat it will only go into 3.0. |
I would not see this as an advantage as all other valid errors can't be parse normally. The users would need to do the error handling we are already doing with the I would rather adjust the
|
I am not sure if I understand what you are trying to convey here. I meant this would be generic only for all errors in delete API. Isn't getting an error struct (
Anyways, this seems to be the best idea so far. It will work not only for delete API but for all APIs when it can't parse the response. I will raise a PR with this. |
I raised this PR for the fix. |
What is the bug?
The function
opensearchapi.Client.Document.Delete
has bugged error handling. When it returns an error, for example when attempting to delete a non-existing document, the function rightly returns an error, but this error, instead of being of type*opensearch.StructError
, is a*fmt.wrapError
with messagemsg
:"opensearch error response could not be parsed as error: {\"_index\":\"my-index\",\"_id\":\"my-id\",\"version\":4,\"result:\":\"not_found\", <ET CETERA . . . > }"
The "inner error" at least has the correct "result": "not_found", but this inner error has been wrapped with this parsing error.
How can one reproduce the bug?
This bug can be reproduced by calling opensearchapi.Client.Document.Delete function on any non-existing document ID.
What is the expected behavior?
The expectation is to receive an error that can be cast as
*opensearch.StructError
usingerrors.As(err, &myStructErr)
, or at the very least, the error should not be wrapped around this parsing error saying the error response could not be parsed as error.What is your host/environment?
MacOS Sonoma 14.5
The text was updated successfully, but these errors were encountered: