forked from kataras/iris
/
error.go
72 lines (59 loc) · 1.61 KB
/
error.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package errors
import (
"fmt"
"runtime"
"github.com/kataras/iris/logger"
)
// Error holds the error
type Error struct {
message string
}
// Error returns the message of the actual error
func (e *Error) Error() string {
return e.message
}
// Format returns a formatted new error based on the arguments
func (e *Error) Format(args ...interface{}) error {
return fmt.Errorf(e.message, args...)
}
// With does the same thing as Format but it receives an error type which if it's nil it returns a nil error
func (e *Error) With(err error) error {
if err == nil {
return nil
}
return e.Format(err.Error())
}
// Return returns the actual error as it is
func (e *Error) Return() error {
return e.Format()
}
// Panic output the message and after panics
func (e *Error) Panic() {
if e == nil {
return
}
_, fn, line, _ := runtime.Caller(1)
errMsg := e.message
errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
panic(errMsg)
}
// Panicf output the formatted message and after panics
func (e *Error) Panicf(args ...interface{}) {
if e == nil {
return
}
_, fn, line, _ := runtime.Caller(1)
errMsg := e.Format(args...).Error()
errMsg = "\nCaller was: " + fmt.Sprintf("%s:%d", fn, line)
panic(errMsg)
}
//
// New creates and returns an Error with a message
func New(errMsg string) *Error {
// return &Error{fmt.Errorf("\n" + logger.Prefix + "Error: " + errMsg)}
return &Error{message: "\n" + logger.Prefix + " Error: " + errMsg}
}
// Printf prints to the logger a specific error with optionally arguments
func Printf(logger *logger.Logger, err error, args ...interface{}) {
logger.Printf(err.Error(), args...)
}