@Override public void onNext(SaslMessage saslMessage) { try { SaslMessage response = mSaslHandshakeClientHandler.handleSaslMessage(saslMessage); if (response == null) { mRequestObserver.onCompleted(); } else { mRequestObserver.onNext(response); } } catch (SaslException e) { mAuthenticated.setException(e); mRequestObserver .onError(Status.fromCode(Status.Code.UNAUTHENTICATED).withCause(e).asException()); } }
@Override protected void doStop() { messageDispatcher.stop(); lock.lock(); try { clientStream.closeSendWithError(Status.CANCELLED.asException()); } finally { lock.unlock(); notifyStopped(); } }
/** * Returns a {@link RpcResponse} corresponding to the given {@link Status} generated by the server. */ public static RpcResponse rpcResponse(Status status, @Nullable Object message) { if (status.isOk()) { return RpcResponse.of(message); } else { return RpcResponse.ofFailure(status.asException()); } }
@Override public void errorNoMessage(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) { responseObserver.onError(Status.ABORTED.asException()); }
responseObserver.onError(Status.NOT_FOUND.asException()); return;
@Override public void errorWithMessage(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) { responseObserver.onError(Status.ABORTED.withDescription("aborted call").asException()); }
@Override public void messageRead(ByteBufOrStream message) { try { final O msg = marshaller.deserializeResponse(message); if (firstResponse == null) { firstResponse = msg; } if (unsafeWrapResponseBuffers && message.buf() != null) { GrpcUnsafeBufferUtil.storeBuffer(message.buf(), msg, ctx); } try (SafeCloseable ignored = ctx.push()) { listener.onMessage(msg); } } catch (Throwable t) { req.close(Status.fromThrowable(t).asException()); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } }
LOG.warn("Could not serialize the cause: {}. Failed with: {}", cause, exc); return Status.fromCode(code).asException(trailers);
final StatusException statusException = newStatus.asException(); final Throwable cause = statusException.getCause(); if (cause != null) {
.withDescription(t.getMessage()) .withCause(t) .asException(metadata);
public void deleteShelf(long shelfId) throws StatusException { synchronized (lock) { if (shelves.remove(shelfId) == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } } }
public Shelf getShelf(long shelfId) throws StatusException { synchronized (lock) { @Nullable Shelf shelf = shelfInfoToShelf.apply(shelves.get(shelfId)); if (shelf == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } return shelf; } }
public Iterable<Book> listBooks(long shelfId) throws StatusException { synchronized (lock) { @Nullable ShelfInfo shelfInfo = shelves.get(shelfId); if (shelfInfo == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } return ImmutableList.copyOf(shelfInfo.books.values()); } }
@Test public void testErrorPropagation() throws Exception { Publisher publisher = getTestPublisherBuilder() .setExecutorProvider(SINGLE_THREAD_EXECUTOR) .setBatchingSettings( Publisher.Builder.DEFAULT_BATCHING_SETTINGS .toBuilder() .setElementCountThreshold(1L) .setDelayThreshold(Duration.ofSeconds(5)) .build()) .build(); testPublisherServiceImpl.addPublishError(Status.DATA_LOSS.asException()); try { sendTestMessage(publisher, "A").get(); fail("should throw exception"); } catch (ExecutionException e) { assertThat(e.getCause()).isInstanceOf(DataLossException.class); } }
public void deleteBook(long shelfId, long bookId) throws StatusException { synchronized (lock) { @Nullable ShelfInfo shelfInfo = shelves.get(shelfId); if (shelfInfo == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } if (shelfInfo.books.remove(bookId) == null) { throw Status.NOT_FOUND .withDescription("Unknown book ID") .asException(); } } } }
public Book getBook(long shelfId, long bookId) throws StatusException { synchronized (lock) { @Nullable ShelfInfo shelfInfo = shelves.get(shelfId); if (shelfInfo == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } @Nullable Book book = shelfInfo.books.get(bookId); if (book == null) { throw Status.NOT_FOUND .withDescription("Unknown book ID") .asException(); } return book; } }
public Book createBook(long shelfId, Book book) throws StatusException { synchronized (lock) { @Nullable ShelfInfo shelfInfo = shelves.get(shelfId); if (shelfInfo == null) { throw Status.NOT_FOUND .withDescription("Unknown shelf ID") .asException(); } shelfInfo.lastBookId++; book = book.toBuilder() .setId(shelfInfo.lastBookId) .build(); shelfInfo.books.put(shelfInfo.lastBookId, book); } return book; }
private static Throwable prepareError(Throwable throwable) { if (throwable instanceof StatusException || throwable instanceof StatusRuntimeException) { return throwable; } else { return Status.fromThrowable(throwable).asException(); } } }
/** * Returns a {@link RpcResponse} corresponding to the given {@link Status} generated by the server. */ public static RpcResponse rpcResponse(Status status, @Nullable Object message) { if (status.isOk()) { return RpcResponse.of(message); } else { return RpcResponse.ofFailure(status.asException()); } }
@Override public void onError(Throwable t) { if (!Status.fromThrowable(t).getCode().equals(Status.CANCELLED.getCode())) { LOGGER.error("[{}] stream closed with error", streamId, t); } try { callbacks.forEach(cb -> cb.onStreamCloseWithError(streamId, defaultTypeUrl, t)); responseObserver.onError(Status.fromThrowable(t).asException()); } finally { cancel(); } }