Skip to content

Commit

Permalink
refactor: 错误信息本地化
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 27, 2023
1 parent fe561bb commit d172fa9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module github.com/issue9/scheduled

require github.com/issue9/assert/v3 v3.1.0
require (
github.com/issue9/assert/v3 v3.1.0
github.com/issue9/localeutil v0.25.0
)

go 1.17
require golang.org/x/text v0.14.0 // indirect

go 1.18
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/issue9/assert/v3 v3.1.0 h1:oxLFXS7QnBKI4lB31pRoYO96yErkWAJtR7iv+LNjAPg=
github.com/issue9/assert/v3 v3.1.0/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM=
github.com/issue9/localeutil v0.25.0 h1:RJPsbfnRt1FauXjAgkRdKtdXiM+FqdfUT0J/axGJIVU=
github.com/issue9/localeutil v0.25.0/go.mod h1:JvTb8B/2oVEZU1VHrBJXPHlE/1gZJFRMcF/ziKD8JJY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8 changes: 5 additions & 3 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
"time"

"github.com/issue9/localeutil"

"github.com/issue9/scheduled/schedulers/at"
"github.com/issue9/scheduled/schedulers/cron"
"github.com/issue9/scheduled/schedulers/ticker"
Expand Down Expand Up @@ -81,7 +83,7 @@ func (j *Job) calcState(now time.Time) {

// 运行当前的任务
func (j *Job) run(at time.Time, errlog, infolog Logger) {
infolog.Printf("scheduled: start job %s at %s\n", j.Name(), at.String())
infolog.LocaleString(localeutil.Phrase("scheduled: start job %s at %s", j.Name(), at.String()))

j.locker.Lock()
defer j.locker.Unlock()
Expand All @@ -94,13 +96,13 @@ func (j *Job) run(at time.Time, errlog, infolog Logger) {
j.err = fmt.Errorf("%v", msg)
}
j.state = Failed
errlog.Print(j.err)
errlog.Error(j.err)
}
}()

if j.err = j.f(at); j.err != nil {
j.state = Failed
errlog.Print(j.err)
errlog.Error(j.err)
} else {
j.state = Stopped
}
Expand Down
10 changes: 8 additions & 2 deletions locales/und.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
id: und
messages:
- key: 'scheduled: start job %s at %s\n'
- key: invalid state %d
message:
msg: 'scheduled: start job %s at %s\n'
msg: invalid state %d
- key: invalid state text %s
message:
msg: invalid state text %s
- key: 'scheduled: start job %s at %s'
message:
msg: 'scheduled: start job %s at %s'
10 changes: 8 additions & 2 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
id: und
messages:
- key: 'scheduled: start job %s at %s\n'
- key: invalid state %d
message:
msg: '在 %[2]s 运行计划任务 %[1]s\n'
msg: 无效的状态 %d
- key: invalid state text %s
message:
msg: 无效的状态字符串 %s
- key: 'scheduled: start job %s at %s'
message:
msg: 在 %[2]s 运行计划任务 %[1]s
31 changes: 16 additions & 15 deletions scheduled.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT

//go:generate web locale -l=und -func=github.com/issue9/scheduled.Logger.Printf -m -f=yaml ./
//go:generate web locale -l=und -func=github.com/issue9/localeutil.Phrase,github.com/issue9/localeutil.Error -m -f=yaml ./
//go:generate web update-locale -src=./locales/und.yaml -dest=./locales/zh-CN.yaml

// Package scheduled 个计划任务管理工具
Expand All @@ -15,7 +15,7 @@
package scheduled

import (
"fmt"
"github.com/issue9/localeutil"

"github.com/issue9/scheduled/schedulers"
)
Expand All @@ -32,9 +32,14 @@ type (
SchedulerFunc = schedulers.SchedulerFunc

// Logger 日志接口
//
// NOTE: 同时实现了 [github.com/issue9/logs.Logger] 对象
Logger interface {
Print(...interface{})
Printf(format string, v ...interface{})
// Error 输出错误对象到日志
Error(error)

// LocaleString 输出本地化的内容到日志
LocaleString(localeutil.Stringer)
}

State int8
Expand All @@ -58,9 +63,7 @@ var (

func (l *defaultLogger) Error(error) {}

func (l *defaultLogger) Print(v ...interface{}) {}

func (l *defaultLogger) Printf(format string, v ...interface{}) {}
func (l *defaultLogger) LocaleString(localeutil.Stringer) {}

func (s State) String() string {
v, found := stateStringMap[s]
Expand All @@ -71,18 +74,16 @@ func (s State) String() string {
}

func (s State) MarshalText() ([]byte, error) {
v, found := stateStringMap[s]
if found {
if v, found := stateStringMap[s]; found {
return []byte(v), nil
}
return nil, fmt.Errorf("无效的值 %v", s)
return nil, localeutil.Error("invalid state %d", s)
}

func (s *State) UnmarshalText(data []byte) error {
v, found := stringStateMap[string(data)]
if !found {
return fmt.Errorf("无效的值 %v", string(data))
if v, found := stringStateMap[string(data)]; found {
*s = v
return nil
}
*s = v
return nil
return localeutil.Error("invalid state text %s", string(data))
}
5 changes: 3 additions & 2 deletions scheduled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/localeutil"
)

var (
Expand Down Expand Up @@ -36,8 +37,8 @@ func TestMarshal(t *testing.T) {
a.Equal(s.String(), "<unknown>")

text, err := s.MarshalText()
a.Nil(text).ErrorString(err, "无效的值")
a.Nil(text).Equal(err, localeutil.Error("invalid value"))

err = s.UnmarshalText([]byte("not-exists"))
a.ErrorString(err, "无效的值")
a.Equal(err, localeutil.Error("invalid value"))
}

0 comments on commit d172fa9

Please sign in to comment.