Exceptions { public static RuntimeException from(int streamId, ByteBuf frame) { Objects.requireNonNull(frame, "frame must not be null"); int errorCode = ErrorFrameCodec.errorCode(frame); String message = ErrorFrameCodec.dataUtf8(frame); if (streamId == 0) { switch (errorCode) { case INVALID_SETUP: return new InvalidSetupException(message); case UNSUPPORTED_SETUP: return new UnsupportedSetupException(message); case REJECTED_SETUP: return new RejectedSetupException(message); case REJECTED_RESUME: return new RejectedResumeException(message); case CONNECTION_ERROR: return new ConnectionErrorException(message); case CONNECTION_CLOSE: return new ConnectionCloseException(message); default: return new IllegalArgumentException( String.format("Invalid Error frame in Stream ID 0: 0x%08X '%s'", errorCode, message)); } } else { switch (errorCode) { case APPLICATION_ERROR: return new ApplicationErrorException(message); case REJECTED: return new RejectedException(message); case CANCELED: return new CanceledException(message); case INVALID: return new InvalidException(message); default: if (errorCode >= MIN_USER_ALLOWED_ERROR_CODE || errorCode <= MAX_USER_ALLOWED_ERROR_CODE) { return new CustomRSocketException(errorCode, message); } return new IllegalArgumentException( String.format( "Invalid Error frame in Stream ID %d: 0x%08X '%s'", streamId, errorCode, message)); } } } private  Exceptions(); static RuntimeException from(int streamId, ByteBuf frame);  }
@DisplayName("from returns ApplicationErrorException") @Test void fromApplicationException() { ByteBuf byteBuf = createErrorFrame(1, APPLICATION_ERROR, "test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(ApplicationErrorException.class) .hasMessage("test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(IllegalArgumentException.class) .hasMessage( "Invalid Error frame in Stream ID 0: 0x%08X '%s'", APPLICATION_ERROR, "test-message"); }
@DisplayName("from returns CanceledException") @Test void fromCanceledException() { ByteBuf byteBuf = createErrorFrame(1, CANCELED, "test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(CanceledException.class) .hasMessage("test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Invalid Error frame in Stream ID 0: 0x%08X '%s'", CANCELED, "test-message"); }
@DisplayName("from returns ConnectionCloseException") @Test void fromConnectionCloseException() { ByteBuf byteBuf = createErrorFrame(0, CONNECTION_CLOSE, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(ConnectionCloseException.class) .hasMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(IllegalArgumentException.class) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", CONNECTION_CLOSE, "test-message"); }
@DisplayName("from returns ConnectionErrorException") @Test void fromConnectionErrorException() { ByteBuf byteBuf = createErrorFrame(0, CONNECTION_ERROR, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(ConnectionErrorException.class) .hasMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(IllegalArgumentException.class) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", CONNECTION_ERROR, "test-message"); }
@DisplayName("from returns IllegalArgumentException if error frame has illegal error code") @Test void fromIllegalErrorFrame() { ByteBuf byteBuf = createErrorFrame(0, 0x00000000, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .hasMessage("Invalid Error frame in Stream ID 0: 0x%08X '%s'", 0, "test-message") .isInstanceOf(IllegalArgumentException.class); assertThat(Exceptions.from(1, byteBuf)) .hasMessage("Invalid Error frame in Stream ID 1: 0x%08X '%s'", 0x00000000, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns InvalidException") @Test void fromInvalidException() { ByteBuf byteBuf = createErrorFrame(1, INVALID, "test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(InvalidException.class) .hasMessage("test-message"); assertThat(Exceptions.from(0, byteBuf)) .hasMessage("Invalid Error frame in Stream ID 0: 0x%08X '%s'", INVALID, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns InvalidSetupException") @Test void fromInvalidSetupException() { ByteBuf byteBuf = createErrorFrame(0, INVALID_SETUP, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(InvalidSetupException.class) .hasMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", INVALID_SETUP, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns RejectedException") @Test void fromRejectedException() { ByteBuf byteBuf = createErrorFrame(1, REJECTED, "test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(RejectedException.class) .withFailMessage("test-message"); assertThat(Exceptions.from(0, byteBuf)) .hasMessage("Invalid Error frame in Stream ID 0: 0x%08X '%s'", REJECTED, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns RejectedResumeException") @Test void fromRejectedResumeException() { ByteBuf byteBuf = createErrorFrame(0, REJECTED_RESUME, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(RejectedResumeException.class) .hasMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", REJECTED_RESUME, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns RejectedSetupException") @Test void fromRejectedSetupException() { ByteBuf byteBuf = createErrorFrame(0, REJECTED_SETUP, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(RejectedSetupException.class) .withFailMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", REJECTED_SETUP, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns UnsupportedSetupException") @Test void fromUnsupportedSetupException() { ByteBuf byteBuf = createErrorFrame(0, UNSUPPORTED_SETUP, "test-message"); assertThat(Exceptions.from(0, byteBuf)) .isInstanceOf(UnsupportedSetupException.class) .hasMessage("test-message"); assertThat(Exceptions.from(1, byteBuf)) .hasMessage( "Invalid Error frame in Stream ID 1: 0x%08X '%s'", UNSUPPORTED_SETUP, "test-message") .isInstanceOf(IllegalArgumentException.class); }
@DisplayName("from returns CustomRSocketException") @Test void fromCustomRSocketException() { for (int i = 0; i < 1000; i++) { int randomCode = ThreadLocalRandom.current().nextBoolean() ? ThreadLocalRandom.current() .nextInt(Integer.MIN_VALUE, ErrorFrameCodec.MAX_USER_ALLOWED_ERROR_CODE) : ThreadLocalRandom.current() .nextInt(ErrorFrameCodec.MIN_USER_ALLOWED_ERROR_CODE, Integer.MAX_VALUE); ByteBuf byteBuf = createErrorFrame(0, randomCode, "test-message"); assertThat(Exceptions.from(1, byteBuf)) .isInstanceOf(CustomRSocketException.class) .hasMessage("test-message"); assertThat(Exceptions.from(0, byteBuf)) .hasMessage("Invalid Error frame in Stream ID 0: 0x%08X '%s'", randomCode, "test-message") .isInstanceOf(IllegalArgumentException.class); byteBuf.release(); } }
@DisplayName("from throws NullPointerException with null frame") @Test void fromWithNullFrame() { assertThatNullPointerException() .isThrownBy(() -> Exceptions.from(0, null)) .withMessage("frame must not be null"); }