Commit 936e6550 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 5f319762
...@@ -15,6 +15,7 @@ package main ...@@ -15,6 +15,7 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"strings" "strings"
) )
...@@ -115,3 +116,61 @@ func erraddcontext(f func() interface{}) { ...@@ -115,3 +116,61 @@ func erraddcontext(f func() interface{}) {
arg := f() arg := f()
panic(&Error{arg, e}) panic(&Error{arg, e})
} }
// build error message associated with e
// should be called from under errcatch - in place which caught the error
func errmessage(e *Error) string {
// all callers
var pcv = []uintptr{0}
for {
pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(0, pcv)
if n < len(pcv) {
pcv = pcv[:n]
break
}
}
// pcv -> function names
funcv := []string{}
frames := runtime.CallersFrames(pcv)
for more := true; more; {
var frame runtime.Frame
frame, more = frames.Next()
// do not go beyond main
if frame.Function == "main.main" {
break
}
// skip intermediates
if strings.HasSuffix(frame.Function, "_") {
continue
}
funcv = append(funcv, frame.Function)
}
// do not show anything after raise*()
iraise := -1
for i, funcname := range(funcv) {
if strings.HasPrefix(funcname, "main.raise") {
iraise = i
}
}
funcv = funcv[iraise+1:]
// print funcv prefix in top-down order
msgprefix := []string{}
for i := range(funcv) {
f := funcv[len(funcv)-i-1]
f = strings.TrimPrefix(f, "main.")
msgprefix = append(msgprefix, f)
}
msg := strings.Join(append(msgprefix, ""), ": ")
msg += fmt.Sprint(e)
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
return msg
}
...@@ -66,7 +66,6 @@ import ( ...@@ -66,7 +66,6 @@ import (
"os" "os"
pathpkg "path" pathpkg "path"
"path/filepath" "path/filepath"
"runtime"
"runtime/debug" "runtime/debug"
"sort" "sort"
"strings" "strings"
...@@ -946,56 +945,7 @@ func main() { ...@@ -946,56 +945,7 @@ func main() {
// catch Error and report info from it // catch Error and report info from it
defer errcatch(func(e *Error) { defer errcatch(func(e *Error) {
// all callers msg := errmessage(e)
var pcv = []uintptr{0}
for {
pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(0, pcv)
if n < len(pcv) {
pcv = pcv[:n]
break
}
}
// pcv -> function names
funcv := []string{}
frames := runtime.CallersFrames(pcv)
for more := true; more; {
var frame runtime.Frame
frame, more = frames.Next()
// do not go beyound main
if frame.Function == "main.main" {
break
}
// skip intermediates
if strings.HasSuffix(frame.Function, "_") {
continue
}
funcv = append(funcv, frame.Function)
}
// do not show anything after raise*()
iraise := -1
for i, funcname := range(funcv) {
if strings.HasPrefix(funcname, "main.raise") {
iraise = i
}
}
funcv = funcv[iraise+1:]
// print funcv prefix in top-down order
msgprefix := []string{}
for i := range(funcv) {
f := funcv[len(funcv)-i-1]
f = strings.TrimPrefix(f, "main.")
msgprefix = append(msgprefix, f)
}
msg := strings.Join(append(msgprefix, ""), ": ")
msg += fmt.Sprint(e)
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprint(os.Stderr, msg) fmt.Fprint(os.Stderr, msg)
// also show traceback if debug // also show traceback if debug
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment