feat(types): Add gRPC Richer Error Model support (BadRequest) (#1068)
* types: add tonic as dependency, add error_details.proto * types: add BadRequest support from flemosr/tonic-richer-error * types: adjust code following suggestions Adjustments following suggestions by @LucioFranco in https://github.com/hyperium/tonic/pull/1068. Adjust style, remove unecessary prints, avoid glob imports, apply `non_exhaustive` to `ErrorDetails` and `ErrorDetail`, avoid pub fields in `ErrorDetails`, adjust `WithErrorDetails::with_error_details_vec` args, add `gen_details_bytes`. * types: add generated protobuf code As suggested by @LucioFranco in https://github.com/hyperium/tonic/pull/1068#discussion_r956117520. This avoids the need for consumers to have `protoc` in their path. Implemented following changes in https://github.com/hyperium/tonic/pull/1065. * types: add custom metadata support This allows consumers to provide custom metadata when creating a `Status` with error details. * types: adjust code following suggestions Adjustments following suggestions by @LucioFranco in https://github.com/hyperium/tonic/pull/1068. Move `error_details_vec` mod into `error_details` mod, adjust doc comments, rename `WithErrorDetails` trait to `StatusExt`.
This commit is contained in:
@@ -17,6 +17,7 @@ version = "0.6.0"
|
||||
[dependencies]
|
||||
prost = "0.11"
|
||||
prost-types = "0.11"
|
||||
tonic = {version = "0.8", path = "../tonic"}
|
||||
|
||||
[build-dependencies]
|
||||
prost-build = "0.11"
|
||||
[dev-dependencies]
|
||||
tonic-build = {version = "0.8", path = "../tonic-build", features = ["prost"]}
|
||||
@@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
prost_build::compile_protos(&["proto/status.proto"], &["proto/"]).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package google.rpc;
|
||||
|
||||
import "google/protobuf/duration.proto";
|
||||
|
||||
option go_package = "google.golang.org/genproto/googleapis/rpc/errdetails;errdetails";
|
||||
option java_multiple_files = true;
|
||||
option java_outer_classname = "ErrorDetailsProto";
|
||||
option java_package = "com.google.rpc";
|
||||
option objc_class_prefix = "RPC";
|
||||
|
||||
// Describes when the clients can retry a failed request. Clients could ignore
|
||||
// the recommendation here or retry when this information is missing from error
|
||||
// responses.
|
||||
//
|
||||
// It's always recommended that clients should use exponential backoff when
|
||||
// retrying.
|
||||
//
|
||||
// Clients should wait until `retry_delay` amount of time has passed since
|
||||
// receiving the error response before retrying. If retrying requests also
|
||||
// fail, clients should use an exponential backoff scheme to gradually increase
|
||||
// the delay between retries based on `retry_delay`, until either a maximum
|
||||
// number of retries have been reached or a maximum retry delay cap has been
|
||||
// reached.
|
||||
message RetryInfo {
|
||||
// Clients should wait at least this long between retrying the same request.
|
||||
google.protobuf.Duration retry_delay = 1;
|
||||
}
|
||||
|
||||
// Describes additional debugging info.
|
||||
message DebugInfo {
|
||||
// The stack trace entries indicating where the error occurred.
|
||||
repeated string stack_entries = 1;
|
||||
|
||||
// Additional debugging information provided by the server.
|
||||
string detail = 2;
|
||||
}
|
||||
|
||||
// Describes how a quota check failed.
|
||||
//
|
||||
// For example if a daily limit was exceeded for the calling project,
|
||||
// a service could respond with a QuotaFailure detail containing the project
|
||||
// id and the description of the quota limit that was exceeded. If the
|
||||
// calling project hasn't enabled the service in the developer console, then
|
||||
// a service could respond with the project id and set `service_disabled`
|
||||
// to true.
|
||||
//
|
||||
// Also see RetryInfo and Help types for other details about handling a
|
||||
// quota failure.
|
||||
message QuotaFailure {
|
||||
// A message type used to describe a single quota violation. For example, a
|
||||
// daily quota or a custom quota that was exceeded.
|
||||
message Violation {
|
||||
// The subject on which the quota check failed.
|
||||
// For example, "clientip:<ip address of client>" or "project:<Google
|
||||
// developer project id>".
|
||||
string subject = 1;
|
||||
|
||||
// A description of how the quota check failed. Clients can use this
|
||||
// description to find more about the quota configuration in the service's
|
||||
// public documentation, or find the relevant quota limit to adjust through
|
||||
// developer console.
|
||||
//
|
||||
// For example: "Service disabled" or "Daily Limit for read operations
|
||||
// exceeded".
|
||||
string description = 2;
|
||||
}
|
||||
|
||||
// Describes all quota violations.
|
||||
repeated Violation violations = 1;
|
||||
}
|
||||
|
||||
// Describes the cause of the error with structured details.
|
||||
//
|
||||
// Example of an error when contacting the "pubsub.googleapis.com" API when it
|
||||
// is not enabled:
|
||||
// ```json
|
||||
// { "reason": "API_DISABLED"
|
||||
// "domain": "googleapis.com"
|
||||
// "metadata": {
|
||||
// "resource": "projects/123",
|
||||
// "service": "pubsub.googleapis.com"
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
// This response indicates that the pubsub.googleapis.com API is not enabled.
|
||||
//
|
||||
// Example of an error that is returned when attempting to create a Spanner
|
||||
// instance in a region that is out of stock:
|
||||
// ```json
|
||||
// { "reason": "STOCKOUT"
|
||||
// "domain": "spanner.googleapis.com",
|
||||
// "metadata": {
|
||||
// "availableRegions": "us-central1,us-east2"
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
message ErrorInfo {
|
||||
// The reason of the error. This is a constant value that identifies the
|
||||
// proximate cause of the error. Error reasons are unique within a particular
|
||||
// domain of errors. This should be at most 63 characters and match
|
||||
// /[A-Z0-9_]+/.
|
||||
string reason = 1;
|
||||
|
||||
// The logical grouping to which the "reason" belongs. The error domain
|
||||
// is typically the registered service name of the tool or product that
|
||||
// generates the error. Example: "pubsub.googleapis.com". If the error is
|
||||
// generated by some common infrastructure, the error domain must be a
|
||||
// globally unique value that identifies the infrastructure. For Google API
|
||||
// infrastructure, the error domain is "googleapis.com".
|
||||
string domain = 2;
|
||||
|
||||
// Additional structured details about this error.
|
||||
//
|
||||
// Keys should match /[a-zA-Z0-9-_]/ and be limited to 64 characters in
|
||||
// length. When identifying the current value of an exceeded limit, the units
|
||||
// should be contained in the key, not the value. For example, rather than
|
||||
// {"instanceLimit": "100/request"}, should be returned as,
|
||||
// {"instanceLimitPerRequest": "100"}, if the client exceeds the number of
|
||||
// instances that can be created in a single (batch) request.
|
||||
map<string, string> metadata = 3;
|
||||
}
|
||||
|
||||
// Describes what preconditions have failed.
|
||||
//
|
||||
// For example, if an RPC failed because it required the Terms of Service to be
|
||||
// acknowledged, it could list the terms of service violation in the
|
||||
// PreconditionFailure message.
|
||||
message PreconditionFailure {
|
||||
// A message type used to describe a single precondition failure.
|
||||
message Violation {
|
||||
// The type of PreconditionFailure. We recommend using a service-specific
|
||||
// enum type to define the supported precondition violation subjects. For
|
||||
// example, "TOS" for "Terms of Service violation".
|
||||
string type = 1;
|
||||
|
||||
// The subject, relative to the type, that failed.
|
||||
// For example, "google.com/cloud" relative to the "TOS" type would indicate
|
||||
// which terms of service is being referenced.
|
||||
string subject = 2;
|
||||
|
||||
// A description of how the precondition failed. Developers can use this
|
||||
// description to understand how to fix the failure.
|
||||
//
|
||||
// For example: "Terms of service not accepted".
|
||||
string description = 3;
|
||||
}
|
||||
|
||||
// Describes all precondition violations.
|
||||
repeated Violation violations = 1;
|
||||
}
|
||||
|
||||
// Describes violations in a client request. This error type focuses on the
|
||||
// syntactic aspects of the request.
|
||||
message BadRequest {
|
||||
// A message type used to describe a single bad request field.
|
||||
message FieldViolation {
|
||||
// A path leading to a field in the request body. The value will be a
|
||||
// sequence of dot-separated identifiers that identify a protocol buffer
|
||||
// field. E.g., "field_violations.field" would identify this field.
|
||||
string field = 1;
|
||||
|
||||
// A description of why the request element is bad.
|
||||
string description = 2;
|
||||
}
|
||||
|
||||
// Describes all violations in a client request.
|
||||
repeated FieldViolation field_violations = 1;
|
||||
}
|
||||
|
||||
// Contains metadata about the request that clients can attach when filing a bug
|
||||
// or providing other forms of feedback.
|
||||
message RequestInfo {
|
||||
// An opaque string that should only be interpreted by the service generating
|
||||
// it. For example, it can be used to identify requests in the service's logs.
|
||||
string request_id = 1;
|
||||
|
||||
// Any data that was used to serve this request. For example, an encrypted
|
||||
// stack trace that can be sent back to the service provider for debugging.
|
||||
string serving_data = 2;
|
||||
}
|
||||
|
||||
// Describes the resource that is being accessed.
|
||||
message ResourceInfo {
|
||||
// A name for the type of resource being accessed, e.g. "sql table",
|
||||
// "cloud storage bucket", "file", "Google calendar"; or the type URL
|
||||
// of the resource: e.g. "type.googleapis.com/google.pubsub.v1.Topic".
|
||||
string resource_type = 1;
|
||||
|
||||
// The name of the resource being accessed. For example, a shared calendar
|
||||
// name: "example.com_4fghdhgsrgh@group.calendar.google.com", if the current
|
||||
// error is [google.rpc.Code.PERMISSION_DENIED][google.rpc.Code.PERMISSION_DENIED].
|
||||
string resource_name = 2;
|
||||
|
||||
// The owner of the resource (optional).
|
||||
// For example, "user:<owner email>" or "project:<Google developer project
|
||||
// id>".
|
||||
string owner = 3;
|
||||
|
||||
// Describes what error is encountered when accessing this resource.
|
||||
// For example, updating a cloud project may require the `writer` permission
|
||||
// on the developer console project.
|
||||
string description = 4;
|
||||
}
|
||||
|
||||
// Provides links to documentation or for performing an out of band action.
|
||||
//
|
||||
// For example, if a quota check failed with an error indicating the calling
|
||||
// project hasn't enabled the accessed service, this can contain a URL pointing
|
||||
// directly to the right place in the developer console to flip the bit.
|
||||
message Help {
|
||||
// Describes a URL link.
|
||||
message Link {
|
||||
// Describes what the link offers.
|
||||
string description = 1;
|
||||
|
||||
// The URL of the link.
|
||||
string url = 2;
|
||||
}
|
||||
|
||||
// URL(s) pointing to additional information on handling the current error.
|
||||
repeated Link links = 1;
|
||||
}
|
||||
|
||||
// Provides a localized error message that is safe to return to the user
|
||||
// which can be attached to an RPC error.
|
||||
message LocalizedMessage {
|
||||
// The locale used following the specification defined at
|
||||
// http://www.rfc-editor.org/rfc/bcp/bcp47.txt.
|
||||
// Examples are: "en-US", "fr-CH", "es-MX"
|
||||
string locale = 1;
|
||||
|
||||
// The localized error message in the above locale.
|
||||
string message = 2;
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
use super::std_messages::{BadRequest, FieldViolation};
|
||||
|
||||
pub(crate) mod vec;
|
||||
|
||||
/// Groups the standard error messages structs. Provides associated
|
||||
/// functions and methods to setup and edit each error message independently.
|
||||
/// Used when extracting error details from `tonic::Status`, and when
|
||||
/// creating a `tonic::Status` with error details.
|
||||
#[non_exhaustive]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ErrorDetails {
|
||||
/// This field stores [`BadRequest`] data, if any.
|
||||
pub(crate) bad_request: Option<BadRequest>,
|
||||
}
|
||||
|
||||
impl ErrorDetails {
|
||||
/// Generates an [`ErrorDetails`] struct with all fields set to `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails};
|
||||
///
|
||||
/// let err_details = ErrorDetails::new();
|
||||
/// ```
|
||||
pub fn new() -> Self {
|
||||
ErrorDetails { bad_request: None }
|
||||
}
|
||||
|
||||
/// Generates an [`ErrorDetails`] struct with [`BadRequest`] details and
|
||||
/// remaining fields set to `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails, FieldViolation};
|
||||
///
|
||||
/// let err_details = ErrorDetails::with_bad_request(vec![
|
||||
/// FieldViolation::new("field_1", "description 1"),
|
||||
/// FieldViolation::new("field_2", "description 2"),
|
||||
/// ]);
|
||||
/// ```
|
||||
pub fn with_bad_request(field_violations: Vec<FieldViolation>) -> Self {
|
||||
ErrorDetails {
|
||||
bad_request: Some(BadRequest::new(field_violations)),
|
||||
..ErrorDetails::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates an [`ErrorDetails`] struct with [`BadRequest`] details (one
|
||||
/// [`FieldViolation`] set) and remaining fields set to `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails};
|
||||
///
|
||||
/// let err_details = ErrorDetails::with_bad_request_violation(
|
||||
/// "field",
|
||||
/// "description",
|
||||
/// );
|
||||
/// ```
|
||||
pub fn with_bad_request_violation(
|
||||
field: impl Into<String>,
|
||||
description: impl Into<String>,
|
||||
) -> Self {
|
||||
ErrorDetails {
|
||||
bad_request: Some(BadRequest::with_violation(field, description)),
|
||||
..ErrorDetails::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get [`BadRequest`] details, if any
|
||||
pub fn bad_request(&self) -> Option<BadRequest> {
|
||||
self.bad_request.clone()
|
||||
}
|
||||
|
||||
/// Set [`BadRequest`] details. Can be chained with other `.set_` and
|
||||
/// `.add_` [`ErrorDetails`] methods.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails, FieldViolation};
|
||||
///
|
||||
/// let mut err_details = ErrorDetails::new();
|
||||
///
|
||||
/// err_details.set_bad_request(vec![
|
||||
/// FieldViolation::new("field_1", "description 1"),
|
||||
/// FieldViolation::new("field_2", "description 2"),
|
||||
/// ]);
|
||||
/// ```
|
||||
pub fn set_bad_request(&mut self, violations: Vec<FieldViolation>) -> &mut Self {
|
||||
self.bad_request = Some(BadRequest::new(violations));
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds a [`FieldViolation`] to [`BadRequest`] details. Sets
|
||||
/// [`BadRequest`] details if it is not set yet. Can be chained with other
|
||||
/// `.set_` and `.add_` [`ErrorDetails`] methods.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails};
|
||||
///
|
||||
/// let mut err_details = ErrorDetails::new();
|
||||
///
|
||||
/// err_details.add_bad_request_violation("field", "description");
|
||||
/// ```
|
||||
pub fn add_bad_request_violation(
|
||||
&mut self,
|
||||
field: impl Into<String>,
|
||||
description: impl Into<String>,
|
||||
) -> &mut Self {
|
||||
match &mut self.bad_request {
|
||||
Some(bad_request) => {
|
||||
bad_request.add_violation(field, description);
|
||||
}
|
||||
None => {
|
||||
self.bad_request = Some(BadRequest::with_violation(field, description));
|
||||
}
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns `true` if [`BadRequest`] is set and its `field_violations`
|
||||
/// vector is not empty, otherwise returns `false`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic_types::{ErrorDetails};
|
||||
///
|
||||
/// let mut err_details = ErrorDetails::with_bad_request(vec![]);
|
||||
///
|
||||
/// assert_eq!(err_details.has_bad_request_violations(), false);
|
||||
///
|
||||
/// err_details.add_bad_request_violation("field", "description");
|
||||
///
|
||||
/// assert_eq!(err_details.has_bad_request_violations(), true);
|
||||
/// ```
|
||||
pub fn has_bad_request_violations(&self) -> bool {
|
||||
if let Some(bad_request) = &self.bad_request {
|
||||
return !bad_request.field_violations.is_empty();
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use super::super::std_messages::BadRequest;
|
||||
|
||||
/// Wraps the structs corresponding to the standard error messages, allowing
|
||||
/// the implementation and handling of vectors containing any of them.
|
||||
#[non_exhaustive]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ErrorDetail {
|
||||
/// Wraps the [`BadRequest`] struct.
|
||||
BadRequest(BadRequest),
|
||||
}
|
||||
|
||||
impl From<BadRequest> for ErrorDetail {
|
||||
fn from(err_detail: BadRequest) -> Self {
|
||||
ErrorDetail::BadRequest(err_detail)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
/// The `Status` type defines a logical error model that is suitable for
|
||||
/// different programming environments, including REST APIs and RPC APIs. It is
|
||||
/// used by \[gRPC\](<https://github.com/grpc>). Each `Status` message contains
|
||||
/// three pieces of data: error code, error message, and error details.
|
||||
///
|
||||
/// You can find out more about this error model and how to work with it in the
|
||||
/// [API Design Guide](<https://cloud.google.com/apis/design/errors>).
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Status {
|
||||
/// The status code, which should be an enum value of \[google.rpc.Code][google.rpc.Code\].
|
||||
#[prost(int32, tag="1")]
|
||||
pub code: i32,
|
||||
/// A developer-facing error message, which should be in English. Any
|
||||
/// user-facing error message should be localized and sent in the
|
||||
/// \[google.rpc.Status.details][google.rpc.Status.details\] field, or localized by the client.
|
||||
#[prost(string, tag="2")]
|
||||
pub message: ::prost::alloc::string::String,
|
||||
/// A list of messages that carry the error details. There is a common set of
|
||||
/// message types for APIs to use.
|
||||
#[prost(message, repeated, tag="3")]
|
||||
pub details: ::prost::alloc::vec::Vec<::prost_types::Any>,
|
||||
}
|
||||
/// Describes when the clients can retry a failed request. Clients could ignore
|
||||
/// the recommendation here or retry when this information is missing from error
|
||||
/// responses.
|
||||
///
|
||||
/// It's always recommended that clients should use exponential backoff when
|
||||
/// retrying.
|
||||
///
|
||||
/// Clients should wait until `retry_delay` amount of time has passed since
|
||||
/// receiving the error response before retrying. If retrying requests also
|
||||
/// fail, clients should use an exponential backoff scheme to gradually increase
|
||||
/// the delay between retries based on `retry_delay`, until either a maximum
|
||||
/// number of retries have been reached or a maximum retry delay cap has been
|
||||
/// reached.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct RetryInfo {
|
||||
/// Clients should wait at least this long between retrying the same request.
|
||||
#[prost(message, optional, tag="1")]
|
||||
pub retry_delay: ::core::option::Option<::prost_types::Duration>,
|
||||
}
|
||||
/// Describes additional debugging info.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct DebugInfo {
|
||||
/// The stack trace entries indicating where the error occurred.
|
||||
#[prost(string, repeated, tag="1")]
|
||||
pub stack_entries: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
||||
/// Additional debugging information provided by the server.
|
||||
#[prost(string, tag="2")]
|
||||
pub detail: ::prost::alloc::string::String,
|
||||
}
|
||||
/// Describes how a quota check failed.
|
||||
///
|
||||
/// For example if a daily limit was exceeded for the calling project,
|
||||
/// a service could respond with a QuotaFailure detail containing the project
|
||||
/// id and the description of the quota limit that was exceeded. If the
|
||||
/// calling project hasn't enabled the service in the developer console, then
|
||||
/// a service could respond with the project id and set `service_disabled`
|
||||
/// to true.
|
||||
///
|
||||
/// Also see RetryInfo and Help types for other details about handling a
|
||||
/// quota failure.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct QuotaFailure {
|
||||
/// Describes all quota violations.
|
||||
#[prost(message, repeated, tag="1")]
|
||||
pub violations: ::prost::alloc::vec::Vec<quota_failure::Violation>,
|
||||
}
|
||||
/// Nested message and enum types in `QuotaFailure`.
|
||||
pub mod quota_failure {
|
||||
/// A message type used to describe a single quota violation. For example, a
|
||||
/// daily quota or a custom quota that was exceeded.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Violation {
|
||||
/// The subject on which the quota check failed.
|
||||
/// For example, "clientip:<ip address of client>" or "project:<Google
|
||||
/// developer project id>".
|
||||
#[prost(string, tag="1")]
|
||||
pub subject: ::prost::alloc::string::String,
|
||||
/// A description of how the quota check failed. Clients can use this
|
||||
/// description to find more about the quota configuration in the service's
|
||||
/// public documentation, or find the relevant quota limit to adjust through
|
||||
/// developer console.
|
||||
///
|
||||
/// For example: "Service disabled" or "Daily Limit for read operations
|
||||
/// exceeded".
|
||||
#[prost(string, tag="2")]
|
||||
pub description: ::prost::alloc::string::String,
|
||||
}
|
||||
}
|
||||
/// Describes the cause of the error with structured details.
|
||||
///
|
||||
/// Example of an error when contacting the "pubsub.googleapis.com" API when it
|
||||
/// is not enabled:
|
||||
/// ```json
|
||||
/// { "reason": "API_DISABLED"
|
||||
/// "domain": "googleapis.com"
|
||||
/// "metadata": {
|
||||
/// "resource": "projects/123",
|
||||
/// "service": "pubsub.googleapis.com"
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// This response indicates that the pubsub.googleapis.com API is not enabled.
|
||||
///
|
||||
/// Example of an error that is returned when attempting to create a Spanner
|
||||
/// instance in a region that is out of stock:
|
||||
/// ```json
|
||||
/// { "reason": "STOCKOUT"
|
||||
/// "domain": "spanner.googleapis.com",
|
||||
/// "metadata": {
|
||||
/// "availableRegions": "us-central1,us-east2"
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct ErrorInfo {
|
||||
/// The reason of the error. This is a constant value that identifies the
|
||||
/// proximate cause of the error. Error reasons are unique within a particular
|
||||
/// domain of errors. This should be at most 63 characters and match
|
||||
/// /\[A-Z0-9_\]+/.
|
||||
#[prost(string, tag="1")]
|
||||
pub reason: ::prost::alloc::string::String,
|
||||
/// The logical grouping to which the "reason" belongs. The error domain
|
||||
/// is typically the registered service name of the tool or product that
|
||||
/// generates the error. Example: "pubsub.googleapis.com". If the error is
|
||||
/// generated by some common infrastructure, the error domain must be a
|
||||
/// globally unique value that identifies the infrastructure. For Google API
|
||||
/// infrastructure, the error domain is "googleapis.com".
|
||||
#[prost(string, tag="2")]
|
||||
pub domain: ::prost::alloc::string::String,
|
||||
/// Additional structured details about this error.
|
||||
///
|
||||
/// Keys should match /\[a-zA-Z0-9-_\]/ and be limited to 64 characters in
|
||||
/// length. When identifying the current value of an exceeded limit, the units
|
||||
/// should be contained in the key, not the value. For example, rather than
|
||||
/// {"instanceLimit": "100/request"}, should be returned as,
|
||||
/// {"instanceLimitPerRequest": "100"}, if the client exceeds the number of
|
||||
/// instances that can be created in a single (batch) request.
|
||||
#[prost(map="string, string", tag="3")]
|
||||
pub metadata: ::std::collections::HashMap<::prost::alloc::string::String, ::prost::alloc::string::String>,
|
||||
}
|
||||
/// Describes what preconditions have failed.
|
||||
///
|
||||
/// For example, if an RPC failed because it required the Terms of Service to be
|
||||
/// acknowledged, it could list the terms of service violation in the
|
||||
/// PreconditionFailure message.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct PreconditionFailure {
|
||||
/// Describes all precondition violations.
|
||||
#[prost(message, repeated, tag="1")]
|
||||
pub violations: ::prost::alloc::vec::Vec<precondition_failure::Violation>,
|
||||
}
|
||||
/// Nested message and enum types in `PreconditionFailure`.
|
||||
pub mod precondition_failure {
|
||||
/// A message type used to describe a single precondition failure.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Violation {
|
||||
/// The type of PreconditionFailure. We recommend using a service-specific
|
||||
/// enum type to define the supported precondition violation subjects. For
|
||||
/// example, "TOS" for "Terms of Service violation".
|
||||
#[prost(string, tag="1")]
|
||||
pub r#type: ::prost::alloc::string::String,
|
||||
/// The subject, relative to the type, that failed.
|
||||
/// For example, "google.com/cloud" relative to the "TOS" type would indicate
|
||||
/// which terms of service is being referenced.
|
||||
#[prost(string, tag="2")]
|
||||
pub subject: ::prost::alloc::string::String,
|
||||
/// A description of how the precondition failed. Developers can use this
|
||||
/// description to understand how to fix the failure.
|
||||
///
|
||||
/// For example: "Terms of service not accepted".
|
||||
#[prost(string, tag="3")]
|
||||
pub description: ::prost::alloc::string::String,
|
||||
}
|
||||
}
|
||||
/// Describes violations in a client request. This error type focuses on the
|
||||
/// syntactic aspects of the request.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct BadRequest {
|
||||
/// Describes all violations in a client request.
|
||||
#[prost(message, repeated, tag="1")]
|
||||
pub field_violations: ::prost::alloc::vec::Vec<bad_request::FieldViolation>,
|
||||
}
|
||||
/// Nested message and enum types in `BadRequest`.
|
||||
pub mod bad_request {
|
||||
/// A message type used to describe a single bad request field.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct FieldViolation {
|
||||
/// A path leading to a field in the request body. The value will be a
|
||||
/// sequence of dot-separated identifiers that identify a protocol buffer
|
||||
/// field. E.g., "field_violations.field" would identify this field.
|
||||
#[prost(string, tag="1")]
|
||||
pub field: ::prost::alloc::string::String,
|
||||
/// A description of why the request element is bad.
|
||||
#[prost(string, tag="2")]
|
||||
pub description: ::prost::alloc::string::String,
|
||||
}
|
||||
}
|
||||
/// Contains metadata about the request that clients can attach when filing a bug
|
||||
/// or providing other forms of feedback.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct RequestInfo {
|
||||
/// An opaque string that should only be interpreted by the service generating
|
||||
/// it. For example, it can be used to identify requests in the service's logs.
|
||||
#[prost(string, tag="1")]
|
||||
pub request_id: ::prost::alloc::string::String,
|
||||
/// Any data that was used to serve this request. For example, an encrypted
|
||||
/// stack trace that can be sent back to the service provider for debugging.
|
||||
#[prost(string, tag="2")]
|
||||
pub serving_data: ::prost::alloc::string::String,
|
||||
}
|
||||
/// Describes the resource that is being accessed.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct ResourceInfo {
|
||||
/// A name for the type of resource being accessed, e.g. "sql table",
|
||||
/// "cloud storage bucket", "file", "Google calendar"; or the type URL
|
||||
/// of the resource: e.g. "type.googleapis.com/google.pubsub.v1.Topic".
|
||||
#[prost(string, tag="1")]
|
||||
pub resource_type: ::prost::alloc::string::String,
|
||||
/// The name of the resource being accessed. For example, a shared calendar
|
||||
/// name: "example.com_4fghdhgsrgh@group.calendar.google.com", if the current
|
||||
/// error is \[google.rpc.Code.PERMISSION_DENIED][google.rpc.Code.PERMISSION_DENIED\].
|
||||
#[prost(string, tag="2")]
|
||||
pub resource_name: ::prost::alloc::string::String,
|
||||
/// The owner of the resource (optional).
|
||||
/// For example, "user:<owner email>" or "project:<Google developer project
|
||||
/// id>".
|
||||
#[prost(string, tag="3")]
|
||||
pub owner: ::prost::alloc::string::String,
|
||||
/// Describes what error is encountered when accessing this resource.
|
||||
/// For example, updating a cloud project may require the `writer` permission
|
||||
/// on the developer console project.
|
||||
#[prost(string, tag="4")]
|
||||
pub description: ::prost::alloc::string::String,
|
||||
}
|
||||
/// Provides links to documentation or for performing an out of band action.
|
||||
///
|
||||
/// For example, if a quota check failed with an error indicating the calling
|
||||
/// project hasn't enabled the accessed service, this can contain a URL pointing
|
||||
/// directly to the right place in the developer console to flip the bit.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Help {
|
||||
/// URL(s) pointing to additional information on handling the current error.
|
||||
#[prost(message, repeated, tag="1")]
|
||||
pub links: ::prost::alloc::vec::Vec<help::Link>,
|
||||
}
|
||||
/// Nested message and enum types in `Help`.
|
||||
pub mod help {
|
||||
/// Describes a URL link.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct Link {
|
||||
/// Describes what the link offers.
|
||||
#[prost(string, tag="1")]
|
||||
pub description: ::prost::alloc::string::String,
|
||||
/// The URL of the link.
|
||||
#[prost(string, tag="2")]
|
||||
pub url: ::prost::alloc::string::String,
|
||||
}
|
||||
}
|
||||
/// Provides a localized error message that is safe to return to the user
|
||||
/// which can be attached to an RPC error.
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct LocalizedMessage {
|
||||
/// The locale used following the specification defined at
|
||||
/// <http://www.rfc-editor.org/rfc/bcp/bcp47.txt.>
|
||||
/// Examples are: "en-US", "fr-CH", "es-MX"
|
||||
#[prost(string, tag="1")]
|
||||
pub locale: ::prost::alloc::string::String,
|
||||
/// The localized error message in the above locale.
|
||||
#[prost(string, tag="2")]
|
||||
pub message: ::prost::alloc::string::String,
|
||||
}
|
||||
+453
-2
@@ -1,4 +1,12 @@
|
||||
//! A collection of useful protobuf types that can be used with `tonic`.
|
||||
//!
|
||||
//! This crate also introduces the [`StatusExt`] trait and implements it in
|
||||
//! [`tonic::Status`], allowing the implementation of the
|
||||
//! [gRPC Richer Error Model] with [`tonic`] in a convenient way.
|
||||
//!
|
||||
//! [`tonic::Status`]: https://docs.rs/tonic/latest/tonic/struct.Status.html
|
||||
//! [`tonic`]: https://docs.rs/tonic/latest/tonic/
|
||||
//! [gRPC Richer Error Model]: https://www.grpc.io/docs/guides/error/
|
||||
|
||||
#![warn(
|
||||
missing_debug_implementations,
|
||||
@@ -13,8 +21,451 @@
|
||||
#![doc(html_root_url = "https://docs.rs/tonic-types/0.6.0")]
|
||||
#![doc(issue_tracker_base_url = "https://github.com/hyperium/tonic/issues/")]
|
||||
|
||||
mod pb {
|
||||
include!(concat!(env!("OUT_DIR"), "/google.rpc.rs"));
|
||||
use prost::{DecodeError, Message};
|
||||
use prost_types::Any;
|
||||
use tonic::{codegen::Bytes, metadata::MetadataMap, Code};
|
||||
|
||||
/// Useful protobuf types
|
||||
pub mod pb {
|
||||
include!("generated/google.rpc.rs");
|
||||
}
|
||||
|
||||
pub use pb::Status;
|
||||
|
||||
mod error_details;
|
||||
mod std_messages;
|
||||
|
||||
pub use error_details::{vec::ErrorDetail, ErrorDetails};
|
||||
pub use std_messages::{BadRequest, FieldViolation};
|
||||
|
||||
trait IntoAny {
|
||||
fn into_any(self) -> Any;
|
||||
}
|
||||
|
||||
trait FromAny {
|
||||
fn from_any(any: Any) -> Result<Self, DecodeError>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
fn gen_details_bytes(code: Code, message: &String, details: Vec<Any>) -> Bytes {
|
||||
let status = pb::Status {
|
||||
code: code as i32,
|
||||
message: message.clone(),
|
||||
details,
|
||||
};
|
||||
|
||||
Bytes::from(status.encode_to_vec())
|
||||
}
|
||||
|
||||
/// Used to implement associated functions and methods on `tonic::Status`, that
|
||||
/// allow the addition and extraction of standard error details.
|
||||
pub trait StatusExt {
|
||||
/// Generates a `tonic::Status` with error details obtained from an
|
||||
/// [`ErrorDetails`] struct, and custom metadata.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{metadata::MetadataMap, Code, Status};
|
||||
/// use tonic_types::{ErrorDetails, StatusExt};
|
||||
///
|
||||
/// let status = Status::with_error_details_and_metadata(
|
||||
/// Code::InvalidArgument,
|
||||
/// "bad request",
|
||||
/// ErrorDetails::with_bad_request_violation("field", "description"),
|
||||
/// MetadataMap::new()
|
||||
/// );
|
||||
/// ```
|
||||
fn with_error_details_and_metadata(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: ErrorDetails,
|
||||
metadata: MetadataMap,
|
||||
) -> tonic::Status;
|
||||
|
||||
/// Generates a `tonic::Status` with error details obtained from an
|
||||
/// [`ErrorDetails`] struct.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Code, Status};
|
||||
/// use tonic_types::{ErrorDetails, StatusExt};
|
||||
///
|
||||
/// let status = Status::with_error_details(
|
||||
/// Code::InvalidArgument,
|
||||
/// "bad request",
|
||||
/// ErrorDetails::with_bad_request_violation("field", "description"),
|
||||
/// );
|
||||
/// ```
|
||||
fn with_error_details(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: ErrorDetails,
|
||||
) -> tonic::Status;
|
||||
|
||||
/// Generates a `tonic::Status` with error details provided in a vector of
|
||||
/// [`ErrorDetail`] enums, and custom metadata.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{metadata::MetadataMap, Code, Status};
|
||||
/// use tonic_types::{BadRequest, StatusExt};
|
||||
///
|
||||
/// let status = Status::with_error_details_vec_and_metadata(
|
||||
/// Code::InvalidArgument,
|
||||
/// "bad request",
|
||||
/// vec![
|
||||
/// BadRequest::with_violation("field", "description").into(),
|
||||
/// ],
|
||||
/// MetadataMap::new()
|
||||
/// );
|
||||
/// ```
|
||||
fn with_error_details_vec_and_metadata(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: impl IntoIterator<Item = ErrorDetail>,
|
||||
metadata: MetadataMap,
|
||||
) -> tonic::Status;
|
||||
|
||||
/// Generates a `tonic::Status` with error details provided in a vector of
|
||||
/// [`ErrorDetail`] enums.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Code, Status};
|
||||
/// use tonic_types::{BadRequest, StatusExt};
|
||||
///
|
||||
/// let status = Status::with_error_details_vec(
|
||||
/// Code::InvalidArgument,
|
||||
/// "bad request",
|
||||
/// vec![
|
||||
/// BadRequest::with_violation("field", "description").into(),
|
||||
/// ]
|
||||
/// );
|
||||
/// ```
|
||||
fn with_error_details_vec(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: impl IntoIterator<Item = ErrorDetail>,
|
||||
) -> tonic::Status;
|
||||
|
||||
/// Can be used to check if the error details contained in `tonic::Status`
|
||||
/// are malformed or not. Tries to get an [`ErrorDetails`] struct from a
|
||||
/// `tonic::Status`. If some `prost::DecodeError` occurs, it will be
|
||||
/// returned. If not debugging, consider using
|
||||
/// [`StatusExt::get_error_details`] or
|
||||
/// [`StatusExt::get_error_details_vec`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Status, Response};
|
||||
/// use tonic_types::{StatusExt};
|
||||
///
|
||||
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
|
||||
/// match req_result {
|
||||
/// Ok(_) => {},
|
||||
/// Err(status) => {
|
||||
/// let err_details = status.get_error_details();
|
||||
/// if let Some(bad_request) = err_details.bad_request() {
|
||||
/// // Handle bad_request details
|
||||
/// }
|
||||
/// // ...
|
||||
/// }
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
fn check_error_details(&self) -> Result<ErrorDetails, DecodeError>;
|
||||
|
||||
/// Get an [`ErrorDetails`] struct from `tonic::Status`. If some
|
||||
/// `prost::DecodeError` occurs, an empty [`ErrorDetails`] struct will be
|
||||
/// returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Status, Response};
|
||||
/// use tonic_types::{StatusExt};
|
||||
///
|
||||
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
|
||||
/// match req_result {
|
||||
/// Ok(_) => {},
|
||||
/// Err(status) => {
|
||||
/// let err_details = status.get_error_details();
|
||||
/// if let Some(bad_request) = err_details.bad_request() {
|
||||
/// // Handle bad_request details
|
||||
/// }
|
||||
/// // ...
|
||||
/// }
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
fn get_error_details(&self) -> ErrorDetails;
|
||||
|
||||
/// Can be used to check if the error details contained in `tonic::Status`
|
||||
/// are malformed or not. Tries to get a vector of [`ErrorDetail`] enums
|
||||
/// from a `tonic::Status`. If some `prost::DecodeError` occurs, it will be
|
||||
/// returned. If not debugging, consider using
|
||||
/// [`StatusExt::get_error_details_vec`] or
|
||||
/// [`StatusExt::get_error_details`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Status, Response};
|
||||
/// use tonic_types::{ErrorDetail, StatusExt};
|
||||
///
|
||||
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
|
||||
/// match req_result {
|
||||
/// Ok(_) => {},
|
||||
/// Err(status) => {
|
||||
/// match status.check_error_details_vec() {
|
||||
/// Ok(err_details) => {
|
||||
/// // Handle extracted details
|
||||
/// }
|
||||
/// Err(decode_error) => {
|
||||
/// // Handle decode_error
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
fn check_error_details_vec(&self) -> Result<Vec<ErrorDetail>, DecodeError>;
|
||||
|
||||
/// Get a vector of [`ErrorDetail`] enums from `tonic::Status`. If some
|
||||
/// `prost::DecodeError` occurs, an empty vector will be returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Status, Response};
|
||||
/// use tonic_types::{ErrorDetail, StatusExt};
|
||||
///
|
||||
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
|
||||
/// match req_result {
|
||||
/// Ok(_) => {},
|
||||
/// Err(status) => {
|
||||
/// let err_details = status.get_error_details_vec();
|
||||
/// for err_detail in err_details.iter() {
|
||||
/// match err_detail {
|
||||
/// ErrorDetail::BadRequest(bad_request) => {
|
||||
/// // Handle bad_request details
|
||||
/// }
|
||||
/// // ...
|
||||
/// _ => {}
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
fn get_error_details_vec(&self) -> Vec<ErrorDetail>;
|
||||
|
||||
/// Get first [`BadRequest`] details found on `tonic::Status`, if any. If
|
||||
/// some `prost::DecodeError` occurs, returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tonic::{Status, Response};
|
||||
/// use tonic_types::{StatusExt};
|
||||
///
|
||||
/// fn handle_request_result<T>(req_result: Result<Response<T>, Status>) {
|
||||
/// match req_result {
|
||||
/// Ok(_) => {},
|
||||
/// Err(status) => {
|
||||
/// if let Some(bad_request) = status.get_details_bad_request() {
|
||||
/// // Handle bad_request details
|
||||
/// }
|
||||
/// }
|
||||
/// };
|
||||
/// }
|
||||
/// ```
|
||||
fn get_details_bad_request(&self) -> Option<BadRequest>;
|
||||
}
|
||||
|
||||
impl StatusExt for tonic::Status {
|
||||
fn with_error_details_and_metadata(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: ErrorDetails,
|
||||
metadata: MetadataMap,
|
||||
) -> Self {
|
||||
let message: String = message.into();
|
||||
|
||||
let mut conv_details: Vec<Any> = Vec::with_capacity(10);
|
||||
|
||||
if let Some(bad_request) = details.bad_request {
|
||||
conv_details.push(bad_request.into_any());
|
||||
}
|
||||
|
||||
let details = gen_details_bytes(code, &message, conv_details);
|
||||
|
||||
tonic::Status::with_details_and_metadata(code, message, details, metadata)
|
||||
}
|
||||
|
||||
fn with_error_details(code: Code, message: impl Into<String>, details: ErrorDetails) -> Self {
|
||||
tonic::Status::with_error_details_and_metadata(code, message, details, MetadataMap::new())
|
||||
}
|
||||
|
||||
fn with_error_details_vec_and_metadata(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: impl IntoIterator<Item = ErrorDetail>,
|
||||
metadata: MetadataMap,
|
||||
) -> Self {
|
||||
let message: String = message.into();
|
||||
|
||||
let mut conv_details: Vec<Any> = Vec::new();
|
||||
|
||||
for error_detail in details.into_iter() {
|
||||
match error_detail {
|
||||
ErrorDetail::BadRequest(bad_req) => {
|
||||
conv_details.push(bad_req.into_any());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let details = gen_details_bytes(code, &message, conv_details);
|
||||
|
||||
tonic::Status::with_details_and_metadata(code, message, details, metadata)
|
||||
}
|
||||
|
||||
fn with_error_details_vec(
|
||||
code: Code,
|
||||
message: impl Into<String>,
|
||||
details: impl IntoIterator<Item = ErrorDetail>,
|
||||
) -> Self {
|
||||
tonic::Status::with_error_details_vec_and_metadata(
|
||||
code,
|
||||
message,
|
||||
details,
|
||||
MetadataMap::new(),
|
||||
)
|
||||
}
|
||||
|
||||
fn check_error_details(&self) -> Result<ErrorDetails, DecodeError> {
|
||||
let status = pb::Status::decode(self.details())?;
|
||||
|
||||
let mut details = ErrorDetails::new();
|
||||
|
||||
for any in status.details.into_iter() {
|
||||
match any.type_url.as_str() {
|
||||
BadRequest::TYPE_URL => {
|
||||
details.bad_request = Some(BadRequest::from_any(any)?);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(details)
|
||||
}
|
||||
|
||||
fn get_error_details(&self) -> ErrorDetails {
|
||||
self.check_error_details().unwrap_or(ErrorDetails::new())
|
||||
}
|
||||
|
||||
fn check_error_details_vec(&self) -> Result<Vec<ErrorDetail>, DecodeError> {
|
||||
let status = pb::Status::decode(self.details())?;
|
||||
|
||||
let mut details: Vec<ErrorDetail> = Vec::with_capacity(status.details.len());
|
||||
|
||||
for any in status.details.into_iter() {
|
||||
match any.type_url.as_str() {
|
||||
BadRequest::TYPE_URL => {
|
||||
details.push(BadRequest::from_any(any)?.into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(details)
|
||||
}
|
||||
|
||||
fn get_error_details_vec(&self) -> Vec<ErrorDetail> {
|
||||
self.check_error_details_vec().unwrap_or(Vec::new())
|
||||
}
|
||||
|
||||
fn get_details_bad_request(&self) -> Option<BadRequest> {
|
||||
let status = pb::Status::decode(self.details()).ok()?;
|
||||
|
||||
for any in status.details.into_iter() {
|
||||
match any.type_url.as_str() {
|
||||
BadRequest::TYPE_URL => match BadRequest::from_any(any) {
|
||||
Ok(detail) => return Some(detail),
|
||||
Err(_) => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tonic::{Code, Status};
|
||||
|
||||
use super::{BadRequest, ErrorDetails, StatusExt};
|
||||
|
||||
#[test]
|
||||
fn gen_status_with_details() {
|
||||
let mut err_details = ErrorDetails::new();
|
||||
|
||||
err_details.add_bad_request_violation("field", "description");
|
||||
|
||||
let fmt_details = format!("{:?}", err_details);
|
||||
|
||||
let err_details_vec = vec![BadRequest::with_violation("field", "description").into()];
|
||||
|
||||
let fmt_details_vec = format!("{:?}", err_details_vec);
|
||||
|
||||
let status_from_struct = Status::with_error_details(
|
||||
Code::InvalidArgument,
|
||||
"error with bad request details",
|
||||
err_details,
|
||||
);
|
||||
|
||||
let status_from_vec = Status::with_error_details_vec(
|
||||
Code::InvalidArgument,
|
||||
"error with bad request details",
|
||||
err_details_vec,
|
||||
);
|
||||
|
||||
let ext_details = match status_from_vec.check_error_details() {
|
||||
Ok(ext_details) => ext_details,
|
||||
Err(err) => panic!(
|
||||
"Error extracting details struct from status_from_vec: {:?}",
|
||||
err
|
||||
),
|
||||
};
|
||||
|
||||
let fmt_ext_details = format!("{:?}", ext_details);
|
||||
|
||||
assert!(
|
||||
fmt_ext_details.eq(&fmt_details),
|
||||
"Extracted details struct differs from original details struct"
|
||||
);
|
||||
|
||||
let ext_details_vec = match status_from_struct.check_error_details_vec() {
|
||||
Ok(ext_details) => ext_details,
|
||||
Err(err) => panic!(
|
||||
"Error extracting details_vec from status_from_struct: {:?}",
|
||||
err
|
||||
),
|
||||
};
|
||||
|
||||
let fmt_ext_details_vec = format!("{:?}", ext_details_vec);
|
||||
|
||||
assert!(
|
||||
fmt_ext_details_vec.eq(&fmt_details_vec),
|
||||
"Extracted details vec differs from original details vec"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
mod bad_request;
|
||||
|
||||
pub use bad_request::{BadRequest, FieldViolation};
|
||||
@@ -0,0 +1,183 @@
|
||||
use prost::{DecodeError, Message};
|
||||
use prost_types::Any;
|
||||
|
||||
use super::super::pb;
|
||||
use super::super::{FromAny, IntoAny};
|
||||
|
||||
/// Used at the `field_violations` field of the [`BadRequest`] struct.
|
||||
/// Describes a single bad request field.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FieldViolation {
|
||||
/// Path leading to a field in the request body. Value should be a
|
||||
/// sequence of dot-separated identifiers that identify a protocol buffer
|
||||
/// field.
|
||||
pub field: String,
|
||||
|
||||
/// Description of why the field is bad.
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl FieldViolation {
|
||||
/// Creates a new [`FieldViolation`] struct.
|
||||
pub fn new(field: impl Into<String>, description: impl Into<String>) -> Self {
|
||||
FieldViolation {
|
||||
field: field.into(),
|
||||
description: description.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used to encode/decode the `BadRequest` standard error message described in
|
||||
/// [error_details.proto]. Describes violations in a client request. Focuses
|
||||
/// on the syntactic aspects of the request.
|
||||
///
|
||||
/// [error_details.proto]: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BadRequest {
|
||||
/// Describes all field violations of the request.
|
||||
pub field_violations: Vec<FieldViolation>,
|
||||
}
|
||||
|
||||
impl BadRequest {
|
||||
/// Type URL of the `BadRequest` standard error message type.
|
||||
pub const TYPE_URL: &'static str = "type.googleapis.com/google.rpc.BadRequest";
|
||||
|
||||
/// Creates a new [`BadRequest`] struct.
|
||||
pub fn new(field_violations: Vec<FieldViolation>) -> Self {
|
||||
BadRequest { field_violations }
|
||||
}
|
||||
|
||||
/// Creates a new [`BadRequest`] struct with a single [`FieldViolation`] in
|
||||
/// `field_violations`.
|
||||
pub fn with_violation(field: impl Into<String>, description: impl Into<String>) -> Self {
|
||||
BadRequest {
|
||||
field_violations: vec![FieldViolation {
|
||||
field: field.into(),
|
||||
description: description.into(),
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a [`FieldViolation`] to [`BadRequest`]'s `field_violations`.
|
||||
pub fn add_violation(
|
||||
&mut self,
|
||||
field: impl Into<String>,
|
||||
description: impl Into<String>,
|
||||
) -> &mut Self {
|
||||
self.field_violations.append(&mut vec![FieldViolation {
|
||||
field: field.into(),
|
||||
description: description.into(),
|
||||
}]);
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns `true` if [`BadRequest`]'s `field_violations` vector is empty,
|
||||
/// and `false` if it is not.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.field_violations.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAny for BadRequest {
|
||||
fn into_any(self) -> Any {
|
||||
let detail_data = pb::BadRequest {
|
||||
field_violations: self
|
||||
.field_violations
|
||||
.into_iter()
|
||||
.map(|v| pb::bad_request::FieldViolation {
|
||||
field: v.field,
|
||||
description: v.description,
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
Any {
|
||||
type_url: BadRequest::TYPE_URL.to_string(),
|
||||
value: detail_data.encode_to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAny for BadRequest {
|
||||
fn from_any(any: Any) -> Result<Self, DecodeError> {
|
||||
let buf: &[u8] = &any.value;
|
||||
let bad_req = pb::BadRequest::decode(buf)?;
|
||||
|
||||
let bad_req = BadRequest {
|
||||
field_violations: bad_req
|
||||
.field_violations
|
||||
.into_iter()
|
||||
.map(|v| FieldViolation {
|
||||
field: v.field,
|
||||
description: v.description,
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
|
||||
Ok(bad_req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::super::{FromAny, IntoAny};
|
||||
use super::BadRequest;
|
||||
|
||||
#[test]
|
||||
fn gen_bad_request() {
|
||||
let mut br_details = BadRequest::new(Vec::new());
|
||||
let formatted = format!("{:?}", br_details);
|
||||
|
||||
let expected = "BadRequest { field_violations: [] }";
|
||||
|
||||
assert!(
|
||||
formatted.eq(expected),
|
||||
"empty BadRequest differs from expected result"
|
||||
);
|
||||
|
||||
assert!(
|
||||
br_details.is_empty(),
|
||||
"empty BadRequest returns 'false' from .is_empty()"
|
||||
);
|
||||
|
||||
br_details
|
||||
.add_violation("field_a", "description_a")
|
||||
.add_violation("field_b", "description_b");
|
||||
|
||||
let formatted = format!("{:?}", br_details);
|
||||
|
||||
let expected_filled = "BadRequest { field_violations: [FieldViolation { field: \"field_a\", description: \"description_a\" }, FieldViolation { field: \"field_b\", description: \"description_b\" }] }";
|
||||
|
||||
assert!(
|
||||
formatted.eq(expected_filled),
|
||||
"filled BadRequest differs from expected result"
|
||||
);
|
||||
|
||||
assert!(
|
||||
br_details.is_empty() == false,
|
||||
"filled BadRequest returns 'true' from .is_empty()"
|
||||
);
|
||||
|
||||
let gen_any = br_details.into_any();
|
||||
let formatted = format!("{:?}", gen_any);
|
||||
|
||||
let expected = "Any { type_url: \"type.googleapis.com/google.rpc.BadRequest\", value: [10, 24, 10, 7, 102, 105, 101, 108, 100, 95, 97, 18, 13, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 95, 97, 10, 24, 10, 7, 102, 105, 101, 108, 100, 95, 98, 18, 13, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 95, 98] }";
|
||||
|
||||
assert!(
|
||||
formatted.eq(expected),
|
||||
"Any from filled BadRequest differs from expected result"
|
||||
);
|
||||
|
||||
let br_details = match BadRequest::from_any(gen_any) {
|
||||
Err(error) => panic!("Error generating BadRequest from Any: {:?}", error),
|
||||
Ok(from_any) => from_any,
|
||||
};
|
||||
|
||||
let formatted = format!("{:?}", br_details);
|
||||
|
||||
assert!(
|
||||
formatted.eq(expected_filled),
|
||||
"BadRequest from Any differs from expected result"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use std::{path::PathBuf, process::Command};
|
||||
|
||||
#[test]
|
||||
fn bootstrap() {
|
||||
let iface_files = &["proto/status.proto", "proto/error_details.proto"];
|
||||
let dirs = &["proto"];
|
||||
|
||||
let out_dir = PathBuf::from(std::env!("CARGO_MANIFEST_DIR"))
|
||||
.join("src")
|
||||
.join("generated");
|
||||
|
||||
tonic_build::configure()
|
||||
.out_dir(format!("{}", out_dir.display()))
|
||||
.compile(iface_files, dirs)
|
||||
.unwrap();
|
||||
|
||||
let status = Command::new("git")
|
||||
.arg("diff")
|
||||
.arg("--exit-code")
|
||||
.arg("--")
|
||||
.arg(format!("{}", out_dir.display()))
|
||||
.status()
|
||||
.unwrap();
|
||||
|
||||
if !status.success() {
|
||||
panic!("You should commit the protobuf files");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user