Skip to content

Adjust parent types for exceptions #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018-2019 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018-2019, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,13 @@

import io.servicetalk.transport.api.RetryableException;

import java.net.SocketException;

/**
* Thrown when a newly created connection was rejected.
*/
public final class ConnectionRejectedException extends RuntimeException implements RetryableException {
private static final long serialVersionUID = -2573006397795678870L;
public class ConnectionRejectedException extends SocketException implements RetryableException {
Copy link
Member Author

@idelpivnitskiy idelpivnitskiy Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debated between ConnectException and SocketException. Based on their javadoc, decided to go with SocketException because technically the connection was already established, but there was a problem "accessing" it due to a selector or a host becoming unavailable.

private static final long serialVersionUID = 8033460879797361579L;

/**
* Creates a new instance.
Expand All @@ -39,6 +41,7 @@ public ConnectionRejectedException(final String message) {
* @param cause the cause.
*/
public ConnectionRejectedException(final String message, final Throwable cause) {
super(message, cause);
super(message);
initCause(cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,7 +38,10 @@ public interface LoadBalancer<C extends LoadBalancedConnection> extends Listenab
* @param selector A {@link Function} that evaluates a connection for selection.
* This selector should return {@code null} if the connection <strong>MUST</strong> not be selected.
* This selector is guaranteed to be called for any connection that is returned from this method.
* @return a {@link Single} that completes with the most appropriate connection to use.
* @return a {@link Single} that completes with the most appropriate connection to use. A
* {@link Single#failed(Throwable) failed Single} with {@link NoAvailableHostException} can be returned if no
* connection can be selected at this time or with {@link ConnectionRejectedException} if a newly created connection
* was rejected by the {@code selector} or this load balancer.
*/
Single<C> selectConnection(Predicate<C> selector);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,11 +17,13 @@

import io.servicetalk.transport.api.RetryableException;

import java.io.IOException;

/**
* Thrown when no host is available but at least one is required.
*/
public class NoAvailableHostException extends RuntimeException implements RetryableException {
private static final long serialVersionUID = 5340791072245425967L;
public class NoAvailableHostException extends IOException implements RetryableException {
private static final long serialVersionUID = -7717046537430445936L;

/**
* Creates a new instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,8 @@
/**
* Indicates that a duplicate value was added while constructing a {@link PartitionAttributes}.
*/
public final class DuplicateAttributeException extends RuntimeException {
private static final long serialVersionUID = 3128709895329437549L;
public final class DuplicateAttributeException extends IllegalStateException {
private static final long serialVersionUID = 8374128121212690894L;

private final Key key;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,8 +40,8 @@ public final class RepeatStrategies {
/**
* An {@link Exception} instance used to indicate termination of repeats.
*/
public static final class TerminateRepeatException extends Exception {
private static final long serialVersionUID = -1725458427890873886L;
public static final class TerminateRepeatException extends RuntimeException {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decision to throw this exception happens at runtime

private static final long serialVersionUID = -1725458427890873887L;

// It is fine to reuse this instance and let it escape to the user as enableSuppression is set to false.
static final TerminateRepeatException INSTANCE = new TerminateRepeatException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,6 +55,7 @@

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -468,8 +469,7 @@ private void handleSubscribe0(
if (subscription != null) {
deliverErrorFromSource(subscriber, new DuplicateSubscribeException(subscription, subscriber));
} else if (closed) {
deliverErrorFromSource(subscriber, new ClosedServiceDiscovererException(DefaultDnsClient.this +
" has been closed!"));
deliverErrorFromSource(subscriber, new ClosedDnsServiceDiscovererException());
} else {
subscription = newSubscription(subscriber);
try {
Expand Down Expand Up @@ -564,8 +564,7 @@ private void doQuery0() {

if (closed) {
// best effort check to cleanup state after close.
handleTerminalError0(new ClosedServiceDiscovererException(DefaultDnsClient.this +
" has been closed!"));
handleTerminalError0(new ClosedDnsServiceDiscovererException());
} else {
final DnsResolutionObserver resolutionObserver = newResolutionObserver();
LOGGER.trace("DnsClient {}, querying DNS for {}", DefaultDnsClient.this, AbstractDnsPublisher.this);
Expand Down Expand Up @@ -897,13 +896,9 @@ public io.netty.resolver.dns.DnsServerAddressStream duplicate() {
}
}

private static final class ClosedServiceDiscovererException extends RuntimeException
private static final class ClosedDnsServiceDiscovererException extends ClosedChannelException
implements RejectedSubscribeError {
private static final long serialVersionUID = 1411660766942024081L;

ClosedServiceDiscovererException(final String message) {
super(message);
}
private static final long serialVersionUID = -8092675984257002148L;
}

private static final class SrvAddressRemovedException extends RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
*/
package io.servicetalk.grpc.api;

import io.servicetalk.serializer.api.SerializationException;

/**
* Exception thrown when a message was encoded with an unsupported encoder.
*
* @deprecated This exception type was thrown only by {@code ProtoBufSerializationProviderBuilder} that was deprecated
* and will be removed in future releases. Use {@link SerializationException} instead that can be thrown by a new
* implementation.
*/
@Deprecated
public final class MessageEncodingException extends RuntimeException {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProtoBufSerializationProviderBuilder was deprecated only in the main branch, we don't need to cherry-pick this into 0.41

private static final long serialVersionUID = 4146293629657567572L;

private final String encoding;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package io.servicetalk.http.api;

import io.servicetalk.serializer.api.SerializationException;

import javax.annotation.Nullable;

final class UnsupportedHttpChunkException extends SerializationException {
final class UnsupportedHttpChunkException extends IllegalArgumentException {
Copy link
Member Author

@idelpivnitskiy idelpivnitskiy Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from IllegalArgumentException to SerializationException was made only in the main branch in #1673. This is not related to serialization and more about what payload body object users passed to an HTTP message. Reverting back to IllegalArgumentException.
cc @Scottmitch

private static final long serialVersionUID = -4336685587984151152L;

UnsupportedHttpChunkException(@Nullable Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,6 @@ private static final class HttpResponseStatusException extends RuntimeException
this.status = status;
}

HttpResponseStatusException(HttpResponseStatus status, @Nullable final String message) {
super(message);
this.status = status;
}

@Override
public String getMessage() {
return status + (null != super.getMessage() ? " : " + super.getMessage() : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,18 @@ private Single<C> selectConnection0(Predicate<C> selector) {
if (!selector.test(newCnx)) {
// Failure in selection could be temporary, hence add it to the queue and be consistent
// with the fact that select failure does not close a connection.
return newCnx.closeAsync().concat(failed(new ConnectionRejectedException(
return newCnx.closeAsync().concat(failed(StacklessConnectionRejectedException.newInstance(
"Newly created connection " + newCnx + " for " + targetResource
+ " was rejected by the selection filter.")));
+ " was rejected by the selection filter.",
RoundRobinLoadBalancer.class, "selectConnection0(...)")));
}
if (host.addConnection(newCnx)) {
return succeeded(newCnx);
}
return newCnx.closeAsync().concat(this.usedHosts == CLOSED_LIST ? failedLBClosed(targetResource) :
failed(new ConnectionRejectedException(
failed(StacklessConnectionRejectedException.newInstance(
"Failed to add newly created connection " + newCnx + " for " + targetResource
+ " for " + host)));
+ " for " + host, RoundRobinLoadBalancer.class, "selectConnection0(...)")));
});
}

Expand Down Expand Up @@ -937,4 +938,21 @@ public static StacklessNoAvailableHostException newInstance(String message, Clas
return ThrowableUtils.unknownStackTrace(new StacklessNoAvailableHostException(message), clazz, method);
}
}

private static final class StacklessConnectionRejectedException extends ConnectionRejectedException {
private static final long serialVersionUID = -4940708893680455819L;

private StacklessConnectionRejectedException(final String message) {
super(message);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

public static StacklessConnectionRejectedException newInstance(String message, Class<?> clazz, String method) {
return ThrowableUtils.unknownStackTrace(new StacklessConnectionRejectedException(message), clazz, method);
}
}
}