Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(errors): allow Echo to auto-populate fake error details #1456

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions schema/google/showcase/v1beta1/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ message EchoRequest {
string request_id = 7 [
(google.api.field_info).format = UUID4
];

// If `error` is set, then this flag causes the Showcase server to append
// multiple fake error details in the error response. This is useful for
// testing client behavior on error responses.
bool generate_error_details = 8;
}

// The response message for the Echo methods.
Expand Down
92 changes: 89 additions & 3 deletions server/services/echo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
anypb "google.golang.org/protobuf/types/known/anypb"
durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// NewEchoServer returns a new EchoServer for the Showcase API.
Expand All @@ -44,10 +45,95 @@ type echoServerImpl struct {
waiter server.Waiter
}

func PopulateFakeErrorDetails(grpcStatus *status.Status) (*status.Status, error) {
return grpcStatus.WithDetails(
&errdetails.ErrorInfo{
Reason: "(showcase:ErrorInfo) RPC requested we populate all error details",
Domain: "googleapis.com",
},
&errdetails.RetryInfo{
RetryDelay: &durationpb.Duration{
Seconds: 3,
Nanos: 14159265,
},
},
&errdetails.DebugInfo{
StackEntries: []string{"frame 1", "frame 2", "frame3"},
Detail: "(showcase:DebugInfo) sample stack frames",
},
&errdetails.QuotaFailure{
Violations: []*errdetails.QuotaFailure_Violation{
{
Subject: "(showcase:QuotaFailure) showcase-testing:fake errors",
Description: "This is fake quota error 0",
},
{
Subject: "(showcase:QuotaFailure) showcase-testing:more fake errors",
Description: "This is fake quota error 1",
},
},
},
&errdetails.PreconditionFailure{
Violations: []*errdetails.PreconditionFailure_Violation{
{
Type: "Fake Type 0",
Subject: "(showcase:PreconditionFailure) showcase-testing:fake errors",
Description: "This is fake precondition error 0",
},
{
Type: "Fake Type 1",
Subject: "(showcase:PreconditionFailure) showcase-testing:fake errors",
Description: "This is fake precondition error 1",
},
},
},
&errdetails.BadRequest{
FieldViolations: []*errdetails.BadRequest_FieldViolation{
{
Field: "field 0",
Description: "(showcase:BadRequest) description 0",
},
{
Field: "field 1",
Description: "(showcase:BadRequest) description 1",
},
},
},
&errdetails.Help{
Links: []*errdetails.Help_Link{
{
Description: "(showcase:Help) Description 0",
Url: "URL 0",
},
{
Description: "(showcase:Help) Description 1",
Url: "URL 1",
},
},
},
&errdetails.LocalizedMessage{
Locale: "en-US",
Message: "(showcase:LocalizedMessage) Some message for the user",
},
)
}

func (s *echoServerImpl) Echo(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
err := status.ErrorProto(in.GetError())
if err != nil {
return nil, err
requestedError := in.GetError()
if requestedError != nil {
if in.GenerateErrorDetails {
returnDetails, err := PopulateFakeErrorDetails(status.FromProto(requestedError))
if err != nil {
return nil, err
}
if returnDetails != nil {
return nil, returnDetails.Err()
}
}
err := status.ErrorProto(requestedError)
if err != nil {
return nil, err
}
}
echoHeaders(ctx)
echoTrailers(ctx)
Expand Down
Loading