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(errorx): add Request Timeout http error handling #245

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
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;
}
Loading