mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-31 19:42:15 +00:00
fix: recover from request panics (#11713)
# Which Problems Are Solved Panics may cause a service discruption by unexpectedly closing an request's connection. Or in the case of gRPC completely killing the service. Allthough panics are still individual bugs that need to be solved, this PR makes sure a panic is gracefully handled and an understandable error is returned to the client. # How the Problems Are Solved - Recover in the middleware interceptors for the 3 API protocols (HTTP, gRPC, connect). - HTTP middleware uses formatted responses for: - OIDC errors (JSON formatted response) - UI (error page rendering) - SCIM - Upon recovery an alert level log is printed (ERROR+4 for stdlib log handlers) # Additional Context - internal observation --------- Co-authored-by: Livio Spring <[email protected]> Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
co-authored by
Livio Spring
Livio Spring
parent
bebee4c795
commit
5934e07960
@@ -2,11 +2,13 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/grpc/gerrors"
|
||||
_ "github.com/zitadel/zitadel/internal/statik"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func ErrorHandler() grpc.UnaryServerInterceptor {
|
||||
@@ -15,7 +17,21 @@ func ErrorHandler() grpc.UnaryServerInterceptor {
|
||||
}
|
||||
}
|
||||
|
||||
func toGRPCError(ctx context.Context, req interface{}, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
resp, err := handler(ctx, req)
|
||||
return resp, gerrors.ZITADELToGRPCError(ctx, err)
|
||||
func toGRPCError(ctx context.Context, req interface{}, handler grpc.UnaryHandler) (_ interface{}, err error) {
|
||||
ctx, cancel := context.WithCancelCause(ctx)
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
recErr, ok := rec.(error)
|
||||
if !ok {
|
||||
recErr = fmt.Errorf("%v", rec)
|
||||
}
|
||||
if recErr != nil {
|
||||
err = zerrors.ThrowInternal(recErr, zerrors.IDRecover, "Errors.Internal")
|
||||
}
|
||||
}
|
||||
cause := err // avoid passing the transport error as cancel cause.
|
||||
err = gerrors.ZITADELToGRPCError(ctx, err)
|
||||
cancel(cause)
|
||||
}()
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user