Search in sources :

Example 51 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class DevfileToApiExceptionMapperTest method shouldReturnBadrequestExceptionWhenCauseIsOtherException.

@Test
public void shouldReturnBadrequestExceptionWhenCauseIsOtherException() {
    ScmItemNotFoundException itemNotFoundException = new ScmItemNotFoundException("unknown");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", itemNotFoundException));
    assertTrue(exception instanceof BadRequestException);
}
Also used : ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) BadRequestException(org.eclipse.che.api.core.BadRequestException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 52 with DevfileException

use of org.eclipse.che.api.workspace.server.devfile.exception.DevfileException in project devspaces-images by redhat-developer.

the class AuthorizingFileContentProvider method fetchContent.

@Override
public String fetchContent(String fileURL) throws IOException, DevfileException {
    final String requestURL = formatUrl(fileURL);
    try {
        Optional<PersonalAccessToken> token = personalAccessTokenManager.get(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
        if (token.isPresent()) {
            PersonalAccessToken personalAccessToken = token.get();
            String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
            gitCredentialManager.createOrReplace(personalAccessToken);
            return content;
        } else {
            try {
                return urlFetcher.fetch(requestURL);
            } catch (IOException exception) {
                if (exception instanceof SSLException) {
                    ScmCommunicationException cause = new ScmCommunicationException(String.format("Failed to fetch a content from URL %s due to TLS key misconfiguration. Please refer to the docs about how to correctly import it. ", requestURL));
                    throw new DevfileException(exception.getMessage(), cause);
                } else if (exception instanceof FileNotFoundException) {
                    if (isPublicRepository(remoteFactoryUrl)) {
                        // for public repo-s return 404 as-is
                        throw exception;
                    }
                }
                // unable to determine exact cause, so let's just try to authorize...
                try {
                    PersonalAccessToken personalAccessToken = personalAccessTokenManager.fetchAndSave(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
                    String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
                    gitCredentialManager.createOrReplace(personalAccessToken);
                    return content;
                } catch (ScmUnauthorizedException | UnknownScmProviderException e) {
                    throw new DevfileException(e.getMessage(), e);
                } catch (ScmCommunicationException e) {
                    throw new IOException(String.format("Failed to fetch a content from URL %s. Make sure the URL" + " is correct. For private repository, make sure authentication is configured." + " Additionally, if you're using " + " relative form, make sure the referenced file are actually stored" + " relative to the devfile on the same host," + " or try to specify URL in absolute form. The current attempt to authenticate" + " request, failed with the following error message: %s", fileURL, e.getMessage()), e);
                }
            }
        }
    } catch (ScmConfigurationPersistenceException | UnsatisfiedScmPreconditionException | ScmUnauthorizedException | ScmCommunicationException e) {
        throw new DevfileException(e.getMessage(), e);
    }
}
Also used : ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) UnsatisfiedScmPreconditionException(org.eclipse.che.api.factory.server.scm.exception.UnsatisfiedScmPreconditionException) ScmConfigurationPersistenceException(org.eclipse.che.api.factory.server.scm.exception.ScmConfigurationPersistenceException)

Aggregations

DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)52 Test (org.testng.annotations.Test)22 IOException (java.io.IOException)20 ApiException (org.eclipse.che.api.core.ApiException)18 FileContentProvider (org.eclipse.che.api.workspace.server.devfile.FileContentProvider)18 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)16 HashMap (java.util.HashMap)14 List (java.util.List)14 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 Map (java.util.Map)12 Optional (java.util.Optional)12 ServerException (org.eclipse.che.api.core.ServerException)12 ExtendedError (org.eclipse.che.api.core.rest.shared.dto.ExtendedError)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)12 ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 Collections (java.util.Collections)10 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)10 UnknownScmProviderException (org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException)10