1- // Copyright 2022 FishGoddess. All rights reserved.
1+ // Copyright 2023 FishGoddess. All rights reserved.
22// Use of this source code is governed by a MIT style
33// license that can be found in the LICENSE file.
44
@@ -9,140 +9,12 @@ import (
99 "fmt"
1010)
1111
12- const (
13- codeNil = 0
14- codeUnknown = 1
15- )
16-
17- const (
18- nilString = "<nil>"
19- )
20-
21- var (
22- // FormatError formats e and returns a string in Error().
23- FormatError = func (e * Error ) string {
24- return e .err .Error ()
25- }
26-
27- // FormatString formats e and returns a string in String().
28- FormatString = func (e * Error ) string {
29- return fmt .Sprintf ("%d (%s)" , e .code , e .msg )
30- }
31- )
32-
33- // Error wraps err with some information.
34- type Error struct {
35- err error
36-
37- code int32
38- msg string
39- }
40-
41- func (e * Error ) Error () string {
42- if e == nil || e .err == nil {
43- return nilString
44- }
45-
46- return FormatError (e )
47- }
48-
49- func (e * Error ) String () string {
50- if e == nil || e .err == nil {
51- return nilString
52- }
53-
54- return FormatString (e )
55- }
56-
57- // Is returns if e has the same type of target.
58- func (e * Error ) Is (target error ) bool {
59- if e == nil {
60- return e == target
61- }
62-
63- err , ok := target .(* Error )
64- if ! ok {
65- return e .err == target
66- }
67-
68- return e .code == err .code
69- }
70-
71- // Unwrap returns err inside.
72- func (e * Error ) Unwrap () error {
73- if e == nil {
74- return nil
75- }
76-
77- return e .err
78- }
79-
80- // Wrap wraps err with code
81- func Wrap (err error , code int32 , opts ... Option ) error {
82- if err == nil {
83- return nil
84- }
85-
86- e := & Error {
87- err : err ,
88- code : code ,
89- msg : err .Error (),
90- }
91-
92- applyOptions (e , opts )
93- return e
12+ // New returns a string error.
13+ func New (text string ) error {
14+ return errors .New (text )
9415}
9516
96- // Unwrap returns if err is Error and its code == code, and the original error will be returned, too.
97- func Unwrap (err error , code int32 ) (error , bool ) {
98- for {
99- if err == nil {
100- return nil , false
101- }
102-
103- e , ok := err .(* Error )
104- if ! ok {
105- return err , false
106- }
107-
108- if e .code == code {
109- return err , true
110- }
111-
112- err = errors .Unwrap (err )
113- }
114- }
115-
116- // Is returns if err is Error and its code == code.
117- func Is (err error , code int32 ) bool {
118- _ , ok := Unwrap (err , code )
119- return ok
120- }
121-
122- // Code returns the code of err.
123- func Code (err error ) int32 {
124- if err == nil {
125- return codeNil
126- }
127-
128- e , ok := err .(* Error )
129- if ! ok {
130- return codeUnknown
131- }
132-
133- return e .code
134- }
135-
136- // Msg returns the msg of err.
137- func Msg (err error ) string {
138- if err == nil {
139- return nilString
140- }
141-
142- e , ok := err .(* Error )
143- if ! ok {
144- return err .Error ()
145- }
146-
147- return e .msg
17+ // NewF returns a string error.
18+ func NewF (text string , params ... interface {}) error {
19+ return fmt .Errorf (text , params ... )
14820}
0 commit comments