template-monorepo/internal/library/errors/http_grpc.go

42 lines
1.1 KiB
Go
Raw Normal View History

2026-05-19 11:00:28 +00:00
package errs
import (
"net/http"
"gateway/internal/library/errors/code"
"google.golang.org/grpc/codes"
)
// httpStatusFromGRPCDetail maps a stored gRPC codes.Code (in Detail) to HTTP.
// Used for CatGRPC errors produced by FromGRPCError.
func httpStatusFromGRPCDetail(d code.Detail) int {
switch codes.Code(d) {
case codes.OK:
return http.StatusOK
case codes.InvalidArgument, codes.OutOfRange, codes.FailedPrecondition:
return http.StatusBadRequest
case codes.NotFound:
return http.StatusNotFound
case codes.AlreadyExists, codes.Aborted:
return http.StatusConflict
case codes.PermissionDenied:
return http.StatusForbidden
case codes.Unauthenticated:
return http.StatusUnauthorized
case codes.ResourceExhausted:
return http.StatusTooManyRequests
case codes.DeadlineExceeded:
return http.StatusGatewayTimeout
case codes.Unavailable:
return http.StatusServiceUnavailable
case codes.Unimplemented:
return http.StatusNotImplemented
case codes.Canceled:
return http.StatusRequestTimeout
case codes.Unknown, codes.Internal, codes.DataLoss:
return http.StatusInternalServerError
default:
return http.StatusInternalServerError
}
}