1 /* 2 * Copyright 2015 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import javax.annotation.Nullable; 20 21 /** 22 * {@link Status} in Exception form, for propagating Status information via exceptions. This is 23 * semantically equivalent to {@link StatusRuntimeException}, except for usage in APIs that promote 24 * checked exceptions. gRPC's stubs favor {@code StatusRuntimeException}. 25 */ 26 public class StatusException extends Exception { 27 private static final long serialVersionUID = -660954903976144640L; 28 private final Status status; 29 private final Metadata trailers; 30 private final boolean fillInStackTrace; 31 32 /** 33 * Constructs an exception with both a status. See also {@link Status#asException()}. 34 * 35 * @since 1.0.0 36 */ StatusException(Status status)37 public StatusException(Status status) { 38 this(status, null); 39 } 40 41 /** 42 * Constructs an exception with both a status and trailers. See also 43 * {@link Status#asException(Metadata)}. 44 * 45 * @since 1.0.0 46 */ StatusException(Status status, @Nullable Metadata trailers)47 public StatusException(Status status, @Nullable Metadata trailers) { 48 this(status, trailers, /*fillInStackTrace=*/ true); 49 } 50 StatusException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace)51 StatusException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) { 52 super(Status.formatThrowableMessage(status), status.getCause()); 53 this.status = status; 54 this.trailers = trailers; 55 this.fillInStackTrace = fillInStackTrace; 56 fillInStackTrace(); 57 } 58 59 @Override fillInStackTrace()60 public synchronized Throwable fillInStackTrace() { 61 // Let's observe final variables in two states! This works because Throwable will invoke this 62 // method before fillInStackTrace is set, thus doing nothing. After the constructor has set 63 // fillInStackTrace, this method will properly fill it in. Additionally, sub classes may call 64 // this normally, because fillInStackTrace will either be set, or this method will be 65 // overriden. 66 return fillInStackTrace ? super.fillInStackTrace() : this; 67 } 68 69 /** 70 * Returns the status code as a {@link Status} object. 71 * 72 * @since 1.0.0 73 */ getStatus()74 public final Status getStatus() { 75 return status; 76 } 77 78 /** 79 * Returns the received trailers. 80 * 81 * @since 1.0.0 82 */ getTrailers()83 public final Metadata getTrailers() { 84 return trailers; 85 } 86 } 87