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

OkHttpClient return code is always = -1 when the response is code 404 or any other not successful code #366

Closed
eusorov opened this issue Oct 26, 2023 · 0 comments
Labels
status:completed Completed - but might not be released yet.

Comments

@eusorov
Copy link
Contributor

eusorov commented Oct 26, 2023

Describe the bug
When OkHttpClient sends snowplow payload and it fails with any code like 404, then the return code is always = -1.
It only sets to actual value, when the response is successful.

To Reproduce
OkHttpClientAdapter:

public int doPost(String url, String payload) {
        int returnValue = -1;

        RequestBody body = RequestBody.create(payload, JSON);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", Constants.POST_CONTENT_TYPE)
                .post(body)
                .build();
        try (Response response = httpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                LOGGER.error("OkHttpClient POST Request failed: {}", response);
            } else {
                returnValue = response.code();
            }
        } catch (IOException e) {
            LOGGER.error("OkHttpClient POST Request failed: {}", e.getMessage());
        }

        return returnValue;
    }

Expected behavior
in the block

if (!response.isSuccessful()) {
}
there should be also returnValue = response.code(); So for example retry etc can pick up the code and evaluate the value.
better remove the else block like this:

                try (Response response = httpClient.newCall(request).execute()) {
			if (!response.isSuccessful()) {
				LOGGER.error("OkHttpClient POST Request failed: {}", response);
			}
			returnValue = response.code();
		} catch (IOException e) {
			LOGGER.error("OkHttpClient POST Request failed: {}", e.getMessage());
		}

Same applies for doGet() method
Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: Mac Ventura 13.6 (22G120)
  • Version: Java 8

Additional context
Add any other context about the problem here.

eusorov added a commit to eusorov/snowplow-java-tracker that referenced this issue Oct 26, 2023
@github-actions github-actions bot added the status:completed Completed - but might not be released yet. label Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:completed Completed - but might not be released yet.
Projects
None yet
Development

No branches or pull requests

1 participant