Commit 0fcbf33e authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 936e6550
...@@ -37,6 +37,11 @@ func (e *Error) Error() string { ...@@ -37,6 +37,11 @@ func (e *Error) Error() string {
// raise error to upper level // raise error to upper level
func raise(arg interface{}) { func raise(arg interface{}) {
// XXX is it ok?
e, ok := arg.(*Error)
if ok {
panic(e) // reraise
}
panic(&Error{arg, nil}) panic(&Error{arg, nil})
} }
...@@ -118,6 +123,65 @@ func erraddcontext(f func() interface{}) { ...@@ -118,6 +123,65 @@ func erraddcontext(f func() interface{}) {
} }
// get name of currently running function (caller of myfuncname())
func myfuncname() string {
pcv := [1]uintptr{}
runtime.Callers(2, pcv[:])
f := runtime.FuncForPC(pcv[0])
if f == nil {
return ""
}
return f.Name()
}
// add calling context to error
// n TODO
func erraddcallercontext(topfunc string, e *Error) *Error {
// all callers
var pcv = []uintptr{0}
for {
pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(1, 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 topfunc
if topfunc != "" && frame.Function == topfunc {
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:]
for _, f := range funcv {
f = strings.TrimPrefix(f, "main.")
e = &Error{f, e}
}
return e
}
// build error message associated with e // build error message associated with e
// should be called from under errcatch - in place which caught the error // should be called from under errcatch - in place which caught the error
func errmessage(e *Error) string { func errmessage(e *Error) string {
...@@ -125,7 +189,8 @@ func errmessage(e *Error) string { ...@@ -125,7 +189,8 @@ func errmessage(e *Error) string {
var pcv = []uintptr{0} var pcv = []uintptr{0}
for { for {
pcv = make([]uintptr, 2*len(pcv)) pcv = make([]uintptr, 2*len(pcv))
n := runtime.Callers(0, pcv) //n := runtime.Callers(0, pcv)
n := runtime.Callers(1, pcv)
if n < len(pcv) { if n < len(pcv) {
pcv = pcv[:n] pcv = pcv[:n]
break break
...@@ -138,8 +203,10 @@ func errmessage(e *Error) string { ...@@ -138,8 +203,10 @@ func errmessage(e *Error) string {
for more := true; more; { for more := true; more; {
var frame runtime.Frame var frame runtime.Frame
frame, more = frames.Next() frame, more = frames.Next()
// do not go beyond main // // do not go beyond main
if frame.Function == "main.main" { if frame.Function == "main.main" {
// do not go beyond caller
// if len(funcv) > 0 && frame.Function == funcv[0] {
break break
} }
// skip intermediates // skip intermediates
......
...@@ -421,6 +421,8 @@ func cmd_pull_(pullspecv []PullSpec) { ...@@ -421,6 +421,8 @@ func cmd_pull_(pullspecv []PullSpec) {
// prefix namespace and this way won't leave stale removed things) // prefix namespace and this way won't leave stale removed things)
xgit("rm", "--cached", "-r", "--ignore-unmatch", "--", prefix) xgit("rm", "--cached", "-r", "--ignore-unmatch", "--", prefix)
curfunc := myfuncname()
fmt.Printf("curfunc: %q\n", curfunc)
err := filepath.Walk(dir, func (path string, info os.FileInfo, err error) (errout error) { err := filepath.Walk(dir, func (path string, info os.FileInfo, err error) (errout error) {
// any error -> stop // any error -> stop
if err != nil { if err != nil {
...@@ -430,7 +432,9 @@ func cmd_pull_(pullspecv []PullSpec) { ...@@ -430,7 +432,9 @@ func cmd_pull_(pullspecv []PullSpec) {
// propagate exceptions properly via filepath.Walk as errors (filepath is not our code) // propagate exceptions properly via filepath.Walk as errors (filepath is not our code)
defer errcatch(func (e *Error) { defer errcatch(func (e *Error) {
errout = e //errout = e
//errout = &Error{errmessage(e), nil}
errout = erraddcallercontext(curfunc, e)
}) })
// files -> add directly to index to commit later // files -> add directly to index to commit later
...@@ -475,7 +479,9 @@ func cmd_pull_(pullspecv []PullSpec) { ...@@ -475,7 +479,9 @@ func cmd_pull_(pullspecv []PullSpec) {
if err != nil { if err != nil {
if _, ok := err.(*Error); ok { if _, ok := err.(*Error); ok {
// this was already raised - reraise // this was already raised - reraise
panic(err) //panic(err)
//raise(err.(*Error).arg)
raise(err)
} }
raisef("pulling from %s: %s", dir, err) raisef("pulling from %s: %s", dir, err)
......
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