Skip to content

Commit

Permalink
Align the unreachable devfile exception message (#615)
Browse files Browse the repository at this point in the history
Add a common exception message and use it for cases when:

The devfile location is unavailable.
The devfile content is is not valid: e.g the response content is an html content of an scm authorisation page.
  • Loading branch information
vinokurig authored Nov 28, 2023
1 parent f34e804 commit 5baa3a7
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .ci/openshift-ci/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ testFactoryResolverNoPatOAuth() {
echo "[INFO] Check factory resolver for public repository with NO PAT/OAuth setup"
testFactoryResolverResponse $1 200

echo "[INFO] Check factory resolver for private repository with NO PAT/OAuth setup"
testFactoryResolverResponse $2 500
}

# check that raw devfile url factory resolver returns correct value without any PAT/OAuth setup
testFactoryResolverNoPatOAuthRaw() {
echo "[INFO] Check factory resolver for public repository with NO PAT/OAuth setup"
testFactoryResolverResponse $1 200

echo "[INFO] Check factory resolver for private repository with NO PAT/OAuth setup"
testFactoryResolverResponse $2 400
}
Expand Down
3 changes: 1 addition & 2 deletions .ci/openshift-ci/test-azure-no-pat-oauth-flow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ source "${SCRIPT_DIR}"/common.sh
trap "catchFinish" EXIT SIGINT

setupTestEnvironment ${OCP_NON_ADMIN_USER_NAME}
# due to the issue https://github.com/eclipse/che/issues/22469
# testFactoryResolverNoPatOAuth ${PUBLIC_REPO_URL} ${PRIVATE_REPO_URL}
testFactoryResolverNoPatOAuth ${PUBLIC_REPO_URL} ${PRIVATE_REPO_URL}
testCloneGitRepoProjectShouldExists ${PUBLIC_REPO_WORKSPACE_NAME} ${PUBLIC_PROJECT_NAME} ${PUBLIC_REPO_URL} ${USER_CHE_NAMESPACE}
deleteTestWorkspace ${PUBLIC_REPO_WORKSPACE_NAME} ${USER_CHE_NAMESPACE}
testCloneGitRepoNoProjectExists ${PRIVATE_REPO_WORKSPACE_NAME} ${PRIVATE_PROJECT_NAME} ${PRIVATE_REPO_URL} ${USER_CHE_NAMESPACE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ source "${SCRIPT_DIR}"/common.sh
trap "catchFinish" EXIT SIGINT

setupTestEnvironment ${OCP_NON_ADMIN_USER_NAME}
testFactoryResolverNoPatOAuth ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}
testFactoryResolverNoPatOAuthRaw ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}

testCloneGitRepoProjectShouldExists ${PUBLIC_REPO_WORKSPACE_NAME} ${PUBLIC_PROJECT_NAME} ${PUBLIC_REPO_RAW_PATH_URL} ${USER_CHE_NAMESPACE}
deleteTestWorkspace ${PUBLIC_REPO_WORKSPACE_NAME} ${USER_CHE_NAMESPACE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ source "${SCRIPT_DIR}"/common.sh
trap "catchFinish" EXIT SIGINT

setupTestEnvironment ${OCP_NON_ADMIN_USER_NAME}
testFactoryResolverNoPatOAuth ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}
testFactoryResolverNoPatOAuthRaw ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}

testCloneGitRepoNoProjectExists ${PUBLIC_REPO_WORKSPACE_NAME} ${PUBLIC_PROJECT_NAME} ${PUBLIC_REPO_RAW_PATH_URL} ${USER_CHE_NAMESPACE}
deleteTestWorkspace ${PUBLIC_REPO_WORKSPACE_NAME} ${USER_CHE_NAMESPACE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ source "${SCRIPT_DIR}"/common.sh
trap "catchFinish" EXIT SIGINT

setupTestEnvironment ${OCP_NON_ADMIN_USER_NAME}
testFactoryResolverNoPatOAuth ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}
testFactoryResolverNoPatOAuthRaw ${PUBLIC_REPO_RAW_PATH_URL} ${PRIVATE_REPO_RAW_PATH_URL}

testCloneGitRepoNoProjectExists ${PUBLIC_REPO_WORKSPACE_NAME} ${PUBLIC_PROJECT_NAME} ${PUBLIC_REPO_RAW_PATH_URL} ${USER_CHE_NAMESPACE}
deleteTestWorkspace ${PUBLIC_REPO_WORKSPACE_NAME} ${USER_CHE_NAMESPACE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher.OAUTH_2_PREFIX;
import static org.eclipse.che.api.factory.server.scm.exception.ExceptionMessages.getDevfileConnectionErrorMessage;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -123,7 +124,7 @@ protected String fetchContentWithoutToken(String requestURL)
}
}
throw new DevfileException(
"Could not reach devfile at " + "`" + exception.getMessage() + "`", exception);
getDevfileConnectionErrorMessage(exception.getMessage()), exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2012-2023 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.api.factory.server.scm.exception;

public class ExceptionMessages {
public static String getDevfileConnectionErrorMessage(String location) {
return String.format("Could not reach devfile at %s", location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.che.api.factory.server.ApiExceptionMapper.toApiException;
import static org.eclipse.che.api.factory.server.scm.exception.ExceptionMessages.getDevfileConnectionErrorMessage;
import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION;
import static org.eclipse.che.api.workspace.server.devfile.Constants.CURRENT_API_VERSION;
import static org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE;
Expand Down Expand Up @@ -130,7 +131,7 @@ public Optional<FactoryMetaDto> createFactoryFromDevfile(
continue;
} catch (DevfileException e) {
LOG.debug("Unexpected devfile exception: {}", e.getMessage());
throw toApiException(e, location);
throw new ApiException(e.getMessage());
}
if (isNullOrEmpty(devfileYamlContent)) {
return Optional.empty();
Expand All @@ -142,7 +143,7 @@ public Optional<FactoryMetaDto> createFactoryFromDevfile(
try {
devfileVersionDetector.devfileVersion(parsedDevfile);
} catch (DevfileException e) {
throw new ApiException("Failed to fetch devfile");
throw new ApiException(getDevfileConnectionErrorMessage(devfileLocation));
}
return Optional.of(
createFactory(parsedDevfile, overrideProperties, fileContentProvider, location));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ public String location() {
}
}));

when(fileContentProvider.fetchContent(anyString())).thenThrow(new DevfileException("", cause));
when(fileContentProvider.fetchContent(anyString()))
.thenThrow(new DevfileException(expectedMessage, cause));

// when
try {
Expand All @@ -457,7 +458,7 @@ public String location() {

@Test(
expectedExceptions = ApiException.class,
expectedExceptionsMessageRegExp = "Failed to fetch devfile")
expectedExceptionsMessageRegExp = "Could not reach devfile at location")
public void shouldThrowErrorOnUnsupportedDevfileContent()
throws ApiException, DevfileException, IOException {
JsonNode jsonNode = mock(JsonNode.class);
Expand Down

0 comments on commit 5baa3a7

Please sign in to comment.