@Test @DisplayName("throw wrapped InvalidProtocolBufferException when Metadata bytes are invalid") void throwOnInvalidBytes() { Metadata metadata = new Metadata(); metadata.put(MetadataConverter.KEY, new byte[]{(byte) 1}); IllegalStateException e = assertThrows(IllegalStateException.class, () -> MetadataConverter.toError(metadata), "InvalidProtocolBufferException was not thrown."); assertTrue(e.getCause() instanceof InvalidProtocolBufferException); }
public static Optional<Error> toError(Metadata metadata) { checkNotNull(metadata); byte[] bytes = metadata.get(KEY); if (bytes == null) { return Optional.empty(); } try { Error error = Error.parseFrom(bytes); return Optional.of(error); } catch (InvalidProtocolBufferException e) { throw Exceptions.illegalStateWithCauseOf(e); } }
MetadataConverter { public static Optional<Error> toError(Metadata metadata) { checkNotNull(metadata); byte[] bytes = metadata.get(KEY); if (bytes == null) { return Optional.empty(); } try { Error error = Error.parseFrom(bytes); return Optional.of(error); } catch (InvalidProtocolBufferException e) { throw Exceptions.illegalStateWithCauseOf(e); } } }
MetadataConverter { public static Optional<Error> toError(Metadata metadata) { checkNotNull(metadata); byte[] bytes = metadata.get(KEY); if (bytes == null) { return Optional.empty(); } try { Error error = Error.parseFrom(bytes); return Optional.of(error); } catch (InvalidProtocolBufferException e) { throw Exceptions.illegalStateWithCauseOf(e); } } private  MetadataConverter(); }
MetadataConverter { public static Optional<Error> toError(Metadata metadata) { checkNotNull(metadata); byte[] bytes = metadata.get(KEY); if (bytes == null) { return Optional.empty(); } try { Error error = Error.parseFrom(bytes); return Optional.of(error); } catch (InvalidProtocolBufferException e) { throw Exceptions.illegalStateWithCauseOf(e); } } private  MetadataConverter(); static Metadata toMetadata(Error error); static Optional<Error> toError(Metadata metadata); }
MetadataConverter { public static Optional<Error> toError(Metadata metadata) { checkNotNull(metadata); byte[] bytes = metadata.get(KEY); if (bytes == null) { return Optional.empty(); } try { Error error = Error.parseFrom(bytes); return Optional.of(error); } catch (InvalidProtocolBufferException e) { throw Exceptions.illegalStateWithCauseOf(e); } } private  MetadataConverter(); static Metadata toMetadata(Error error); static Optional<Error> toError(Metadata metadata);  }