|
| 1 | +/* |
| 2 | + * Copyright 2019-2024 CloudNetService team & contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package eu.cloudnetservice.ext.rest.netty; |
| 18 | + |
| 19 | +import io.netty5.channel.ChannelException; |
| 20 | +import io.netty5.handler.timeout.ReadTimeoutException; |
| 21 | +import io.netty5.util.concurrent.FutureListener; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.channels.ClosedChannelException; |
| 24 | +import java.util.Locale; |
| 25 | +import javax.net.ssl.SSLException; |
| 26 | +import lombok.NonNull; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * A utility class for logging exceptions that occur while processing a request. |
| 32 | + * |
| 33 | + * @since 1.0 |
| 34 | + */ |
| 35 | +final class NettyExceptionLogger { |
| 36 | + |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(NettyExceptionLogger.class); |
| 38 | + |
| 39 | + /** |
| 40 | + * A future listener that logs the exception produced by the future to which it was attached if it is relevant enough. |
| 41 | + * If the exception is deemed to be irrelevant in the current context, it is silently ignored. |
| 42 | + */ |
| 43 | + public static final FutureListener<Object> LOG_ON_FAILURE = future -> { |
| 44 | + if (future.isFailed() && !future.isCancelled()) { |
| 45 | + NettyExceptionLogger.handleConnectionException(future.cause()); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + private NettyExceptionLogger() { |
| 50 | + throw new UnsupportedOperationException(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Logs the given exception if it is relevant enough, silently ignoring it otherwise. |
| 55 | + * |
| 56 | + * @param cause the cause to log if it is relevant enough. |
| 57 | + * @throws NullPointerException if the given cause is null. |
| 58 | + */ |
| 59 | + static void handleConnectionException(@NonNull Throwable cause) { |
| 60 | + if (LOGGER.isTraceEnabled()) { |
| 61 | + LOGGER.trace("Caught exception while processing rest request", cause); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (cause instanceof ClosedChannelException || cause instanceof ReadTimeoutException) { |
| 66 | + // happens when trying to write to a channel that was improperly closed by the remote |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + var message = cause.getMessage(); |
| 71 | + if (message != null) { |
| 72 | + var lowerMessage = message.toLowerCase(Locale.ROOT); |
| 73 | + if (cause instanceof SSLException && lowerMessage.contains("closed already")) { |
| 74 | + // happens when remote closes the connection while ssl-related work is in progress |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if ((cause instanceof IOException || cause instanceof ChannelException) && canIgnoreException(lowerMessage)) { |
| 79 | + // some sort of socket error that can safely be ignored |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + LOGGER.warn("Caught exception while processing rest request", cause); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks if the given error associated with the given message can be safely ignored. |
| 89 | + * |
| 90 | + * @param message the error message to check. |
| 91 | + * @return true if the error associated with the given message can be safely ignored, false otherwise. |
| 92 | + * @throws NullPointerException if the given message is null. |
| 93 | + */ |
| 94 | + private static boolean canIgnoreException(@NonNull String message) { |
| 95 | + var broken = message.contains("broken"); |
| 96 | + if (broken && message.contains("pipe")) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + if (!message.contains("connection")) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // connection related with an ignorable state |
| 105 | + return broken || message.contains("closed") || message.contains("reset") || message.contains("abort"); |
| 106 | + } |
| 107 | +} |
0 commit comments