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 bad sad error occurred with incorrect central token #42152

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@

// Unauthorized access token
if (packageResolutionResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType.get(), resolvePackageNamesBody);

Check warning on line 831 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L831

Added line #L831 was not covered by tests
}

// If search request was sent wrongly
Expand Down Expand Up @@ -905,7 +905,7 @@

// Unauthorized access token
if (packageResolutionResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType.get(), packageResolutionResponseBody);

Check warning on line 908 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L908

Added line #L908 was not covered by tests
}

// If search request was sent wrongly
Expand Down Expand Up @@ -973,7 +973,7 @@

// Unauthorized access token
if (searchResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType.get(), searchResponseBody);

Check warning on line 976 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L976

Added line #L976 was not covered by tests
}

// If search request was sent wrongly
Expand Down Expand Up @@ -1043,7 +1043,7 @@

// Unauthorized access token
if (searchResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType.get(), searchResponseBody);

Check warning on line 1046 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1046

Added line #L1046 was not covered by tests
}

// If search request was sent wrongly
Expand Down Expand Up @@ -1152,7 +1152,7 @@

// Unauthorized access token
if (deprecationResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType.get(), deprecationResponseBody);

Check warning on line 1155 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1155

Added line #L1155 was not covered by tests
}

// If deprecation request was sent wrongly
Expand Down Expand Up @@ -1474,23 +1474,25 @@
Optional<ResponseBody> body = Optional.ofNullable(response.body());
if (body.isPresent()) {
// If search request was sent wrongly
String responseBody = body.get().string();
MediaType contentType = body.get().contentType();
if (response.code() == HTTP_BAD_REQUEST || response.code() == HTTP_NOT_FOUND) {
Error error = new Gson().fromJson(body.get().string(), Error.class);
Error error = new Gson().fromJson(responseBody, Error.class);
if (error.getMessage() != null && !"".equals(error.getMessage())) {
throw new CentralClientException(error.getMessage());
}
}

// Unauthorized access token
if (response.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(contentType, responseBody);

Check warning on line 1488 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1488

Added line #L1488 was not covered by tests
}

// If error occurred at remote repository or invalid/no response received at the
// gateway server
if (response.code() == HTTP_INTERNAL_ERROR || response.code() == HTTP_UNAVAILABLE ||
response.code() == HTTP_BAD_GATEWAY || response.code() == HTTP_GATEWAY_TIMEOUT) {
Error error = new Gson().fromJson(body.get().string(), Error.class);
Error error = new Gson().fromJson(responseBody, Error.class);
if (error.getMessage() != null && !"".equals(error.getMessage())) {
throw new CentralClientException(msg + " reason:" + error.getMessage());
}
Expand Down Expand Up @@ -1711,23 +1713,21 @@
/**
* Handle unauthorized response.
*
* @param body response body
* @param mediaType mediaType
* @param responseBody response body
* @throws IOException when accessing response body
* @throws CentralClientException with unauthorized error message
*/
private void handleUnauthorizedResponse(Optional<ResponseBody> body)
private void handleUnauthorizedResponse(MediaType mediaType, String responseBody)
throws IOException, CentralClientException {
if (body.isPresent()) {
Optional<MediaType> contentType = Optional.ofNullable(body.get().contentType());
if (contentType.isPresent() && isApplicationJsonContentType(contentType.get().toString())) {
Error error = new Gson().fromJson(body.get().string(), Error.class);
throw new CentralClientException("unauthorized access token. " +
"check access token set in 'Settings.toml' file. reason: " + error.getMessage());
} else {
throw new CentralClientException("unauthorized access token. " +
"check access token set in 'Settings.toml' file.");
}
Optional<MediaType> contentType = Optional.ofNullable(mediaType);
StringBuilder message = new StringBuilder("unauthorized access token. " +

Check warning on line 1724 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1723-L1724

Added lines #L1723 - L1724 were not covered by tests
"check access token set in 'Settings.toml' file.");
if (contentType.isPresent() && isApplicationJsonContentType(contentType.get().toString())) {
Error error = new Gson().fromJson(responseBody, Error.class);
message.append("reason: ").append(error.getMessage());

Check warning on line 1728 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1727-L1728

Added lines #L1727 - L1728 were not covered by tests
}
throw new CentralClientException(message.toString());

Check warning on line 1730 in cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java

View check run for this annotation

Codecov / codecov/patch

cli/central-client/src/main/java/org/ballerinalang/central/client/CentralAPIClient.java#L1730

Added line #L1730 was not covered by tests
}

private void logResponseVerbose(Response response, String bodyContent) {
Expand Down
Loading