Skip to content

Commit

Permalink
support dubbo3 unary error compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Dec 8, 2023
1 parent 056487a commit 75ae4b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions protocol/triple/triple_protocol/handler_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ package triple_protocol

import (
"context"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"net/http"
)

import (
"github.com/dubbogo/grpc-go"
"github.com/dubbogo/grpc-go/status"
)

import (
Expand Down Expand Up @@ -66,6 +69,10 @@ func (t *tripleCompatInterceptor) compatUnaryServerInterceptor(ctx context.Conte
if !ok {
panic(fmt.Sprintf("%+v is not of type *RPCResult", respRaw))
}
triErr, ok := compatError(err)
if ok {
err = triErr
}
// todo(DMwangnima): expose API for users to write response headers and trailers
return NewResponse(resp.Rest), err
}
Expand Down Expand Up @@ -133,3 +140,22 @@ func generateCompatUnaryHandlerFunc(
return conn.Send(resp.Any())
}
}

func compatError(err error) (*Error, bool) {
s, ok := status.FromError(err)
if !ok {
return nil, ok
}

triErr := NewError(Code(s.Code()), errors.New(s.Message()))
for _, detail := range s.Details() {
// dubbo3 detail use MessageV1, we need to convert it to MessageV2
errDetail, e := NewErrorDetail(proto.MessageV2(detail.(proto.Message)))
if e != nil {
return nil, false
}
triErr.AddDetail(errDetail)
}

return triErr, ok
}
34 changes: 34 additions & 0 deletions protocol/triple/triple_protocol/handler_compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package triple_protocol

import (
"github.com/dubbogo/grpc-go/codes"
"github.com/dubbogo/grpc-go/status"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCompatError(t *testing.T) {
err := status.Error(codes.Code(1234), "user defined")
triErr, ok := compatError(err)
assert.True(t, ok)
assert.Equal(t, Code(1234), triErr.Code())
assert.Equal(t, "user defined", triErr.Message())
assert.Equal(t, 1, len(triErr.Details()))
}

0 comments on commit 75ae4b2

Please sign in to comment.