Skip to content

Commit 6319a28

Browse files
Add NewInternalErrorWithDPanic for common dpanic pattern (#3797)
* Add NewInternalErrorWithDPanic for common dpanic pattern
1 parent ef98234 commit 6319a28

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2023 Temporal Technologies Inc. All rights reserved.
4+
//
5+
// Copyright (c) 2020 Uber Technologies, Inc.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
package serviceerror
26+
27+
import (
28+
"go.temporal.io/api/serviceerror"
29+
30+
"go.temporal.io/server/common/log"
31+
)
32+
33+
// NewInternalErrorWithDPanic is a wrapper for service error that will panic if it's in dev environment
34+
func NewInternalErrorWithDPanic(logger log.Logger, msg string) error {
35+
logger.DPanic(msg)
36+
return serviceerror.NewInternal(msg)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// The MIT License
2+
//
3+
// Copyright (c) 2023 Temporal Technologies Inc. All rights reserved.
4+
//
5+
// Copyright (c) 2020 Uber Technologies, Inc.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
package serviceerror
26+
27+
import (
28+
"testing"
29+
30+
"github.com/stretchr/testify/require"
31+
"github.com/stretchr/testify/suite"
32+
33+
"go.temporal.io/api/serviceerror"
34+
35+
"go.temporal.io/server/common/log"
36+
)
37+
38+
type serviceErrorWithDPanicSuite struct {
39+
*require.Assertions
40+
suite.Suite
41+
}
42+
43+
func TestServiceErrorWithDPanicSuite(t *testing.T) {
44+
suite.Run(t, new(serviceErrorWithDPanicSuite))
45+
}
46+
47+
func (s *serviceErrorWithDPanicSuite) SetupTest() {
48+
s.Assertions = require.New(s.T())
49+
}
50+
51+
func (s *serviceErrorWithDPanicSuite) TestNewDPanicInProd() {
52+
cfg := log.Config{
53+
Level: "info",
54+
}
55+
56+
logger := log.NewZapLogger(log.BuildZapLogger(cfg))
57+
s.NotNil(logger)
58+
59+
err := NewInternalErrorWithDPanic(logger, "Must not panic!")
60+
s.NotNil(err)
61+
_, ok := err.(*serviceerror.Internal)
62+
s.True(ok)
63+
}
64+
65+
func (s *serviceErrorWithDPanicSuite) TestNewDPanicInDev() {
66+
cfg := log.Config{
67+
Level: "info",
68+
Development: true,
69+
}
70+
71+
logger := log.NewZapLogger(log.BuildZapLogger(cfg))
72+
s.NotNil(logger)
73+
74+
s.Panics(nil, func() {
75+
err := NewInternalErrorWithDPanic(logger, "Must panic!")
76+
s.Nil(err)
77+
})
78+
}

0 commit comments

Comments
 (0)