-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
64 lines (57 loc) · 1.53 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
package utils
import (
"fmt"
"log"
"os"
"runtime"
)
func ExitFatal(format string, args ...interface{}) {
errorMessage := fmt.Sprintf(format, args...)
_, file, line, ok := runtime.Caller(1)
var lineInfo string
if ok {
lineInfo = fmt.Sprintf("%s:%d", file, line)
} else {
lineInfo = "unknown filename and line num"
}
log.Fatalf("fatal error %s: %s", lineInfo, errorMessage)
}
func MustGetEnv(key string) string {
var temp = os.Getenv(key)
if temp == "" {
ExitFatal("no value found in environment for key: %s", key)
}
return temp
}
// FileWithLineNum returns the file name and line number of the function that it was called by.
// It can be used to determine the approximate location of an error
func FileWithLineNum() string {
_, file, line, ok := runtime.Caller(1)
if ok {
return fmt.Sprintf("%s:%d", file, line)
} else {
return "could not find filename and line num"
}
}
func ErrorfWithLineNum(format string, a ...any) error {
// cant call FileWithLineNum as this function will be the caller
_, file, line, ok := runtime.Caller(1)
var lineNum string
if ok {
lineNum = fmt.Sprintf("%s:%d", file, line)
} else {
lineNum = "could not find filename and line num"
}
errMsg := fmt.Sprintf(format, a...)
return fmt.Errorf("%s - %s", lineNum, errMsg)
}
func WrapErrorWithLineNum(err error) error {
_, file, line, ok := runtime.Caller(1)
var lineNum string
if ok {
lineNum = fmt.Sprintf("%s:%d", file, line)
} else {
lineNum = "could not find filename and line num"
}
return fmt.Errorf("%s - %v", lineNum, err)
}