Skip to content

Commit

Permalink
fix error interface nil check (#194)
Browse files Browse the repository at this point in the history
`nil` checks in go is based on the value and interface. Even if
`doRequest` returns `nil`, because `post` return type is
`*TransportError`, the interface is not `nil`. Fix is to replace type to
generic `error`
https://go.dev/doc/faq#nil_error
  • Loading branch information
kenny-statsig authored Jun 26, 2024
1 parent 954c724 commit e477619
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (e *TransportError) Error() string {
func (e *TransportError) Unwrap() error { return e.Err }

type LogEventError struct {
Err *TransportError
Err error
Events int
}

Expand Down
19 changes: 11 additions & 8 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (opts *RequestOptions) fill_defaults() {
}
}

func (transport *transport) download_config_specs(sinceTime int64, responseBody interface{}) (*http.Response, *TransportError) {
func (transport *transport) download_config_specs(sinceTime int64, responseBody interface{}) (*http.Response, error) {
var endpoint string
if transport.options.DisableCDN {
endpoint = fmt.Sprintf("/download_config_specs?sinceTime=%d", sinceTime)
Expand All @@ -74,11 +74,11 @@ func (transport *transport) download_config_specs(sinceTime int64, responseBody
return transport.get(endpoint, responseBody, RequestOptions{})
}

func (transport *transport) get_id_lists(responseBody interface{}) (*http.Response, *TransportError) {
func (transport *transport) get_id_lists(responseBody interface{}) (*http.Response, error) {
return transport.post("/get_id_lists", nil, responseBody, RequestOptions{})
}

func (transport *transport) get_id_list(url string, headers map[string]string) (*http.Response, *TransportError) {
func (transport *transport) get_id_list(url string, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, &TransportError{Err: err}
Expand All @@ -103,7 +103,7 @@ func (transport *transport) get_id_list(url string, headers map[string]string) (
return res, nil
}

func (transport *transport) log_event(event []interface{}, responseBody interface{}, options RequestOptions) (*http.Response, *TransportError) {
func (transport *transport) log_event(event []interface{}, responseBody interface{}, options RequestOptions) (*http.Response, error) {
input := logEventInput{
Events: event,
StatsigMetadata: transport.metadata,
Expand All @@ -116,11 +116,11 @@ func (transport *transport) log_event(event []interface{}, responseBody interfac

}

func (transport *transport) post(endpoint string, body interface{}, responseBody interface{}, options RequestOptions) (*http.Response, *TransportError) {
func (transport *transport) post(endpoint string, body interface{}, responseBody interface{}, options RequestOptions) (*http.Response, error) {
return transport.doRequest("POST", endpoint, body, responseBody, options)
}

func (transport *transport) get(endpoint string, responseBody interface{}, options RequestOptions) (*http.Response, *TransportError) {
func (transport *transport) get(endpoint string, responseBody interface{}, options RequestOptions) (*http.Response, error) {
return transport.doRequest("GET", endpoint, nil, responseBody, options)
}

Expand Down Expand Up @@ -185,10 +185,13 @@ func (transport *transport) doRequest(
in interface{},
out interface{},
options RequestOptions,
) (*http.Response, *TransportError) {
) (*http.Response, error) {
request, err := transport.buildRequest(method, endpoint, in, options.header)
if request == nil || err != nil {
return nil, &TransportError{Err: err}
if err != nil {
return nil, &TransportError{Err: err}
}
return nil, nil
}
options.fill_defaults()
response, err, attempts := retry(options.retries, time.Duration(options.backoff), func() (*http.Response, bool, error) {
Expand Down

0 comments on commit e477619

Please sign in to comment.