@Override public void onError(Throwable t) { Metadata metadata = Status.trailersFromThrowable(t); if (metadata == null) { metadata = new Metadata(); } if (t instanceof MotanBizException) { call.close(Status.INTERNAL.withDescription(t.getMessage()).withCause(t), metadata); } else { call.close(Status.UNAVAILABLE.withDescription(t.getMessage()).withCause(t), metadata); } }
private static Status addCause(Status status, String serializedThrowableProto) { final byte[] decoded; try { decoded = Base64.getDecoder().decode(serializedThrowableProto); } catch (IllegalArgumentException e) { logger.warn("Invalid Base64 in status cause proto, ignoring.", e); return status; } final ThrowableProto grpcThrowableProto; try { grpcThrowableProto = ThrowableProto.parseFrom(decoded); } catch (InvalidProtocolBufferException e) { logger.warn("Invalid serialized status cause proto, ignoring.", e); return status; } return status.withCause(new StatusCauseException(grpcThrowableProto)); } }
@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()); } }
private void doCancel(@Nullable String message, @Nullable Throwable cause) { if (message == null && cause == null) { cause = new CancellationException("Cancelled without a message or cause"); logger.warn("Cancelling without a message or cause is suboptimal", cause); } if (cancelCalled) { return; } cancelCalled = true; Status status = Status.CANCELLED; if (message != null) { status = status.withDescription(message); } if (cause != null) { status = status.withCause(cause); } close(status); req.abort(); }
@Override public Object[] parse(InputStream stream) { try { BinaryDecoder in = DECODER_FACTORY.binaryDecoder(stream, null); Schema reqSchema = message.getRequest(); GenericRecord request = (GenericRecord) new SpecificDatumReader<>(reqSchema).read(null, in); Object[] args = new Object[reqSchema.getFields().size()]; int i = 0; for (Schema.Field field : reqSchema.getFields()) { args[i++] = request.get(field.name()); } return args; } catch (IOException e) { throw Status.INTERNAL.withCause(e). withDescription("Error deserializing avro request arguments").asRuntimeException(); } finally { AvroGrpcUtils.skipAndCloseQuietly(stream); } }
@Override public void unaryCall(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) { IllegalStateException e1 = new IllegalStateException("Exception 1"); IllegalArgumentException e2 = new IllegalArgumentException(); AssertionError e3 = new AssertionError("Exception 3"); Exceptions.clearTrace(e3); RuntimeException e4 = new RuntimeException("Exception 4"); e1.initCause(e2); e2.initCause(e3); e3.initCause(e4); Status status = Status.ABORTED.withCause(e1); responseObserver.onError(status.asRuntimeException()); } }
/** * Writes out a payload message. * * @param message the message to be written out. Ownership is taken by {@link ArmeriaMessageFramer}. * * @return a {@link ByteBufHttpData} with the framed payload. Ownership is passed to caller. */ public ByteBufHttpData writePayload(ByteBuf message) { verifyNotClosed(); final boolean compressed = messageCompression && compressor != Codec.Identity.NONE; final int messageLength = message.readableBytes(); try { final ByteBuf buf; if (messageLength != 0 && compressed) { buf = writeCompressed(message); } else { buf = writeUncompressed(message); } return new ByteBufHttpData(buf, false); } catch (IOException | RuntimeException e) { // IOException will not be thrown, since sink#deliverFrame doesn't throw. throw Status.INTERNAL .withDescription("Failed to frame message") .withCause(e) .asRuntimeException(); } }
} catch (InvalidProtocolBufferException e) { throw Status.INTERNAL.withDescription("Invalid protobuf byte sequence") .withCause(e).asRuntimeException();
@Override public Object parse(InputStream stream) { try { if (message.isOneWay()) return null; BinaryDecoder in = DECODER_FACTORY.binaryDecoder(stream, null); if (!in.readBoolean()) { Object response = new SpecificDatumReader(message.getResponse()).read(null, in); return response; } else { Object value = new SpecificDatumReader(message.getErrors()).read(null, in); if (value instanceof Exception) { return value; } return new AvroRuntimeException(value.toString()); } } catch (IOException e) { throw Status.INTERNAL.withCause(e). withDescription("Error deserializing avro response").asRuntimeException(); } finally { AvroGrpcUtils.skipAndCloseQuietly(stream); } }
.withCause(t) .asException(metadata);
/** * Close the call with {@link Status#PERMISSION_DENIED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallAccessDenied(final ServerCall<?, ?> call, final AccessDeniedException aex) { call.close(Status.PERMISSION_DENIED.withCause(aex).withDescription(ACCESS_DENIED_DESCRIPTION), new Metadata()); }
/** * Close the call with {@link Status#UNAUTHENTICATED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallUnauthenticated(final ServerCall<?, ?> call, final AuthenticationException aex) { call.close(Status.UNAUTHENTICATED.withCause(aex).withDescription(UNAUTHENTICATED_DESCRIPTION), new Metadata()); }
obs.onError(Status.UNIMPLEMENTED .withDescription("compression not supported.") .withCause(e) .asRuntimeException()); return;
@Override public InputStream stream(T value) { try { return new ByteArrayInputStream(printer.print(value).getBytes(charset)); } catch (InvalidProtocolBufferException e) { throw Status.INTERNAL .withCause(e) .withDescription("Unable to print json proto") .asRuntimeException(); } }
private StatusRuntimeException getBadRequestException(Exception e) { return new StatusRuntimeException( Status.fromCode(Status.Code.OUT_OF_RANGE).withDescription(e.getMessage()).withCause(e)); } }
/** * Close the call with {@link Status#UNAUTHENTICATED}. * * @param call The call to close. * @param aex The exception that was the cause. */ protected void closeCallUnauthenticated(final ServerCall<?, ?> call, final AuthenticationException aex) { call.close(Status.UNAUTHENTICATED.withCause(aex).withDescription(UNAUTHENTICATED_DESCRIPTION), new Metadata()); }
public ResultT getBlockingResult() { try { return getAsyncResult().get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); cancel(); throw Status.CANCELLED.withCause(e).asRuntimeException(); } catch (ExecutionException e) { cancel(); throw Status.fromThrowable(e).asRuntimeException(); } }
@Override public void customException(EchoRequest request, StreamObserver<EchoResponse> responseObserver) { try { throw new CustomException("Custom exception!"); } catch (Exception e) { responseObserver.onError(Status.INTERNAL .withDescription(e.getMessage()) .augmentDescription("customException()") .withCause(e) // This can be attached to the Status locally, but NOT transmitted to the client! .asRuntimeException()); } }