]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.arrow.flight; | |
19 | ||
20 | import java.util.Objects; | |
21 | ||
22 | import org.apache.arrow.flight.FlightProducer.ServerStreamListener; | |
23 | import org.apache.arrow.flight.FlightProducer.StreamListener; | |
24 | ||
25 | /** | |
26 | * The result of a Flight RPC, consisting of a status code with an optional description and/or exception that led | |
27 | * to the status. | |
28 | * | |
29 | * <p>If raised or sent through {@link StreamListener#onError(Throwable)} or | |
30 | * {@link ServerStreamListener#error(Throwable)}, the client call will raise the same error (a | |
31 | * {@link FlightRuntimeException} with the same {@link FlightStatusCode} and description). The exception within, if | |
32 | * present, will not be sent to the client. | |
33 | */ | |
34 | public class CallStatus { | |
35 | ||
36 | private final FlightStatusCode code; | |
37 | private final Throwable cause; | |
38 | private final String description; | |
39 | private final ErrorFlightMetadata metadata; | |
40 | ||
41 | public static final CallStatus UNKNOWN = FlightStatusCode.UNKNOWN.toStatus(); | |
42 | public static final CallStatus INTERNAL = FlightStatusCode.INTERNAL.toStatus(); | |
43 | public static final CallStatus INVALID_ARGUMENT = FlightStatusCode.INVALID_ARGUMENT.toStatus(); | |
44 | public static final CallStatus TIMED_OUT = FlightStatusCode.TIMED_OUT.toStatus(); | |
45 | public static final CallStatus NOT_FOUND = FlightStatusCode.NOT_FOUND.toStatus(); | |
46 | public static final CallStatus ALREADY_EXISTS = FlightStatusCode.ALREADY_EXISTS.toStatus(); | |
47 | public static final CallStatus CANCELLED = FlightStatusCode.CANCELLED.toStatus(); | |
48 | public static final CallStatus UNAUTHENTICATED = FlightStatusCode.UNAUTHENTICATED.toStatus(); | |
49 | public static final CallStatus UNAUTHORIZED = FlightStatusCode.UNAUTHORIZED.toStatus(); | |
50 | public static final CallStatus UNIMPLEMENTED = FlightStatusCode.UNIMPLEMENTED.toStatus(); | |
51 | public static final CallStatus UNAVAILABLE = FlightStatusCode.UNAVAILABLE.toStatus(); | |
52 | ||
53 | /** | |
54 | * Create a new status. | |
55 | * | |
56 | * @param code The status code. | |
57 | * @param cause An exception that resulted in this status (or null). | |
58 | * @param description A description of the status (or null). | |
59 | */ | |
60 | public CallStatus(FlightStatusCode code, Throwable cause, String description, ErrorFlightMetadata metadata) { | |
61 | this.code = Objects.requireNonNull(code); | |
62 | this.cause = cause; | |
63 | this.description = description == null ? "" : description; | |
64 | this.metadata = metadata == null ? new ErrorFlightMetadata() : metadata; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Create a new status with no cause or description. | |
69 | * | |
70 | * @param code The status code. | |
71 | */ | |
72 | public CallStatus(FlightStatusCode code) { | |
73 | this(code, /* no cause */ null, /* no description */ null, /* no metadata */ null); | |
74 | } | |
75 | ||
76 | /** | |
77 | * The status code describing the result of the RPC. | |
78 | */ | |
79 | public FlightStatusCode code() { | |
80 | return code; | |
81 | } | |
82 | ||
83 | /** | |
84 | * The exception that led to this result. May be null. | |
85 | */ | |
86 | public Throwable cause() { | |
87 | return cause; | |
88 | } | |
89 | ||
90 | /** | |
91 | * A description of the result. | |
92 | */ | |
93 | public String description() { | |
94 | return description; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Metadata associated with the exception. | |
99 | * | |
100 | * May be null. | |
101 | */ | |
102 | public ErrorFlightMetadata metadata() { | |
103 | return metadata; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Return a copy of this status with an error message. | |
108 | */ | |
109 | public CallStatus withDescription(String message) { | |
110 | return new CallStatus(code, cause, message, metadata); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Return a copy of this status with the given exception as the cause. This will not be sent over the wire. | |
115 | */ | |
116 | public CallStatus withCause(Throwable t) { | |
117 | return new CallStatus(code, t, description, metadata); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Return a copy of this status with associated exception metadata. | |
122 | */ | |
123 | public CallStatus withMetadata(ErrorFlightMetadata metadata) { | |
124 | return new CallStatus(code, cause, description, metadata); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Convert the status to an equivalent exception. | |
129 | */ | |
130 | public FlightRuntimeException toRuntimeException() { | |
131 | return new FlightRuntimeException(this); | |
132 | } | |
133 | ||
134 | @Override | |
135 | public String toString() { | |
136 | return "CallStatus{" + | |
137 | "code=" + code + | |
138 | ", cause=" + cause + | |
139 | ", description='" + description + | |
140 | "', metadata='" + metadata + '\'' + | |
141 | '}'; | |
142 | } | |
143 | } |