Search in sources :

Example 1 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class RetryableRestTemplate method execute.

private <T> T execute(HttpMethod method, Env env, String path, Object request, Class<T> responseType, Object... uriVariables) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String uri = uriTemplateHandler.expand(path, uriVariables).getPath();
    Transaction ct = Tracer.newTransaction("AdminAPI", uri);
    ct.addData("Env", env);
    List<ServiceDTO> services = getAdminServices(env, ct);
    HttpHeaders extraHeaders = assembleExtraHeaders(env);
    for (ServiceDTO serviceDTO : services) {
        try {
            T result = doExecute(method, extraHeaders, serviceDTO, path, request, responseType, uriVariables);
            ct.setStatus(Transaction.SUCCESS);
            ct.complete();
            return result;
        } catch (Throwable t) {
            logger.error("Http request failed, uri: {}, method: {}", uri, method, t);
            Tracer.logError(t);
            if (canRetry(t, method)) {
                Tracer.logEvent(TracerEventType.API_RETRY, uri);
            } else {
                // biz exception rethrow
                ct.setStatus(t);
                ct.complete();
                throw t;
            }
        }
    }
    // all admin server down
    ServiceException e = new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s", portalMetaDomainService.getDomain(env), services));
    ct.setStatus(e);
    ct.complete();
    throw e;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 2 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class AppService method save.

@Transactional
public App save(App entity) {
    if (!isAppIdUnique(entity.getAppId())) {
        throw new ServiceException("appId not unique");
    }
    // protection
    entity.setId(0);
    App app = appRepository.save(entity);
    auditService.audit(App.class.getSimpleName(), app.getId(), Audit.OP.INSERT, app.getDataChangeCreatedBy());
    return app;
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class AppNamespaceService method createDefaultAppNamespace.

@Transactional
public void createDefaultAppNamespace(String appId, String createBy) {
    if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {
        throw new ServiceException("appnamespace not unique");
    }
    AppNamespace appNs = new AppNamespace();
    appNs.setAppId(appId);
    appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);
    appNs.setComment("default app namespace");
    appNs.setFormat(ConfigFileFormat.Properties.getValue());
    appNs.setDataChangeCreatedBy(createBy);
    appNs.setDataChangeLastModifiedBy(createBy);
    appNamespaceRepository.save(appNs);
    auditService.audit(AppNamespace.class.getSimpleName(), appNs.getId(), Audit.OP.INSERT, createBy);
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class ConfigsImportService method forceImportNamespaceFromFile.

/**
 * force import, new items will overwrite existed items.
 */
public void forceImportNamespaceFromFile(final Env env, final String standardFilename, final InputStream zipInputStream) {
    String configText;
    try (InputStream in = zipInputStream) {
        configText = ConfigToFileUtils.fileToString(in);
    } catch (IOException e) {
        throw new ServiceException("Read config file errors:{}", e);
    }
    String operator = userInfoHolder.getUser().getUserId();
    this.importNamespaceFromText(env, standardFilename, configText, false, operator);
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 5 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class ConfigsExportService method exportApps.

private void exportApps(final Collection<Env> exportEnvs, OutputStream outputStream) {
    List<App> hasPermissionApps = findHasPermissionApps();
    if (CollectionUtils.isEmpty(hasPermissionApps)) {
        return;
    }
    try (final ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
        // write app info to zip
        writeAppInfoToZip(hasPermissionApps, zipOutputStream);
        // export app namespace
        exportAppNamespaces(zipOutputStream);
        // export app's clusters
        exportEnvs.parallelStream().forEach(env -> {
            try {
                this.exportClusters(env, hasPermissionApps, zipOutputStream);
            } catch (Exception e) {
                logger.error("export cluster error. env = {}", env, e);
            }
        });
    } catch (IOException e) {
        logger.error("export config error", e);
        throw new ServiceException("export config error", e);
    }
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ZipOutputStream(java.util.zip.ZipOutputStream) IOException(java.io.IOException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) IOException(java.io.IOException) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException)

Aggregations

ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)11 Transactional (org.springframework.transaction.annotation.Transactional)4 App (com.ctrip.framework.apollo.common.entity.App)3 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)3 IOException (java.io.IOException)3 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)2 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)2 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)1 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)1 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)1 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)1 InputStream (java.io.InputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Test (org.junit.Test)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1