@@ -8,11 +8,14 @@ import (
88 "github.com/stretchr/testify/require"
99 enumspb "go.temporal.io/api/enums/v1"
1010 "go.temporal.io/api/serviceerror"
11+ "go.temporal.io/server/common/dynamicconfig"
1112 "go.temporal.io/server/common/persistence/serialization"
1213 "google.golang.org/grpc/codes"
1314 "google.golang.org/grpc/status"
1415)
1516
17+ const testMaxMessageLength = 4000
18+
1619type UnaryHandler func (ctx context.Context , req any ) (any , error )
1720
1821type (
@@ -28,16 +31,17 @@ func (e *ErrorWithoutStatus) Error() string {
2831
2932// Error returns string message.
3033func TestServiceErrorInterceptorUnknown (t * testing.T ) {
34+ interceptor := NewServiceErrorInterceptor (dynamicconfig .GetIntPropertyFn (testMaxMessageLength ))
3135
32- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
36+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
3337 func (ctx context.Context , req any ) (any , error ) {
3438 return nil , status .Error (codes .InvalidArgument , "invalid argument" )
3539 })
3640
3741 require .Error (t , err )
3842 require .Equal (t , codes .InvalidArgument , status .Code (err ))
3943
40- _ , err = ServiceErrorInterceptor (t .Context (), nil , nil ,
44+ _ , err = interceptor . Intercept (t .Context (), nil , nil ,
4145 func (ctx context.Context , req any ) (any , error ) {
4246 errWithoutStatus := & ErrorWithoutStatus {
4347 Message : "unknown error without status" ,
@@ -50,12 +54,13 @@ func TestServiceErrorInterceptorUnknown(t *testing.T) {
5054}
5155
5256func TestServiceErrorInterceptorSer (t * testing.T ) {
57+ interceptor := NewServiceErrorInterceptor (dynamicconfig .GetIntPropertyFn (testMaxMessageLength ))
5358 serErrors := []error {
5459 serialization .NewDeserializationError (enumspb .ENCODING_TYPE_PROTO3 , nil ),
5560 serialization .NewSerializationError (enumspb .ENCODING_TYPE_PROTO3 , nil ),
5661 }
5762 for _ , inErr := range serErrors {
58- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
63+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
5964 func (_ context.Context , _ any ) (any , error ) {
6065 return nil , inErr
6166 })
@@ -64,8 +69,10 @@ func TestServiceErrorInterceptorSer(t *testing.T) {
6469}
6570
6671func TestServiceErrorInterceptorTruncation (t * testing.T ) {
72+ interceptor := NewServiceErrorInterceptor (dynamicconfig .GetIntPropertyFn (testMaxMessageLength ))
73+
6774 t .Run ("nil error is not affected" , func (t * testing.T ) {
68- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
75+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
6976 func (_ context.Context , _ any ) (any , error ) {
7077 return "ok" , nil
7178 })
@@ -74,7 +81,7 @@ func TestServiceErrorInterceptorTruncation(t *testing.T) {
7481
7582 t .Run ("short message is not truncated" , func (t * testing.T ) {
7683 msg := "short error"
77- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
84+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
7885 func (_ context.Context , _ any ) (any , error ) {
7986 return nil , serviceerror .NewInternal (msg )
8087 })
@@ -84,8 +91,8 @@ func TestServiceErrorInterceptorTruncation(t *testing.T) {
8491 })
8592
8693 t .Run ("message at exact limit is not truncated" , func (t * testing.T ) {
87- msg := strings .Repeat ("a" , maxMessageLength )
88- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
94+ msg := strings .Repeat ("a" , testMaxMessageLength )
95+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
8996 func (_ context.Context , _ any ) (any , error ) {
9097 return nil , serviceerror .NewInternal (msg )
9198 })
@@ -95,20 +102,20 @@ func TestServiceErrorInterceptorTruncation(t *testing.T) {
95102 })
96103
97104 t .Run ("message over limit is truncated" , func (t * testing.T ) {
98- msg := strings .Repeat ("a" , maxMessageLength + 100 )
99- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
105+ msg := strings .Repeat ("a" , testMaxMessageLength + 100 )
106+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
100107 func (_ context.Context , _ any ) (any , error ) {
101108 return nil , serviceerror .NewInternal (msg )
102109 })
103110 require .Error (t , err )
104111 st := status .Convert (err )
105- require .LessOrEqual (t , len (st .Message ()), maxMessageLength )
112+ require .LessOrEqual (t , len (st .Message ()), testMaxMessageLength )
106113 require .True (t , strings .HasSuffix (st .Message (), truncatedSuffix ))
107114 })
108115
109116 t .Run ("truncation preserves error code" , func (t * testing.T ) {
110- msg := strings .Repeat ("x" , maxMessageLength + 500 )
111- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
117+ msg := strings .Repeat ("x" , testMaxMessageLength + 500 )
118+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
112119 func (_ context.Context , _ any ) (any , error ) {
113120 return nil , serviceerror .NewNotFound (msg )
114121 })
@@ -119,15 +126,15 @@ func TestServiceErrorInterceptorTruncation(t *testing.T) {
119126 t .Run ("truncation respects multi-byte UTF-8 boundary" , func (t * testing.T ) {
120127 // Fill up to near the limit with multi-byte characters (3 bytes each for '€')
121128 // then push over the limit so truncation must split within the repeated chars.
122- euroCount := maxMessageLength / len ("€" ) // each '€' is 3 bytes
129+ euroCount := testMaxMessageLength / len ("€" ) // each '€' is 3 bytes
123130 msg := strings .Repeat ("€" , euroCount + 100 )
124- _ , err := ServiceErrorInterceptor (t .Context (), nil , nil ,
131+ _ , err := interceptor . Intercept (t .Context (), nil , nil ,
125132 func (_ context.Context , _ any ) (any , error ) {
126133 return nil , serviceerror .NewInternal (msg )
127134 })
128135 require .Error (t , err )
129136 st := status .Convert (err )
130- require .LessOrEqual (t , len (st .Message ()), maxMessageLength )
137+ require .LessOrEqual (t , len (st .Message ()), testMaxMessageLength )
131138 require .True (t , strings .HasSuffix (st .Message (), truncatedSuffix ))
132139 // Verify the truncated body (without suffix) is valid UTF-8 by checking
133140 // that no partial rune was left behind — the full message should be valid.
0 commit comments