diff --git a/backend/modules/prompt/pkg/errno/prompt_test.go b/backend/modules/prompt/pkg/errno/prompt_test.go new file mode 100644 index 000000000..0c7282213 --- /dev/null +++ b/backend/modules/prompt/pkg/errno/prompt_test.go @@ -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) + 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"]) +} diff --git a/backend/pkg/errorx/error.go b/backend/pkg/errorx/error.go index e6fd84d75..ccf60c082 100644 --- a/backend/pkg/errorx/error.go +++ b/backend/pkg/errorx/error.go @@ -17,6 +17,8 @@ import ( type StatusError interface { error Code() int32 + IsAffectStability() bool + WithAffectStability(affectStability bool) Extra() map[string]string WithExtra(map[string]string) } diff --git a/backend/pkg/errorx/internal/status.go b/backend/pkg/errorx/internal/status.go index d630ca9f8..ec4996784 100644 --- a/backend/pkg/errorx/internal/status.go +++ b/backend/pkg/errorx/internal/status.go @@ -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 }