-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre_define_code.go
52 lines (41 loc) · 1.53 KB
/
pre_define_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)
}