Move codes to error package
This commit is contained in:
parent
05f8058be1
commit
a91a5d01d1
1
ERROR_SPECIFICATION.md
Normal file
1
ERROR_SPECIFICATION.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Error Module
|
||||||
17
client/httplib/error.go
Normal file
17
client/httplib/error.go
Normal 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
|
||||||
|
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package codes
|
package error
|
||||||
|
|
||||||
// A Code is an unsigned 32-bit error code as defined in the beego spec.
|
// A Code is an unsigned 32-bit error code as defined in the beego spec.
|
||||||
type Code uint32
|
type Code uint32
|
||||||
@ -10,5 +10,5 @@ const (
|
|||||||
|
|
||||||
// CodeToStr is a map about Code and Code's message
|
// CodeToStr is a map about Code and Code's message
|
||||||
var CodeToStr = map[Code]string{
|
var CodeToStr = map[Code]string{
|
||||||
SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`,
|
SessionSessionStartError: `"SESSION_MODULE_SESSION_START_ERROR"`,
|
||||||
}
|
}
|
||||||
@ -2,7 +2,6 @@ package error
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/beego/beego/v2/core/codes"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,22 +10,22 @@ import (
|
|||||||
// error message.
|
// error message.
|
||||||
// More docs http://beego.me/docs/module/error.md.
|
// More docs http://beego.me/docs/module/error.md.
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Code codes.Code
|
Code Code
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a Error representing c and msg.
|
// 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}
|
return &Error{Code: c, Msg: msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err returns an error representing c and msg. If c is OK, returns nil.
|
// 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)
|
return New(c, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf returns Error(c, fmt.Sprintf(format, a...)).
|
// 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...))
|
return Err(c, fmt.Sprintf(format, a...))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +36,7 @@ func (e *Error) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetCode returns Error's Code.
|
// GetCode returns Error's Code.
|
||||||
func (e *Error) GetCode() codes.Code {
|
func (e *Error) GetCode() Code {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return e.Code
|
return e.Code
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,13 @@
|
|||||||
package error
|
package error
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/beego/beego/v2/core/codes"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestErr(t *testing.T) {
|
func TestErr(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
c codes.Code
|
c Code
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -17,7 +16,7 @@ func TestErr(t *testing.T) {
|
|||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -30,7 +29,7 @@ func TestErr(t *testing.T) {
|
|||||||
|
|
||||||
func TestError_Error(t *testing.T) {
|
func TestError_Error(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
Code codes.Code
|
Code Code
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -39,7 +38,7 @@ func TestError_Error(t *testing.T) {
|
|||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -56,16 +55,16 @@ func TestError_Error(t *testing.T) {
|
|||||||
|
|
||||||
func TestError_GetCode(t *testing.T) {
|
func TestError_GetCode(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
Code codes.Code
|
Code Code
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
fields fields
|
fields fields
|
||||||
want codes.Code
|
want Code
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -82,7 +81,7 @@ func TestError_GetCode(t *testing.T) {
|
|||||||
|
|
||||||
func TestError_GetMessage(t *testing.T) {
|
func TestError_GetMessage(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
Code codes.Code
|
Code Code
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -91,7 +90,7 @@ func TestError_GetMessage(t *testing.T) {
|
|||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -108,7 +107,7 @@ func TestError_GetMessage(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrorf(t *testing.T) {
|
func TestErrorf(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
c codes.Code
|
c Code
|
||||||
format string
|
format string
|
||||||
a []interface{}
|
a []interface{}
|
||||||
}
|
}
|
||||||
@ -118,7 +117,7 @@ func TestErrorf(t *testing.T) {
|
|||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@ -131,7 +130,7 @@ func TestErrorf(t *testing.T) {
|
|||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
c codes.Code
|
c Code
|
||||||
msg string
|
msg string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -140,7 +139,7 @@ func TestNew(t *testing.T) {
|
|||||||
want *Error
|
want *Error
|
||||||
}{
|
}{
|
||||||
// TODO: Add test cases.
|
// 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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user