Skip to content

Commit

Permalink
feat(errorx): add Request Timeout http error handling (#245)
Browse files Browse the repository at this point in the history
Some Influx clients do not send the request in time. We need this
error code to be able to have the right status for the client and also
to be able to update the correct metric.

Signed-off-by: György Krajcsovits <[email protected]>
  • Loading branch information
krajorama authored Jan 6, 2025
1 parent 130b7f6 commit ad9a596
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
38 changes: 38 additions & 0 deletions pkg/errorx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func FromGRPCStatus(s *grpcStatus.Status) error { //nolint:gocyclo
return TooManyRequests{Msg: msg}
case errorxpb.ErrorxType_UNSUPPORTED_MEDIA_TYPE:
return UnsupportedMediaType{Msg: msg}
case errorxpb.ErrorxType_REQUEST_TIMEOUT:
return RequestTimeout{Msg: msg}
default:
return Internal{Msg: "invalid errorx type specifier. " + msg}
}
Expand Down Expand Up @@ -405,6 +407,42 @@ func (e TooManyRequests) GRPCStatusDetails() []protov1.Message {
}}
}

var _ Error = RequestTimeout{}

type RequestTimeout struct {
Msg string
Err error
}

func (e RequestTimeout) Error() string {
if e.Err != nil {
return fmt.Sprintf("%s: %s", e.Msg, e.Err)
}
return e.Msg
}

func (e RequestTimeout) Message() string {
return e.Msg
}

func (e RequestTimeout) Unwrap() error {
return e.Err
}

func (e RequestTimeout) HTTPStatusCode() int {
return http.StatusRequestTimeout
}

func (e RequestTimeout) GRPCStatus() *grpcStatus.Status {
return WithErrorxTypeDetail(grpcStatus.New(codes.DeadlineExceeded, e.Error()), e.GRPCStatusDetails()...)
}

func (e RequestTimeout) GRPCStatusDetails() []protov1.Message {
return []protov1.Message{&errorxpb.ErrorDetails{
Type: errorxpb.ErrorxType_REQUEST_TIMEOUT,
}}
}

func TryUnwrap(err error) error {
if wrapped, ok := err.(interface{ Unwrap() error }); ok {
return wrapped.Unwrap()
Expand Down
5 changes: 5 additions & 0 deletions pkg/errorx/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func TestGRPCStatusRoundTrip(t *testing.T) {
err: TooManyRequests{Msg: "too much!"},
wantErr: TooManyRequests{Msg: "grpc ResourceExhausted: too much!"},
},
{
name: "RequestTimeout",
err: RequestTimeout{Msg: "client timeout"},
wantErr: RequestTimeout{Msg: "grpc DeadlineExceeded: client timeout"},
},
}

for _, tc := range tests {
Expand Down
4 changes: 4 additions & 0 deletions pkg/errorx/http_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func TestLogAndSetHttpError(t *testing.T) {
expectedCode: http.StatusInternalServerError,
expectedMessage: "unknown error",
},
"request timeout": {
err: RequestTimeout{},
expectedCode: http.StatusRequestTimeout,
},
} {
logger := log.NewNopLogger()
recorder := httptest.NewRecorder()
Expand Down
14 changes: 9 additions & 5 deletions pkg/errorxpb/errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protos/errorx/v1/errors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ enum ErrorxType {
CONFLICT = 8;
TOO_MANY_REQUESTS = 9;
UNSUPPORTED_MEDIA_TYPE = 10;
REQUEST_TIMEOUT = 11;
}

0 comments on commit ad9a596

Please sign in to comment.