okta / okta-sdk-golang

A Golang SDK for interacting with the Okta management API, enabling server-side code to manage Okta users, groups, applications, and more.
https://github.com/okta/okta-sdk-golang
Other
171 stars 142 forks source link

return response even upon error #433

Closed MickeyShnaiderman-RecoLabs closed 4 months ago

MickeyShnaiderman-RecoLabs commented 4 months ago

The decoder might fail on the given v object, but the returned Golang error is not indicative enough. Return the response to let the user inspect it to make things easier.

Summary

The SDK functions might fail upon a malformed JSON returned from the server. The internal buildResponse function tries to unmarshal the response. If it fails, it returns the native Golang error without the response from the server. The native error is not informative, so the response is crucial to determine the root cause, especially in a production environment where debugging is not possible. To solve that, the buildResponse function returns the response from the server (instead of nil) even when there is an error. This should not break any existing functionality and usage of the SDK.

Type of PR

Test Information

Go Version: 1.22.0 Os Version: macOS Sonoma 14.2.1 OpenAPI Spec Version:

Signoff

duytiennguyen-okta commented 4 months ago

I will close this one since we are planning to deprecate v2. I will update it for v4

monde commented 4 months ago

@duytiennguyen-okta 's going to update the equivalent code in the current version of the sdk client that is generated from this template:

https://github.com/okta/okta-sdk-golang/blob/master/.generator/templates/response.mustache#L58C1-L64C22

We think the improvement should be to let the caller have access to both the error and the response objects and be responsible to themselves for checking on nils

func buildResponse(resp *http.Response, cli *APIClient, v interface{}) (*APIResponse, error) {
//...
/*
    if err == io.EOF {
        err = nil
    }
    if err != nil {
        return nil, err
    }
    return response, nil
*/
    return response, err
}
monde commented 4 months ago

https://github.com/okta/okta-sdk-golang/pull/436