Skip to content

Commit

Permalink
feat: 添加 SprintError
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 20, 2024
1 parent 1108110 commit e6d1091
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
36 changes: 36 additions & 0 deletions internal/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,39 @@

// Package errs 与错误相关的定义
package errs

import (
"github.com/issue9/localeutil"
"github.com/issue9/logs/v7"
"golang.org/x/xerrors"
)

func Sprint(p *localeutil.Printer, err error, detail bool) string {
if err == nil || p == nil {
panic("参数 p 和 err 不能为 nil")
}

switch t := err.(type) {
case xerrors.Formatter:
b := logs.NewBuffer(detail)
e := t.FormatError(b)
FOR:
for e != nil {
switch tt := e.(type) {
case xerrors.Formatter:
e = tt.FormatError(b)
case localeutil.Stringer:
b.AppendString(tt.LocaleString(p))
break FOR
default:
b.AppendString(tt.Error())
break FOR
}
}
return string(b.Bytes())
case localeutil.Stringer:
return t.LocaleString(p)
default:
return err.Error()
}
}
38 changes: 38 additions & 0 deletions internal/errs/errs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

package errs

import (
"errors"
"testing"

"github.com/issue9/assert/v4"
"github.com/issue9/localeutil"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"
)

func TestSprint(t *testing.T) {
a := assert.New(t, false)

a.Panic(func() {
Sprint(nil, nil, false)
})

c := catalog.NewBuilder(catalog.Fallback(language.SimplifiedChinese))
a.NotError(c.SetString(language.TraditionalChinese, "lang", "tw"))
a.NotError(c.SetString(language.SimplifiedChinese, "lang", "cn"))
p := message.NewPrinter(language.SimplifiedChinese, message.Catalog(c))

a.Equal(Sprint(p, errors.New("lang"), false), "lang")
a.Equal(Sprint(p, localeutil.Error("lang"), false), "cn")

a.Contains(Sprint(p, NewDepthStackError(2, errors.New("lang")), true), "lang")
a.Contains(Sprint(p, NewDepthStackError(2, localeutil.Error("lang")), false), "cn")

a.Contains(Sprint(p, NewDepthStackError(2, NewDepthStackError(2, errors.New("lang"))), false), "lang")
a.Contains(Sprint(p, NewDepthStackError(2, NewDepthStackError(2, localeutil.Error("lang"))), true), "cn")
}
10 changes: 9 additions & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.92.1"
const Version = "0.93.0"

type (
Logger = logs.Logger
Expand Down Expand Up @@ -96,3 +96,11 @@ func NewLocaleError(format string, v ...any) error { return localeutil.Error(for
//
// 此方法返回的错误,在 [Context.Error] 和 [Recovery] 中会被识别且按指定的状态码输出。
func NewError(status int, err error) error { return errs.NewError(status, err) }

// SprintError 将 err 转换为字符串
//
// p 如果 err 实现了 [LocaleStringer],将采用 p 进行转换;
// detail 是否输出调用堆栈;
func SprintError(p *localeutil.Printer, detail bool, err error) string {
return errs.Sprint(p, err, detail)
}

0 comments on commit e6d1091

Please sign in to comment.