-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorswithstack.go
51 lines (43 loc) · 1.41 KB
/
errorswithstack.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
// Copyright (c) 2020 Playscale PTE LTD. All rights reserved.
// Licensed under the MIT license. See LICENSE file included with
// this project for details.
package errors
import (
goerrors "errors"
"runtime/debug"
"sync"
)
var useStackTracesMutex = sync.RWMutex{}
var useStackTraces = true
// New will create a new, regular, go Error with your message and a
// stack trace appended. New(msg + "\nStack Trace:\n" + stackTrace)
func New(msg string) error {
useStackTracesMutex.RLock()
shouldPrint := useStackTraces
useStackTracesMutex.RUnlock()
if !shouldPrint {
return goerrors.New(msg)
}
stackTrace := string(debug.Stack())
result := goerrors.New(msg + "\nStack Trace:\n" + stackTrace)
return result
}
// EnableStackTraces does what it says on the tin, and does so in a
// thread-safe manner. Any calls to errors.New(message) after
// EnableStackTraces() will append a formatted stack trace to the error
// message.
func EnableStackTraces() {
useStackTracesMutex.Lock()
defer useStackTracesMutex.Unlock()
useStackTraces = true
}
// DisableStackTraces does just that. It is safe to call this from
// multiple threads. Any calls to errors.New(message) after
// DisableStackTraces() will simply return a plain old Go standard
// library call to errors.New() as if this package were not even in
// place.
func DisableStackTraces() {
useStackTracesMutex.Lock()
defer useStackTracesMutex.Unlock()
useStackTraces = false
}