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