mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 13:52:51 +00:00
# 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]>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package connect_middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"connectrpc.com/connect"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
func emptyMockHandler(resp connect.AnyResponse, expectedCtxData authz.CtxData) func(*testing.T) connect.UnaryFunc {
|
|
return func(t *testing.T) connect.UnaryFunc {
|
|
return func(ctx context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {
|
|
assert.Equal(t, expectedCtxData, authz.GetCtxData(ctx))
|
|
return resp, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func errorMockHandler() func(*testing.T) connect.UnaryFunc {
|
|
return func(t *testing.T) connect.UnaryFunc {
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "test", "error")
|
|
}
|
|
}
|
|
}
|
|
|
|
func panicMockHandler(payload any) func(*testing.T) connect.UnaryFunc {
|
|
return func(t *testing.T) connect.UnaryFunc {
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
panic(payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
type mockReq[t any] struct {
|
|
connect.Request[t]
|
|
|
|
procedure string
|
|
header http.Header
|
|
}
|
|
|
|
func (m *mockReq[T]) Spec() connect.Spec {
|
|
return connect.Spec{
|
|
Procedure: m.procedure,
|
|
}
|
|
}
|
|
|
|
func (m *mockReq[T]) Header() http.Header {
|
|
if m.header == nil {
|
|
m.header = make(http.Header)
|
|
}
|
|
return m.header
|
|
}
|