下面列出了org.apache.commons.lang3.exception.ExceptionUtils#getThrowableList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public BackgroundException map(final ApiException failure) {
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(cause instanceof SocketException) {
// Map Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Broken pipe
return new DefaultSocketExceptionMappingService().map((SocketException) cause);
}
if(cause instanceof HttpResponseException) {
return new DefaultHttpResponseExceptionMappingService().map((HttpResponseException) cause);
}
if(cause instanceof IOException) {
return new DefaultIOExceptionMappingService().map((IOException) cause);
}
if(cause instanceof IllegalStateException) {
// Caused by: ch.cyberduck.core.sds.io.swagger.client.ApiException: javax.ws.rs.ProcessingException: java.lang.IllegalStateException: Connection pool shut down
return new ConnectionCanceledException(cause);
}
}
return new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(failure.getCode(), failure.getMessage()));
}
@Override
public BackgroundException map(final IOException failure) {
final Throwable[] stack = ExceptionUtils.getThrowables(failure);
for(Throwable t : stack) {
if(t instanceof BackgroundException) {
return (BackgroundException) t;
}
}
if(failure instanceof SSLException) {
return new SSLExceptionMappingService().map((SSLException) failure);
}
final StringBuilder buffer = new StringBuilder();
this.append(buffer, failure.getMessage());
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(!StringUtils.contains(failure.getMessage(), cause.getMessage())) {
this.append(buffer, cause.getMessage());
}
}
return this.wrap(failure, buffer);
}
@Override
public boolean handle(ErrorEvent event, App app) {
Throwable exception = event.getThrowable();
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (classNames.contains(throwable.getClass().getName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(app, throwable.getClass().getName(), throwable.getMessage(), throwable);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (classNames.contains(cause.getClassName())
&& canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) {
doHandle(app, cause.getClassName(), cause.getMessage(), cause.getThrowable());
return true;
}
}
}
}
return false;
}
@SuppressWarnings("unchecked")
public RemoteException(Throwable throwable) {
List<Throwable> list = ExceptionUtils.getThrowableList(throwable);
for (int i = 0; i < list.size(); i++) {
Throwable t = list.get(i);
boolean suitable = true;
List<Throwable> causesOfT = list.subList(i, list.size());
for (Throwable aCauseOfT : causesOfT) {
if (!isSuitable(aCauseOfT)) {
suitable = false;
break;
}
}
if (suitable)
causes.add(new Cause(t));
else
causes.add(new Cause(t.getClass().getName(), t.getMessage()));
}
}
@Override
public boolean handle(Thread thread, Throwable exception) {
//noinspection unchecked
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (classNames.contains(throwable.getClass().getName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(thread, throwable.getClass().getName(), throwable.getMessage(), throwable);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (classNames.contains(cause.getClassName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(thread, cause.getClassName(), cause.getMessage(), cause.getThrowable());
return true;
}
}
}
}
return false;
}
@Override
public boolean handle(Thread thread, Throwable exception) {
@SuppressWarnings("unchecked")
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (throwable instanceof RemoteAccessException) {
Messages messages = AppBeans.get(Messages.NAME);
String msg = messages.getMessage(getClass(), "connectException.message");
if (throwable.getCause() == null) {
App.getInstance().getMainFrame().showNotification(msg, Frame.NotificationType.ERROR);
} else {
String description = messages.formatMessage(getClass(), "connectException.description",
throwable.getCause().toString());
App.getInstance().getMainFrame().showNotification(msg, description, Frame.NotificationType.ERROR);
}
return true;
}
}
return false;
}
@Override
public boolean handle(Throwable exception, WindowManager windowManager) {
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (classNames.contains(throwable.getClass().getName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(throwable.getClass().getName(), throwable.getMessage(), throwable, windowManager);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (classNames.contains(cause.getClassName())
&& canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) {
doHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable(), windowManager);
return true;
}
}
}
}
return false;
}
@Override
public boolean handle(Throwable exception, UiContext context) {
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (classNames.contains(throwable.getClass().getName())
&& canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) {
doHandle(throwable.getClass().getName(), throwable.getMessage(), throwable, context);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (classNames.contains(cause.getClassName())
&& canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) {
doHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable(), context);
return true;
}
}
}
}
return false;
}
@Override
public boolean handle(Throwable exception, WindowManager windowManager) {
List<Throwable> list = ExceptionUtils.getThrowableList(exception);
for (Throwable throwable : list) {
if (className.contains(throwable.getClass().getName()) && isDateOutOfRangeMessage(throwable.getMessage())) {
doHandle(windowManager);
return true;
}
if (throwable instanceof RemoteException) {
RemoteException remoteException = (RemoteException) throwable;
for (RemoteException.Cause cause : remoteException.getCauses()) {
if (className.contains(cause.getClassName()) && isDateOutOfRangeMessage(throwable.getMessage())) {
doHandle(windowManager);
return true;
}
}
}
}
return false;
}
/**
* Method for handling exception of type {@link MultipartException} which is
* thrown in case the request body is not well formed and cannot be
* deserialized. Called by the Spring-Framework for exception handling.
*
* @param request
* the Http request
* @param ex
* the exception which occurred
* @return the entity to be responded containing the exception information
* as entity.
*/
@ExceptionHandler(MultipartException.class)
public ResponseEntity<ExceptionInfo> handleMultipartException(final HttpServletRequest request,
final Exception ex) {
logRequest(request, ex);
final List<Throwable> throwables = ExceptionUtils.getThrowableList(ex);
final Throwable responseCause = Iterables.getLast(throwables);
if (responseCause.getMessage().isEmpty()) {
LOG.warn("Request {} lead to MultipartException without root cause message:\n{}", request.getRequestURL(),
ex.getStackTrace());
}
final ExceptionInfo response = createExceptionInfo(new MultiPartFileUploadException(responseCause));
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@Override
public BackgroundException map(final StorageException failure) {
final StringBuilder buffer = new StringBuilder();
this.append(buffer, failure.getMessage());
if(ExceptionUtils.getRootCause(failure) instanceof UnknownHostException) {
return new NotfoundException(buffer.toString(), failure);
}
switch(failure.getHttpStatusCode()) {
case 403:
return new LoginFailureException(buffer.toString(), failure);
case 404:
return new NotfoundException(buffer.toString(), failure);
case 304:
case 405:
case 400:
case 411:
case 412:
return new InteroperabilityException(buffer.toString(), failure);
case 500:
// InternalError
// OperationTimedOut
return new ConnectionTimeoutException(buffer.toString(), failure);
case 503:
// ServerBusy
return new RetriableAccessDeniedException(buffer.toString(), failure);
}
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(cause instanceof SSLException) {
return new SSLExceptionMappingService().map(buffer.toString(), (SSLException) cause);
}
}
return this.wrap(failure, buffer);
}
protected BackgroundException wrap(final T failure, final String title, final StringBuilder buffer) {
if(buffer.toString().isEmpty()) {
log.warn(String.format("No message for failure %s", failure));
this.append(buffer, LocaleFactory.localizedString("Unknown"));
}
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(cause instanceof InterruptedIOException) {
// Handling socket timeouts
return new ConnectionTimeoutException(buffer.toString(), failure);
}
if(cause instanceof TimeoutException) {
//
return new ConnectionTimeoutException(buffer.toString(), failure);
}
if(cause instanceof SocketException) {
return new DefaultSocketExceptionMappingService().map((SocketException) cause);
}
if(cause instanceof EOFException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof UnknownHostException) {
return new ResolveFailedException(buffer.toString(), failure);
}
if(cause instanceof NoHttpResponseException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof ConnectionClosedException) {
return new ConnectionRefusedException(buffer.toString(), failure);
}
if(cause instanceof InterruptedException) {
return new ConnectionCanceledException(buffer.toString(), failure);
}
}
if(failure instanceof RuntimeException) {
return new ConnectionCanceledException(title, buffer.toString(), failure);
}
return new BackgroundException(title, buffer.toString(), failure);
}
/**
* close_notify(0),
* unexpected_message(10),
* bad_record_mac(20),
* decryption_failed_RESERVED(21),
* record_overflow(22),
* decompression_failure(30),
* handshake_failure(40),
* no_certificate_RESERVED(41),
* bad_certificate(42),
* unsupported_certificate(43),
* certificate_revoked(44),
* certificate_expired(45),
* certificate_unknown(46),
* illegal_parameter(47),
* unknown_ca(48),
* access_denied(49),
* decode_error(50),
* decrypt_error(51),
* export_restriction_RESERVED(60),
* protocol_version(70),
* insufficient_security(71),
* internal_error(80),
* user_canceled(90),
* no_renegotiation(100),
* unsupported_extension(110),
*/
@Override
public BackgroundException map(final SSLException failure) {
final StringBuilder buffer = new StringBuilder();
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(cause instanceof SocketException) {
// Connection has been shutdown: javax.net.ssl.SSLException: java.net.SocketException: Broken pipe
return new DefaultSocketExceptionMappingService().map((SocketException) cause);
}
}
final String message = failure.getMessage();
for(Alert alert : Alert.values()) {
if(StringUtils.containsIgnoreCase(message, alert.name())) {
this.append(buffer, alert.getDescription());
break;
}
}
if(failure instanceof SSLHandshakeException) {
if(ExceptionUtils.getRootCause(failure) instanceof CertificateException) {
log.warn(String.format("Ignore certificate failure %s and drop connection", failure.getMessage()));
// Server certificate not accepted
return new ConnectionCanceledException(failure);
}
if(ExceptionUtils.getRootCause(failure) instanceof EOFException) {
// SSL peer shut down incorrectly
return this.wrap(failure, buffer);
}
return new SSLNegotiateException(buffer.toString(), failure);
}
if(ExceptionUtils.getRootCause(failure) instanceof GeneralSecurityException) {
this.append(buffer, ExceptionUtils.getRootCause(failure).getMessage());
return new InteroperabilityException(buffer.toString(), failure);
}
this.append(buffer, message);
return new InteroperabilityException(buffer.toString(), failure);
}
@Override
public BackgroundException map(final Throwable failure) {
final StringBuilder buffer = new StringBuilder();
if(failure instanceof RuntimeException) {
this.append(buffer, LocaleFactory.localizedString("Unknown application error"));
}
this.append(buffer, failure.getMessage());
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(!StringUtils.contains(failure.getMessage(), cause.getMessage())) {
this.append(buffer, cause.getMessage());
}
}
return this.wrap(failure, LocaleFactory.localizedString("Error", "Error"), buffer);
}
protected String getExceptionCauseMessage(Exception exception) {
for (Throwable throwable : ExceptionUtils.getThrowableList(exception)) {
if (throwable instanceof RemoteAccessException) {
return throwable.toString() +
"\n\nDue to this error, 'web' block cannot connect to the remote 'core' block.\n" +
"First, check the 'core' server log for exceptions to ensure it has started properly.\n" +
"If there are no exceptions in the 'core' log, check that 'cuba.connectionUrlList' property value " +
"contains the valid address of the 'core' server and ends with the web context name of the 'core' block, " +
"e.g. 'cuba.connectionUrlList = http://somehost:8080/app-core'";
} else if (throwable instanceof LocalServiceAccessException) {
return throwable.getMessage();
}
}
return ExceptionUtils.getRootCauseMessage(exception);
}
@Override
@Nullable
protected String searchRemotely(String pack, String key, Locale locale) {
if (!remoteSearch || !AppContext.isStarted())
return null;
if (log.isTraceEnabled())
log.trace("searchRemotely: " + pack + "/" + LocaleResolver.localeToString(locale) + "/" + key);
StopWatch stopWatch = new Slf4JStopWatch("Messages.searchRemotely");
try {
String message = localizedMessageService.getMessage(pack, key, locale);
if (key.equals(message))
return null;
else
return message;
} catch (Exception e) {
List list = ExceptionUtils.getThrowableList(e);
for (Object throwable : list) {
if (throwable instanceof SocketException) {
log.trace("searchRemotely: {}", throwable);
return null; // silently ignore network errors
}
}
throw (RuntimeException) e;
} finally {
stopWatch.stop();
}
}
@Override
public Throwable handleError(RetrofitError retrofitError) {
if (retrofitError.isNetworkError()) {
Log.w(TAG, "Cannot connect to " + retrofitError.getUrl());
return new NoNetworkException();
}
Response response = retrofitError.getResponse();
if (response != null) {
int status = response.getStatus();
if (status == 401) {
//throw our own exception about unauthorized access
Log.w(TAG, "Access in not authorized " + retrofitError.getUrl());
Context context = AppContext.getContext();
AuthHelper.reCreateAuthTokenForLastAccountBlocking(context, Constants.ACCOUNT_TYPE, Constants.ACCOUNT_TOKEN_TYPE, null, null, null);
return new UnauthorizedException("Access in not authorized " + retrofitError.getUrl(), retrofitError);
} else if (status >= 300) {
Log.w(TAG, "Error " + String.valueOf(status) + " while accessing " + retrofitError.getUrl());
return retrofitError;
}
}
int index = ExceptionUtils.indexOfType(retrofitError, ServerErrorException.class);
if (index >= 0) {
List<Throwable> errorList = ExceptionUtils.getThrowableList(retrofitError);
ServerErrorException serverErrorException = (ServerErrorException)errorList.get(index);
if (serverErrorException instanceof DeveloperErrorException) {
Log.e(TAG, "Developer error with code" + serverErrorException.getErrorCode(), serverErrorException);
}
return serverErrorException;
}
return retrofitError;
}
@Override
public Type determine(final BackgroundException failure) {
if(log.isDebugEnabled()) {
log.debug(String.format("Determine cause for failure %s", failure));
}
for(Throwable cause : ExceptionUtils.getThrowableList(failure)) {
if(failure instanceof UnsupportedException) {
return Type.unsupported;
}
if(failure instanceof LoginFailureException) {
return Type.login;
}
if(cause instanceof ResolveFailedException) {
return Type.network;
}
if(failure instanceof ConnectionCanceledException) {
return Type.cancel;
}
if(cause instanceof ConnectionTimeoutException) {
return Type.network;
}
if(cause instanceof ConnectionRefusedException) {
return Type.network;
}
if(cause instanceof SSLNegotiateException) {
return Type.application;
}
if(cause instanceof SSLHandshakeException) {
return Type.application;
}
if(cause instanceof SSLException) {
return Type.network;
}
if(cause instanceof NoHttpResponseException) {
return Type.network;
}
if(cause instanceof ConnectTimeoutException) {
return Type.network;
}
if(cause instanceof SocketException
|| cause instanceof IOResumeException
|| cause instanceof TimeoutException // Used in Promise#retrieve
|| cause instanceof SocketTimeoutException
|| cause instanceof UnknownHostException) {
return Type.network;
}
}
return Type.application;
}