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

Fix request error handling to avoid segmentation violation #99

Merged
merged 2 commits into from Jan 16, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions server/mysterium_api.go
Expand Up @@ -77,6 +77,8 @@ func (mApi *mysteriumApi) NodeSendStats(nodeKey string, signer identity.Signer)
err = mApi.doRequest(req)
if err == nil {
log.Info(mysteriumApiLogPrefix, "Node stats sent: ", nodeKey)
} else {
log.Error(mysteriumApiLogPrefix, "Node stats failed: ", err)
Copy link
Member

Choose a reason for hiding this comment

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

Before client_rest.go had common error loging, which was removed during refactoring.
Can You return it back:

func (client *mysteriumRestClient) executeRequest(method, fullPath string, payloadJson []byte) (*http.Response, error) {
	request, err := http.NewRequest(method, fullPath, bytes.NewBuffer(payloadJson))
	request.Header.Set("User-Agent", MYSTERIUM_API_CLIENT)
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Accept", "application/json")
	if err != nil {
		log.Critical(MYSTERIUM_API_LOG_PREFIX, err)
		return nil, err
	}

	response, err := client.http.Do(request)

	if err != nil {
		log.Error(MYSTERIUM_API_LOG_PREFIX, err)
		return response, err
	}

	err = parseResponseError(response)
	if err != nil {
		log.Error(MYSTERIUM_API_LOG_PREFIX, err)
		return response, err
	}

	return response, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}
return err
}
Expand Down Expand Up @@ -117,9 +119,10 @@ func (mApi *mysteriumApi) SendSessionStats(sessionId string, sessionStats dto.Se

func (mApi *mysteriumApi) doRequest(req *http.Request) error {
resp, err := mApi.http.Do(req)
if err == nil {
resp.Body.Close()
if err != nil {
return err
}
defer resp.Body.Close()
return parseResponseError(resp)
Copy link
Member

Choose a reason for hiding this comment

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

What if parsing fails

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't parse body - it just checks status code and if it's failure, returns error.

}

Expand Down