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 tool pull bad-sad for unauthorized response #40775

Merged
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 @@ -698,7 +698,7 @@ public String[] pullTool(String toolId, String version, Path balaCacheDirPath, S

// Unauthorized access token
if (packagePullResponse.code() == HTTP_UNAUTHORIZED) {
handleUnauthorizedResponse(body);
handleUnauthorizedResponse(body, pkgPullResBodyContent);
}

if (body.isPresent()) {
Expand Down Expand Up @@ -1488,6 +1488,7 @@ public JsonObject getTrigger(String id, String supportedPlatform, String balleri
*
* @param org org name
* @param body response body
* @param responseBody error message
* @throws IOException when accessing response body
* @throws CentralClientException with unauthorized error message
*/
Expand All @@ -1506,6 +1507,29 @@ private void handleUnauthorizedResponse(String org, Optional<ResponseBody> body,
}
}

/**
* Handle unauthorized response.
*
* @param body response body
* @param responseBody error message
* @throws IOException when accessing response body
* @throws CentralClientException with unauthorized error message
*/
private void handleUnauthorizedResponse(Optional<ResponseBody> body, 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(responseBody, 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.");
}
}
}

/**
* Handle unauthorized response.
*
Expand Down