use of org.apache.syncope.common.lib.to.ErrorTO in project syncope by apache.
the class RestClientExceptionMapper method checkSyncopeClientCompositeException.
private SyncopeClientCompositeException checkSyncopeClientCompositeException(final Response response) {
SyncopeClientCompositeException compException = SyncopeClientException.buildComposite();
// Attempts to read ErrorTO or List<ErrorTO> as entity...
List<ErrorTO> errors = null;
try {
ErrorTO error = response.readEntity(ErrorTO.class);
if (error != null) {
errors = Collections.singletonList(error);
}
} catch (Exception e) {
LOG.debug("Could not read {}, attempting to read composite...", ErrorTO.class.getName(), e);
}
if (errors == null) {
try {
errors = response.readEntity(new GenericType<List<ErrorTO>>() {
});
} catch (Exception e) {
LOG.debug("Could not read {} list, attempting to read headers...", ErrorTO.class.getName(), e);
}
}
// ...if not possible, attempts to parse response headers
if (errors == null) {
List<String> exTypesInHeaders = response.getStringHeaders().get(RESTHeaders.ERROR_CODE);
if (exTypesInHeaders == null) {
LOG.debug("No " + RESTHeaders.ERROR_CODE + " provided");
return null;
}
List<String> exInfos = response.getStringHeaders().get(RESTHeaders.ERROR_INFO);
Set<String> handledExceptions = new HashSet<>();
exTypesInHeaders.forEach(exTypeAsString -> {
ClientExceptionType exceptionType = null;
try {
exceptionType = ClientExceptionType.fromHeaderValue(exTypeAsString);
} catch (IllegalArgumentException e) {
LOG.error("Unexpected value of " + RESTHeaders.ERROR_CODE + ": " + exTypeAsString, e);
}
if (exceptionType != null) {
handledExceptions.add(exTypeAsString);
SyncopeClientException clientException = SyncopeClientException.build(exceptionType);
if (exInfos != null && !exInfos.isEmpty()) {
for (String element : exInfos) {
if (element.startsWith(exceptionType.name())) {
clientException.getElements().add(StringUtils.substringAfter(element, ":"));
}
}
}
compException.addException(clientException);
}
});
exTypesInHeaders.removeAll(handledExceptions);
if (!exTypesInHeaders.isEmpty()) {
LOG.error("Unmanaged exceptions: " + exTypesInHeaders);
}
} else {
for (ErrorTO error : errors) {
SyncopeClientException clientException = SyncopeClientException.build(error.getType());
clientException.getElements().addAll(error.getElements());
compException.addException(clientException);
}
}
if (compException.hasExceptions()) {
return compException;
}
return null;
}
Aggregations