Define error code for httplib
This commit is contained in:
91
core/berror/codes.go
Normal file
91
core/berror/codes.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2020 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package berror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// A Code is an unsigned 32-bit error code as defined in the beego spec.
|
||||
type Code interface {
|
||||
Code() uint32
|
||||
Module() string
|
||||
Desc() string
|
||||
Name() string
|
||||
}
|
||||
|
||||
var defaultCodeRegistry = &codeRegistry{
|
||||
codes: make(map[uint32]*codeDefinition, 127),
|
||||
}
|
||||
|
||||
// DefineCode defining a new Code
|
||||
// Before defining a new code, please read Beego specification.
|
||||
// desc could be markdown doc
|
||||
func DefineCode(code uint32, module string, name string, desc string) Code {
|
||||
res := &codeDefinition{
|
||||
code: code,
|
||||
module: module,
|
||||
desc: desc,
|
||||
}
|
||||
defaultCodeRegistry.lock.Lock()
|
||||
defer defaultCodeRegistry.lock.Unlock()
|
||||
|
||||
if _, ok := defaultCodeRegistry.codes[code]; ok {
|
||||
panic(fmt.Sprintf("duplicate code, code %d has been registered", code))
|
||||
}
|
||||
defaultCodeRegistry.codes[code] = res
|
||||
return res
|
||||
}
|
||||
|
||||
type codeRegistry struct {
|
||||
lock sync.RWMutex
|
||||
codes map[uint32]*codeDefinition
|
||||
}
|
||||
|
||||
|
||||
func (cr *codeRegistry) Get(code uint32) (Code, bool) {
|
||||
cr.lock.RLock()
|
||||
defer cr.lock.RUnlock()
|
||||
c, ok := cr.codes[code]
|
||||
return c, ok
|
||||
}
|
||||
|
||||
type codeDefinition struct {
|
||||
code uint32
|
||||
module string
|
||||
desc string
|
||||
name string
|
||||
}
|
||||
|
||||
|
||||
func (c *codeDefinition) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c *codeDefinition) Code() uint32 {
|
||||
return c.code
|
||||
}
|
||||
|
||||
func (c *codeDefinition) Module() string {
|
||||
return c.module
|
||||
}
|
||||
|
||||
func (c *codeDefinition) Desc() string {
|
||||
return c.desc
|
||||
}
|
||||
|
||||
69
core/berror/error.go
Normal file
69
core/berror/error.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2020 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package berror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// code, msg
|
||||
const errFmt = "ERROR-%d, %s"
|
||||
|
||||
// Err returns an error representing c and msg. If c is OK, returns nil.
|
||||
func Error(c Code, msg string) error {
|
||||
return fmt.Errorf(errFmt, c.Code(), msg)
|
||||
}
|
||||
|
||||
// Errorf returns error
|
||||
func Errorf(c Code, format string, a ...interface{}) error {
|
||||
return Error(c, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
func Wrap(err error, c Code, msg string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf(errFmt, c.Code(), msg))
|
||||
}
|
||||
|
||||
func Wrapf(err error, c Code, format string, a ...interface{}) error {
|
||||
return Wrap(err, c, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// FromError is very simple. It just parse error msg and check whether code has been register
|
||||
// if code not being register, return unknown
|
||||
// if err.Error() is not valid beego error code, return unknown
|
||||
func FromError(err error) (Code, bool) {
|
||||
msg := err.Error()
|
||||
codeSeg := strings.SplitN(msg, ",", 2)
|
||||
if strings.HasPrefix(codeSeg[0], "ERROR-") {
|
||||
codeStr := strings.SplitN(codeSeg[0], "-", 2)
|
||||
if len(codeStr) < 2 {
|
||||
return Unknown, false
|
||||
}
|
||||
codeInt, e := strconv.ParseUint(codeStr[1], 10, 32)
|
||||
if e != nil {
|
||||
return Unknown, false
|
||||
}
|
||||
if code, ok := defaultCodeRegistry.Get(uint32(codeInt)); ok {
|
||||
return code, true
|
||||
}
|
||||
}
|
||||
return Unknown, false
|
||||
}
|
||||
77
core/berror/error_test.go
Normal file
77
core/berror/error_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2020 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package berror
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var testCode1 = DefineCode(1, "unit_test", "TestError", "Hello, test code1")
|
||||
|
||||
var testErr = errors.New("hello, this is error")
|
||||
|
||||
func TestErrorf(t *testing.T) {
|
||||
msg := Errorf(testCode1, "errorf %s", "aaaa")
|
||||
assert.NotNil(t, msg)
|
||||
assert.Equal(t, "ERROR-1, errorf aaaa", msg.Error())
|
||||
}
|
||||
|
||||
func TestWrapf(t *testing.T) {
|
||||
err := Wrapf(testErr, testCode1, "Wrapf %s", "aaaa")
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, errors.Is(err, testErr))
|
||||
}
|
||||
|
||||
func TestFromError(t *testing.T) {
|
||||
err := errors.New("ERROR-1, errorf aaaa")
|
||||
code, ok := FromError(err)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, testCode1, code)
|
||||
assert.Equal(t, "unit_test", code.Module())
|
||||
assert.Equal(t, "Hello, test code1", code.Desc())
|
||||
|
||||
err = errors.New("not beego error")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
|
||||
err = errors.New("ERROR-2, not register")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
|
||||
err = errors.New("ERROR-aaa, invalid code")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
|
||||
err = errors.New("aaaaaaaaaaaaaa")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
|
||||
err = errors.New("ERROR-2-3, invalid error")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
|
||||
err = errors.New("ERROR, invalid error")
|
||||
code, ok = FromError(err)
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, Unknown, code)
|
||||
}
|
||||
52
core/berror/pre_define_code.go
Normal file
52
core/berror/pre_define_code.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021 beego
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package berror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// pre define code
|
||||
|
||||
// Unknown indicates got some error which is not defined
|
||||
var Unknown = DefineCode(5000001, "error", "Unknown",fmt.Sprintf(`
|
||||
Unknown error code. Usually you will see this code in three cases:
|
||||
1. You forget to define Code or function DefineCode not being executed;
|
||||
2. This is not Beego's error but you call FromError();
|
||||
3. Beego got unexpected error and don't know how to handle it, and then return Unknown error
|
||||
|
||||
A common practice to DefineCode looks like:
|
||||
%s
|
||||
|
||||
In this way, you may forget to import this package, and got Unknown error.
|
||||
|
||||
Sometimes, you believe you got Beego error, but actually you don't, and then you call FromError(err)
|
||||
|
||||
`, goCodeBlock(`
|
||||
import your_package
|
||||
|
||||
func init() {
|
||||
DefineCode(5100100, "your_module", "detail")
|
||||
// ...
|
||||
}
|
||||
`)))
|
||||
|
||||
func goCodeBlock(code string) string {
|
||||
return codeBlock("go", code)
|
||||
}
|
||||
func codeBlock(lan string, code string) string {
|
||||
return fmt.Sprintf("```%s\n%s\n```", lan, code)
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package error
|
||||
|
||||
// A Code is an unsigned 32-bit error code as defined in the beego spec.
|
||||
type Code uint32
|
||||
|
||||
const (
|
||||
// SessionSessionStartError means func SessionStart error in session module.
|
||||
SessionSessionStartError Code = 5001001
|
||||
)
|
||||
|
||||
// CodeToStr is a map about Code and Code's message
|
||||
var CodeToStr = map[Code]string{
|
||||
SessionSessionStartError: `"SESSION_MODULE_SESSION_START_ERROR"`,
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Error type defines custom error for Beego. It is used by every module
|
||||
// in Beego. Each `Error` message contains three pieces of data: error code,
|
||||
// error message.
|
||||
// More docs http://beego.me/docs/module/error.md.
|
||||
type Error struct {
|
||||
Code Code
|
||||
Msg string
|
||||
}
|
||||
|
||||
// New returns a Error representing c and msg.
|
||||
func New(c Code, msg string) *Error {
|
||||
return &Error{Code: c, Msg: msg}
|
||||
}
|
||||
|
||||
// Err returns an error representing c and msg. If c is OK, returns nil.
|
||||
func Err(c Code, msg string) error {
|
||||
return New(c, msg)
|
||||
}
|
||||
|
||||
// Errorf returns Error(c, fmt.Sprintf(format, a...)).
|
||||
func Errorf(c Code, format string, a ...interface{}) error {
|
||||
return Err(c, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Error returns formatted message for user.
|
||||
func (e *Error) Error() string {
|
||||
codeSrt := strconv.FormatUint(uint64(e.GetCode()), 10)
|
||||
return fmt.Sprintf("beego error: code = %s desc = %s", codeSrt, e.GetMessage())
|
||||
}
|
||||
|
||||
// GetCode returns Error's Code.
|
||||
func (e *Error) GetCode() Code {
|
||||
if e != nil {
|
||||
return e.Code
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetMessage returns Error's Msg.
|
||||
func (e *Error) GetMessage() string {
|
||||
if e != nil {
|
||||
return e.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErr(t *testing.T) {
|
||||
type args struct {
|
||||
c Code
|
||||
msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", args: args{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := Err(tt.args.c, tt.args.msg); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Err() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_Error(t *testing.T) {
|
||||
type fields struct {
|
||||
Code Code
|
||||
Msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: "beego error: code = 5001001 desc = \"SESSION_MODULE_SESSION_START_ERROR\""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &Error{
|
||||
Code: tt.fields.Code,
|
||||
Msg: tt.fields.Msg,
|
||||
}
|
||||
if got := e.Error(); got != tt.want {
|
||||
t.Errorf("Error() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_GetCode(t *testing.T) {
|
||||
type fields struct {
|
||||
Code Code
|
||||
Msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want Code
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: SessionSessionStartError},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &Error{
|
||||
Code: tt.fields.Code,
|
||||
Msg: tt.fields.Msg,
|
||||
}
|
||||
if got := e.GetCode(); got != tt.want {
|
||||
t.Errorf("GetCode() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_GetMessage(t *testing.T) {
|
||||
type fields struct {
|
||||
Code Code
|
||||
Msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: CodeToStr[SessionSessionStartError]},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
e := &Error{
|
||||
Code: tt.fields.Code,
|
||||
Msg: tt.fields.Msg,
|
||||
}
|
||||
if got := e.GetMessage(); got != tt.want {
|
||||
t.Errorf("GetMessage() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorf(t *testing.T) {
|
||||
type args struct {
|
||||
c Code
|
||||
format string
|
||||
a []interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", args: args{SessionSessionStartError, "%s", []interface{}{CodeToStr[SessionSessionStartError]}}, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := Errorf(tt.args.c, tt.args.format, tt.args.a...); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Errorf() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
type args struct {
|
||||
c Code
|
||||
msg string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *Error
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{name: "1", args: args{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: &Error{Code: SessionSessionStartError, Msg: CodeToStr[SessionSessionStartError]}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := New(tt.args.c, tt.args.msg); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("New() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user