Skip to content

Commit 52a670f

Browse files
steveguryrobertroeser
authored andcommitted
Properly propagate exceptions type from server client. (#150)
***Problem*** Errors generated from the `Responder` are serialized according to the spec https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md#error-codes but the type is lost when deserializing by the `Requester`. ***Solution*** Instead of generating a RuntimeException containing the string of the error, generate the right Exception type based on the error code. This allow user code, or filter to make smart decision based on the Exception type (Retryable, ...)
1 parent 085b6e8 commit 52a670f

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

reactivesocket-core/src/main/java/io/reactivesocket/exceptions/TransportException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ public TransportException(Throwable t) {
2424
public synchronized Throwable fillInStackTrace() {
2525
return this;
2626
}
27-
2827
}

reactivesocket-core/src/main/java/io/reactivesocket/internal/Requester.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package io.reactivesocket.internal;
1717

18-
import java.io.IOException;
1918
import java.nio.ByteBuffer;
2019
import java.nio.charset.StandardCharsets;
2120
import java.util.Collection;
@@ -837,9 +836,8 @@ public void onNext(Frame frame) {
837836
cancel();
838837
} else if (type == FrameType.ERROR) {
839838
terminated.set(true);
840-
final ByteBuffer byteBuffer = frame.getData();
841-
String errorMessage = getByteBufferAsString(byteBuffer);
842-
onError(new RuntimeException(errorMessage));
839+
Throwable throwable = Exceptions.from(frame);
840+
onError(throwable);
843841
cancel();
844842
} else {
845843
onError(new RuntimeException("Unexpected FrameType: " + frame.getType()));

reactivesocket-core/src/test/java/io/reactivesocket/LeaseTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.reactivesocket;
1717

18+
import io.reactivesocket.exceptions.RejectedException;
1819
import io.reactivesocket.internal.Publishers;
1920
import io.reactivesocket.internal.Responder;
2021
import org.junit.After;
@@ -182,7 +183,7 @@ public void testWriteWithoutLease() throws InterruptedException {
182183
TestSubscriber<Payload> ts2 = new TestSubscriber<>();
183184
response2.subscribe(ts2);
184185
ts2.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
185-
ts2.assertError(RuntimeException.class);
186+
ts2.assertError(RejectedException.class);
186187
}
187188

188189
@Test(timeout=2000)

reactivesocket-core/src/test/java/io/reactivesocket/internal/RequesterTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.reactivesocket.LatchedCompletable;
2222
import io.reactivesocket.Payload;
2323
import io.reactivesocket.TestConnection;
24+
import io.reactivesocket.exceptions.InvalidRequestException;
2425
import io.reactivesocket.util.PayloadImpl;
2526
import io.reactivex.Observable;
2627
import io.reactivex.subjects.ReplaySubject;
@@ -165,7 +166,7 @@ public void testRequestResponseError() throws InterruptedException {
165166

166167
conn.toInput.send(Frame.Error.from(2, new RuntimeException("Failed")));
167168
ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
168-
ts.assertError(Exception.class);
169+
ts.assertError(InvalidRequestException.class);
169170
assertEquals("Failed", ts.errors().get(0).getMessage());
170171
}
171172

@@ -313,7 +314,7 @@ public void testRequestStreamError() throws InterruptedException {
313314
conn.toInput.send(utf8EncodedErrorFrame(2, "Failure"));
314315

315316
ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
316-
ts.assertError(Exception.class);
317+
ts.assertError(InvalidRequestException.class);
317318
ts.assertValue(utf8EncodedPayload("hello", null));
318319
assertEquals("Failure", ts.errors().get(0).getMessage());
319320
}

0 commit comments

Comments
 (0)