Skip to content

Commit

Permalink
Handle panic during conversion to connectCode (#951)
Browse files Browse the repository at this point in the history
If an unhashable error is given, the error cannot be converted to a
ConnectError with a map, causing a panic and shutting down the server.

---------

Co-authored-by: Youngteac Hong <[email protected]>
  • Loading branch information
2 people authored and raararaara committed Oct 7, 2024
1 parent 49bc08c commit 8ea4fa2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/rpc/connecthelper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ func errorToConnectError(err error) (*connect.Error, bool) {
cause = errors.Unwrap(cause)
}

connectCode, ok := errorToConnectCode[cause]
// NOTE(hackerwins): This prevents panic when the cause is an unhashable
// error.
var connectCode connect.Code
var ok bool
defer func() {
if r := recover(); r != nil {
ok = false
}
}()

connectCode, ok = errorToConnectCode[cause]
if !ok {
return nil, false
}
Expand Down
39 changes: 39 additions & 0 deletions server/rpc/connecthelper/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 The Yorkie Authors. All rights reserved.
*
* Licensed 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 connecthelper

import (
"testing"

"connectrpc.com/connect"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo"

"github.com/yorkie-team/yorkie/api/converter"
)

func TestStatus(t *testing.T) {
t.Run("errorToConnectCode test", func(t *testing.T) {
status, ok := errorToConnectError(converter.ErrPackRequired)
assert.True(t, ok)
assert.Equal(t, status.Code(), connect.CodeInvalidArgument)

status, ok = errorToConnectError(mongo.CommandError{})
assert.False(t, ok)
assert.Nil(t, status)
})
}

0 comments on commit 8ea4fa2

Please sign in to comment.