Move codes to error package

This commit is contained in:
Ming Deng 2021-01-22 16:15:01 +08:00
parent 05f8058be1
commit a91a5d01d1
5 changed files with 39 additions and 23 deletions

1
ERROR_SPECIFICATION.md Normal file
View File

@ -0,0 +1 @@
# Error Module

17
client/httplib/error.go Normal file
View File

@ -0,0 +1,17 @@
// 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 httplib

View File

@ -1,4 +1,4 @@
package codes
package error
// A Code is an unsigned 32-bit error code as defined in the beego spec.
type Code uint32
@ -10,5 +10,5 @@ const (
// CodeToStr is a map about Code and Code's message
var CodeToStr = map[Code]string{
SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`,
SessionSessionStartError: `"SESSION_MODULE_SESSION_START_ERROR"`,
}

View File

@ -2,7 +2,6 @@ package error
import (
"fmt"
"github.com/beego/beego/v2/core/codes"
"strconv"
)
@ -11,22 +10,22 @@ import (
// error message.
// More docs http://beego.me/docs/module/error.md.
type Error struct {
Code codes.Code
Msg string
Code Code
Msg string
}
// New returns a Error representing c and msg.
func New(c codes.Code, msg string) *Error {
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 codes.Code, msg string) error {
func Err(c Code, msg string) error {
return New(c, msg)
}
// Errorf returns Error(c, fmt.Sprintf(format, a...)).
func Errorf(c codes.Code, format string, a ...interface{}) error {
func Errorf(c Code, format string, a ...interface{}) error {
return Err(c, fmt.Sprintf(format, a...))
}
@ -37,7 +36,7 @@ func (e *Error) Error() string {
}
// GetCode returns Error's Code.
func (e *Error) GetCode() codes.Code {
func (e *Error) GetCode() Code {
if e != nil {
return e.Code
}

View File

@ -1,14 +1,13 @@
package error
import (
"github.com/beego/beego/v2/core/codes"
"reflect"
"testing"
)
func TestErr(t *testing.T) {
type args struct {
c codes.Code
c Code
msg string
}
tests := []struct {
@ -17,7 +16,7 @@ func TestErr(t *testing.T) {
wantErr bool
}{
// TODO: Add test cases.
{name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, wantErr: true},
{name: "1", args: args{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -30,7 +29,7 @@ func TestErr(t *testing.T) {
func TestError_Error(t *testing.T) {
type fields struct {
Code codes.Code
Code Code
Msg string
}
tests := []struct {
@ -39,7 +38,7 @@ func TestError_Error(t *testing.T) {
want string
}{
// TODO: Add test cases.
{name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: "beego error: code = 5001001 desc = \"SESSION_MODULE_SESSION_START_ERROR\""},
{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) {
@ -56,16 +55,16 @@ func TestError_Error(t *testing.T) {
func TestError_GetCode(t *testing.T) {
type fields struct {
Code codes.Code
Code Code
Msg string
}
tests := []struct {
name string
fields fields
want codes.Code
want Code
}{
// TODO: Add test cases.
{name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.SessionSessionStartError},
{name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: SessionSessionStartError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -82,7 +81,7 @@ func TestError_GetCode(t *testing.T) {
func TestError_GetMessage(t *testing.T) {
type fields struct {
Code codes.Code
Code Code
Msg string
}
tests := []struct {
@ -91,7 +90,7 @@ func TestError_GetMessage(t *testing.T) {
want string
}{
// TODO: Add test cases.
{name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.CodeToStr[codes.SessionSessionStartError]},
{name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: CodeToStr[SessionSessionStartError]},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -108,7 +107,7 @@ func TestError_GetMessage(t *testing.T) {
func TestErrorf(t *testing.T) {
type args struct {
c codes.Code
c Code
format string
a []interface{}
}
@ -118,7 +117,7 @@ func TestErrorf(t *testing.T) {
wantErr bool
}{
// TODO: Add test cases.
{name: "1", args: args{codes.SessionSessionStartError, "%s", []interface{}{codes.CodeToStr[codes.SessionSessionStartError]}}, wantErr: true},
{name: "1", args: args{SessionSessionStartError, "%s", []interface{}{CodeToStr[SessionSessionStartError]}}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -131,7 +130,7 @@ func TestErrorf(t *testing.T) {
func TestNew(t *testing.T) {
type args struct {
c codes.Code
c Code
msg string
}
tests := []struct {
@ -140,7 +139,7 @@ func TestNew(t *testing.T) {
want *Error
}{
// TODO: Add test cases.
{name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: &Error{Code:codes.SessionSessionStartError, Msg:codes.CodeToStr[codes.SessionSessionStartError]}},
{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) {