This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Iaroslav Ciupin <[email protected]>
- Loading branch information
1 parent
38a233d
commit d492640
Showing
2 changed files
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/flyteorg/flyteadmin/pkg/common" | ||
"github.com/flyteorg/flyteadmin/pkg/errors" | ||
|
||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
// Transforms errors to grpc-compatible error types and optionally truncates it if necessary. | ||
// TransformAndRecordError transforms errors to grpc-compatible error types and optionally truncates it if necessary. | ||
func TransformAndRecordError(err error, metrics *RequestMetrics) error { | ||
var errorMessage = err.Error() | ||
concatenateErrMessage := false | ||
if len(errorMessage) > common.MaxResponseStatusBytes { | ||
errorMessage = err.Error()[:common.MaxResponseStatusBytes] | ||
concatenateErrMessage = true | ||
errMsg := err.Error() | ||
shouldTruncate := false | ||
if len(errMsg) > common.MaxResponseStatusBytes { | ||
errMsg = errMsg[:common.MaxResponseStatusBytes] | ||
shouldTruncate = true | ||
} | ||
if flyteAdminError, ok := err.(errors.FlyteAdminError); !ok { | ||
err = errors.NewFlyteAdminError(codes.Internal, errorMessage) | ||
} else if concatenateErrMessage { | ||
err = errors.NewFlyteAdminError(flyteAdminError.Code(), errorMessage) | ||
|
||
adminErr, isAdminErr := err.(errors.FlyteAdminError) | ||
grpcStatus, isStatus := status.FromError(err) | ||
switch { | ||
case isAdminErr: | ||
if shouldTruncate { | ||
adminErr = errors.NewFlyteAdminError(adminErr.Code(), errMsg) | ||
} | ||
case isStatus: | ||
adminErr = errors.NewFlyteAdminError(grpcStatus.Code(), errMsg) | ||
default: | ||
adminErr = errors.NewFlyteAdminError(codes.Internal, errMsg) | ||
} | ||
metrics.Record(err.(errors.FlyteAdminError).Code()) | ||
return err | ||
|
||
metrics.Record(adminErr.Code()) | ||
return adminErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters