use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.
the class RetryableRestTemplate method exchangeGet.
private <T> ResponseEntity<T> exchangeGet(Env env, String path, ParameterizedTypeReference<T> reference, 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);
HttpEntity<Void> entity = new HttpEntity<>(assembleExtraHeaders(env));
for (ServiceDTO serviceDTO : services) {
try {
ResponseEntity<T> result = restTemplate.exchange(parseHost(serviceDTO) + path, HttpMethod.GET, entity, reference, uriVariables);
ct.setStatus(Transaction.SUCCESS);
ct.complete();
return result;
} catch (Throwable t) {
logger.error("Http request failed, uri: {}, method: {}", uri, HttpMethod.GET, t);
Tracer.logError(t);
if (canRetry(t, HttpMethod.GET)) {
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;
}
use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.
the class ConfigsExportController method exportItems.
/**
* export one config as file.
* keep compatibility.
* file name examples:
* <pre>
* application.properties
* application.yml
* application.json
* </pre>
*/
@PreAuthorize(value = "!@permissionValidator.shouldHideConfigToCurrentUser(#appId, #env, #namespaceName)")
@GetMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/export")
public void exportItems(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, HttpServletResponse res) {
List<String> fileNameSplit = Splitter.on(".").splitToList(namespaceName);
String fileName = namespaceName;
// properties file or public namespace has not suffix (.properties)
if (fileNameSplit.size() <= 1 || !ConfigFileFormat.isValidFormat(fileNameSplit.get(fileNameSplit.size() - 1))) {
fileName = Joiner.on(".").join(namespaceName, ConfigFileFormat.Properties.getValue());
}
NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName);
// generate a file.
res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
// file content
final String configFileContent = NamespaceBOUtils.convert2configFileContent(namespaceBO);
try {
// write content to net
res.getOutputStream().write(configFileContent.getBytes());
} catch (Exception e) {
throw new ServiceException("export items failed:{}", e);
}
}
use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.
the class RetryableRestTemplate method getAdminServices.
private List<ServiceDTO> getAdminServices(Env env, Transaction ct) {
List<ServiceDTO> services = adminServiceAddressLocator.getServiceList(env);
if (CollectionUtils.isEmpty(services)) {
ServiceException e = new ServiceException(String.format("No available admin server." + " Maybe because of meta server down or all admin server down. " + "Meta server address: %s", portalMetaDomainService.getDomain(env)));
ct.setStatus(e);
ct.complete();
throw e;
}
return services;
}
use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.
the class NamespaceService method save.
@Transactional
public Namespace save(Namespace entity) {
if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
throw new ServiceException("namespace not unique");
}
// protection
entity.setId(0);
Namespace namespace = namespaceRepository.save(entity);
auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT, namespace.getDataChangeCreatedBy());
return namespace;
}
use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.
the class ControllerExceptionTest method createFailed.
@Test(expected = ServiceException.class)
public void createFailed() {
AppDTO dto = generateSampleDTOData();
when(appService.findOne(any(String.class))).thenReturn(null);
when(adminService.createNewApp(any(App.class))).thenThrow(new ServiceException("create app failed"));
appController.create(dto);
}
Aggregations