Implemented error messages

This commit is contained in:
Eduard Urbach 2024-06-13 12:13:32 +02:00
parent 423526e567
commit e3b26c79f4
Signed by: eduard
GPG key ID: 49226B848C78F6C8
16 changed files with 362 additions and 60 deletions

30
src/errors/Stack.go Normal file
View file

@ -0,0 +1,30 @@
package errors
import (
"runtime"
"strings"
)
// Stack generates a stack trace.
func Stack() string {
var (
final []string
buffer = make([]byte, 4096)
n = runtime.Stack(buffer, false)
stack = string(buffer[:n])
lines = strings.Split(stack, "\n")
)
for i := 6; i < len(lines); i += 2 {
line := strings.TrimSpace(lines[i])
space := strings.LastIndex(line, " ")
if space != -1 {
line = line[:space]
}
final = append(final, line)
}
return strings.Join(final, "\n")
}