Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions backend/modules/prompt/pkg/errno/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2025 coze-dev Authors
// SPDX-License-Identifier: Apache-2.0

package errno

import (
"testing"

"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/stretchr/testify/assert"

"github.com/coze-dev/coze-loop/backend/pkg/errorx"
)

func TestWithExtra(t *testing.T) {
err := errorx.NewByCode(CommonInternalErrorCode)

statusErr, ok := errorx.FromStatusError(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是不是能在NewByCode的时候通过Option实现,不然New出来还要这么写,有点繁琐

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有加到 new 方法中的原因是这样的:认为影响稳定性是在定义 code 时已经确定的属性,不应该更改;之前智伟你提到的 case 应该是下游抛出来的错误期望设置为不影响稳定性?

assert.True(t, ok)

statusErr.WithAffectStability(false)
bizErr, ok := kerrors.FromBizStatusError(statusErr)
assert.True(t, ok)
assert.Equal(t, "0", bizErr.BizExtra()["biz_err_affect_stability"])

statusErr.WithAffectStability(true)
bizErr, ok = kerrors.FromBizStatusError(statusErr)
assert.True(t, ok)
assert.Equal(t, "1", bizErr.BizExtra()["biz_err_affect_stability"])
}
2 changes: 2 additions & 0 deletions backend/pkg/errorx/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
type StatusError interface {
error
Code() int32
IsAffectStability() bool
WithAffectStability(affectStability bool)
Extra() map[string]string
WithExtra(map[string]string)
}
Expand Down
8 changes: 8 additions & 0 deletions backend/pkg/errorx/internal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func (w *statusError) Error() string {
return fmt.Sprintf("code=%d message=%s", w.statusCode, w.message)
}

func (w *statusError) IsAffectStability() bool {
return w.ext.IsAffectStability
}

func (w *statusError) WithAffectStability(affectStability bool) {
w.ext.IsAffectStability = affectStability
}

func (w *statusError) Extra() map[string]string {
return w.ext.Extra
}
Expand Down
Loading