Make status code name as a getter to sync with internal impl (#439)

This commit is contained in:
Ji Li 2021-02-02 12:13:54 -08:00 committed by GitHub
parent afea2e71d8
commit eedc9acdc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 41 deletions

View File

@ -124,40 +124,34 @@ class StatusCode {
class GrpcError implements Exception {
final int code;
final String codeName;
final String? message;
final Object? rawResponse;
final List<GeneratedMessage>? details;
/// Custom error code.
GrpcError.custom(this.code, [this.message, this.details, this.rawResponse])
: codeName = _getStatusCodeValue(code);
GrpcError.custom(this.code, [this.message, this.details, this.rawResponse]);
/// The operation completed successfully.
GrpcError.ok([this.message, this.details, this.rawResponse])
: code = StatusCode.ok,
codeName = _getStatusCodeValue(StatusCode.ok);
: code = StatusCode.ok;
/// The operation was cancelled (typically by the caller).
GrpcError.cancelled([this.message, this.details, this.rawResponse])
: code = StatusCode.cancelled,
codeName = _getStatusCodeValue(StatusCode.cancelled);
: code = StatusCode.cancelled;
/// Unknown error. An example of where this error may be returned is if a
/// Status value received from another address space belongs to an error-space
/// that is not known in this address space. Also errors raised by APIs that
/// do not return enough error information may be converted to this error.
GrpcError.unknown([this.message, this.details, this.rawResponse])
: code = StatusCode.unknown,
codeName = _getStatusCodeValue(StatusCode.unknown);
: code = StatusCode.unknown;
/// Client specified an invalid argument. Note that this differs from
/// [failedPrecondition]. [invalidArgument] indicates arguments that are
/// problematic regardless of the state of the system (e.g., a malformed file
/// name).
GrpcError.invalidArgument([this.message, this.details, this.rawResponse])
: code = StatusCode.invalidArgument,
codeName = _getStatusCodeValue(StatusCode.invalidArgument);
: code = StatusCode.invalidArgument;
/// Deadline expired before operation could complete. For operations that
/// change the state of the system, this error may be returned even if the
@ -165,19 +159,16 @@ class GrpcError implements Exception {
/// from a server could have been delayed long enough for the deadline to
/// expire.
GrpcError.deadlineExceeded([this.message, this.details, this.rawResponse])
: code = StatusCode.deadlineExceeded,
codeName = _getStatusCodeValue(StatusCode.deadlineExceeded);
: code = StatusCode.deadlineExceeded;
/// Some requested entity (e.g., file or directory) was not found.
GrpcError.notFound([this.message, this.details, this.rawResponse])
: code = StatusCode.notFound,
codeName = _getStatusCodeValue(StatusCode.notFound);
: code = StatusCode.notFound;
/// Some entity that we attempted to create (e.g., file or directory) already
/// exists.
GrpcError.alreadyExists([this.message, this.details, this.rawResponse])
: code = StatusCode.alreadyExists,
codeName = _getStatusCodeValue(StatusCode.alreadyExists);
: code = StatusCode.alreadyExists;
/// The caller does not have permission to execute the specified operation.
/// [permissionDenied] must not be used for rejections caused by exhausting
@ -185,14 +176,12 @@ class GrpcError implements Exception {
/// [permissionDenied] must not be used if the caller cannot be identified
/// (use [unauthenticated] instead for those errors).
GrpcError.permissionDenied([this.message, this.details, this.rawResponse])
: code = StatusCode.permissionDenied,
codeName = _getStatusCodeValue(StatusCode.permissionDenied);
: code = StatusCode.permissionDenied;
/// Some resource has been exhausted, perhaps a per-user quota, or perhaps the
/// entire file system is out of space.
GrpcError.resourceExhausted([this.message, this.details, this.rawResponse])
: code = StatusCode.resourceExhausted,
codeName = _getStatusCodeValue(StatusCode.resourceExhausted);
: code = StatusCode.resourceExhausted;
/// Operation was rejected because the system is not in a state required for
/// the operation's execution. For example, directory to be deleted may be
@ -209,8 +198,7 @@ class GrpcError implements Exception {
/// returned since the client should not retry unless they have first
/// fixed up the directory by deleting files from it.
GrpcError.failedPrecondition([this.message, this.details, this.rawResponse])
: code = StatusCode.failedPrecondition,
codeName = _getStatusCodeValue(StatusCode.failedPrecondition);
: code = StatusCode.failedPrecondition;
/// The operation was aborted, typically due to a concurrency issue like
/// sequencer check failures, transaction aborts, etc.
@ -218,8 +206,7 @@ class GrpcError implements Exception {
/// See litmus test above for deciding between [failedPrecondition],
/// [aborted], and [unavailable].
GrpcError.aborted([this.message, this.details, this.rawResponse])
: code = StatusCode.aborted,
codeName = _getStatusCodeValue(StatusCode.aborted);
: code = StatusCode.aborted;
/// Operation was attempted past the valid range. E.g., seeking or reading
/// past end of file.
@ -235,20 +222,17 @@ class GrpcError implements Exception {
/// when it applies so that callers who are iterating through a space can
/// easily look for an [outOfRange] error to detect when they are done.
GrpcError.outOfRange([this.message, this.details, this.rawResponse])
: code = StatusCode.outOfRange,
codeName = _getStatusCodeValue(StatusCode.outOfRange);
: code = StatusCode.outOfRange;
/// Operation is not implemented or not supported/enabled in this service.
GrpcError.unimplemented([this.message, this.details, this.rawResponse])
: code = StatusCode.unimplemented,
codeName = _getStatusCodeValue(StatusCode.unimplemented);
: code = StatusCode.unimplemented;
/// Internal errors. Means some invariants expected by underlying system has
/// been broken. If you see one of these errors, something is very broken.
// TODO(sigurdm): This should probably not be an [Exception].
GrpcError.internal([this.message, this.details, this.rawResponse])
: code = StatusCode.internal,
codeName = _getStatusCodeValue(StatusCode.internal);
: code = StatusCode.internal;
/// The service is currently unavailable. This is a most likely a transient
/// condition and may be corrected by retrying with a backoff.
@ -256,19 +240,19 @@ class GrpcError implements Exception {
/// See litmus test above for deciding between [failedPrecondition],
/// [aborted], and [unavailable].
GrpcError.unavailable([this.message, this.details, this.rawResponse])
: code = StatusCode.unavailable,
codeName = _getStatusCodeValue(StatusCode.unavailable);
: code = StatusCode.unavailable;
/// Unrecoverable data loss or corruption.
GrpcError.dataLoss([this.message, this.details, this.rawResponse])
: code = StatusCode.dataLoss,
codeName = _getStatusCodeValue(StatusCode.dataLoss);
: code = StatusCode.dataLoss;
/// The request does not have valid authentication credentials for the
/// operation.
GrpcError.unauthenticated([this.message, this.details, this.rawResponse])
: code = StatusCode.unauthenticated,
codeName = _getStatusCodeValue(StatusCode.unauthenticated);
: code = StatusCode.unauthenticated;
/// Given a status code, return the name
String get codeName => (Code.valueOf(code) ?? Code.UNKNOWN).name;
@override
bool operator ==(other) {
@ -284,10 +268,6 @@ class GrpcError implements Exception {
'gRPC Error (code: $code, codeName: $codeName, message: $message, details: $details, rawResponse: $rawResponse)';
}
/// Given a status code, return the name
String _getStatusCodeValue(int code) =>
(Code.valueOf(code) ?? Code.UNKNOWN).name;
/// Parse error details `Any` object into the right kind of `GeneratedMessage`.
///
/// This list comes from `error_details.proto`. If any new error detail types are