下面列出了怎么用io.netty.channel.unix.Errors的API类实例代码及写法,或者点击链接到github查看源代码。
private void fireWriteError(String requestPart, Throwable cause, ChannelHandlerContext ctx) throws Exception {
final String errMesg = String.format("Error writing %s to client", requestPart);
if (cause instanceof java.nio.channels.ClosedChannelException ||
cause instanceof Errors.NativeIoException) {
LOG.info(errMesg + " - client connection is closed.");
if (zuulRequest != null) {
zuulRequest.getContext().cancel();
StatusCategoryUtils.storeStatusCategoryIfNotAlreadyFailure(zuulRequest.getContext(), ZuulStatusCategory.FAILURE_CLIENT_CANCELLED);
}
}
else {
LOG.error(errMesg, cause);
ctx.fireExceptionCaught(new ZuulException(cause, errMesg, true));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{
incrementExceptionCounter(cause);
if (cause instanceof Http2Exception) {
Http2Exception h2e = (Http2Exception) cause;
if (h2e.error() == Http2Error.NO_ERROR
&& Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN.equals(h2e.shutdownHint())) {
// This is the exception we threw ourselves to make the http2 codec gracefully close the connection. So just
// swallow it so that it doesn't propagate and get logged.
LOG.debug("Swallowed Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN ", cause);
}
else {
super.exceptionCaught(ctx, cause);
}
}
else if (cause instanceof Errors.NativeIoException) {
LOG.debug("Swallowed NativeIoException", cause);
}
else {
super.exceptionCaught(ctx, cause);
}
}
@Override
protected void assertRstOnCloseException(IOException cause, Channel clientChannel) {
if (!AbstractKQueueChannel.class.isInstance(clientChannel)) {
super.assertRstOnCloseException(cause, clientChannel);
return;
}
assertTrue("actual [type, message]: [" + cause.getClass() + ", " + cause.getMessage() + "]",
cause instanceof NativeIoException);
assertEquals(Errors.ERRNO_ECONNRESET_NEGATIVE, ((NativeIoException) cause).expectedErr());
}
@Override
protected void assertRstOnCloseException(IOException cause, Channel clientChannel) {
if (!AbstractEpollChannel.class.isInstance(clientChannel)) {
super.assertRstOnCloseException(cause, clientChannel);
return;
}
assertTrue("actual [type, message]: [" + cause.getClass() + ", " + cause.getMessage() + "]",
cause instanceof NativeIoException);
assertEquals(Errors.ERRNO_ECONNRESET_NEGATIVE, ((NativeIoException) cause).expectedErr());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (ctx.channel().isActive()
&& !(cause instanceof Errors.NativeIoException)
&& !(cause instanceof IOException)) {
log.info("service exception, ex={}", cause.getMessage());
}
log.debug("meet exception, may be connection is closed, msg={}", cause.getMessage());
log.debug("remove from channel map");
ChannelManager.getInstance().removeChannel(ctx.channel());
ctx.close();
}
/**
* @param spec A {@link Consumer} that provides a "blank" {@link MessageCreateSpec} to be operated on.
* @param channel The {@link MessageChannel} in which to send the message.
* @param hasEmbed Whether or not the spec contains an embed.
* @return A {@link Mono} where, upon successful completion, emits the created Message. If an error is received,
* it is emitted through the Mono.
*/
public static Mono<Message> sendMessage(Consumer<MessageCreateSpec> spec, MessageChannel channel, boolean hasEmbed) {
return Mono.zip(
DiscordUtils.hasPermission(channel, channel.getClient().getSelfId(), Permission.SEND_MESSAGES),
DiscordUtils.hasPermission(channel, channel.getClient().getSelfId(), Permission.EMBED_LINKS))
.flatMap(TupleUtils.function((canSendMessage, canSendEmbed) -> {
if (!canSendMessage) {
DEFAULT_LOGGER.info("{Channel ID: {}} Missing permission: {}",
channel.getId().asLong(), FormatUtils.capitalizeEnum(Permission.SEND_MESSAGES));
return Mono.empty();
}
if (!canSendEmbed && hasEmbed) {
DEFAULT_LOGGER.info("{Channel ID: {}} Missing permission: {}",
channel.getId().asLong(), FormatUtils.capitalizeEnum(Permission.EMBED_LINKS));
return DiscordUtils.sendMessage(String.format(Emoji.ACCESS_DENIED + " I cannot send embed" +
" links.%nPlease, check my permissions "
+ "and channel-specific ones to verify that **%s** is checked.",
FormatUtils.capitalizeEnum(Permission.EMBED_LINKS)), channel);
}
return channel.createMessage(spec
.andThen(messageSpec -> messageSpec.setAllowedMentions(AllowedMentions.builder()
.parseType(AllowedMentions.Type.ROLE, AllowedMentions.Type.USER)
.build())));
}))
// 403 Forbidden means that the bot is not in the guild
.onErrorResume(ClientException.isStatusCode(HttpResponseStatus.FORBIDDEN.code()), err -> Mono.empty())
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1))
.filter(err -> err instanceof PrematureCloseException || err instanceof Errors.NativeIoException));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{
passport(ctx).add(PassportState.SERVER_CH_EXCEPTION);
if (cause instanceof Errors.NativeIoException) {
LOG.debug("PassportStateServerHandler Inbound NativeIoException " + cause);
incrementExceptionCounter(cause, "PassportStateServerHandler.Inbound");
} else {
super.exceptionCaught(ctx, cause);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{
passport(ctx).add(PassportState.SERVER_CH_EXCEPTION);
if (cause instanceof Errors.NativeIoException) {
LOG.debug("PassportStateServerHandler Outbound NativeIoException " + cause);
incrementExceptionCounter(cause, "PassportStateServerHandler.Outbound");
} else {
super.exceptionCaught(ctx, cause);
}
}
public ErrorType mapNettyToOutboundErrorType(final Throwable t) {
if (t instanceof ReadTimeoutException) {
return READ_TIMEOUT;
}
if (t instanceof OriginConcurrencyExceededException) {
return ORIGIN_CONCURRENCY_EXCEEDED;
}
if (t instanceof OriginConnectException) {
return ((OriginConnectException) t).getErrorType();
}
if (t instanceof OutboundException) {
return ((OutboundException) t).getOutboundErrorType();
}
if (t instanceof Errors.NativeIoException && Errors.ERRNO_ECONNRESET_NEGATIVE == ((Errors.NativeIoException) t).expectedErr()) {
// This is a "Connection reset by peer" which we see fairly often happening when Origin servers are overloaded.
LOG.warn("ERRNO_ECONNRESET_NEGATIVE mapped to RESET_CONNECTION", t);
return RESET_CONNECTION;
}
if (t instanceof ClosedChannelException) {
return RESET_CONNECTION;
}
final Throwable cause = t.getCause();
if (cause instanceof IllegalStateException && cause.getMessage().contains("server")) {
LOG.warn("IllegalStateException mapped to NO_AVAILABLE_SERVERS", cause);
return NO_AVAILABLE_SERVERS;
}
return OTHER;
}
private boolean isClientChannelClosed(Throwable cause) {
if (cause instanceof ClosedChannelException ||
cause instanceof Errors.NativeIoException) {
LOG.error("ZuulFilterChainHandler::isClientChannelClosed - IO Exception");
return true;
}
return false;
}