ParsePanic allows you to get an error object from the output of a go program that panicked. This is particularly useful with https://github.com/mitchellh/panicwrap.
(text string)
| 14 | // ParsePanic allows you to get an error object from the output of a go program |
| 15 | // that panicked. This is particularly useful with https://github.com/mitchellh/panicwrap. |
| 16 | func ParsePanic(text string) (*Error, error) { |
| 17 | lines := strings.Split(text, "\n") |
| 18 | |
| 19 | state := "start" |
| 20 | |
| 21 | var message string |
| 22 | var stack []StackFrame |
| 23 | |
| 24 | for i := 0; i < len(lines); i++ { |
| 25 | line := lines[i] |
| 26 | |
| 27 | if state == "start" { |
| 28 | if strings.HasPrefix(line, "panic: ") { |
| 29 | message = strings.TrimPrefix(line, "panic: ") |
| 30 | state = "seek" |
| 31 | } else { |
| 32 | return nil, Errorf("bugsnag.panicParser: Invalid line (no prefix): %s", line) |
| 33 | } |
| 34 | |
| 35 | } else if state == "seek" { |
| 36 | if strings.HasPrefix(line, "goroutine ") && strings.HasSuffix(line, "[running]:") { |
| 37 | state = "parsing" |
| 38 | } |
| 39 | |
| 40 | } else if state == "parsing" { |
| 41 | if line == "" { |
| 42 | state = "done" |
| 43 | break |
| 44 | } |
| 45 | createdBy := false |
| 46 | if strings.HasPrefix(line, "created by ") { |
| 47 | line = strings.TrimPrefix(line, "created by ") |
| 48 | createdBy = true |
| 49 | } |
| 50 | |
| 51 | i++ |
| 52 | |
| 53 | if i >= len(lines) { |
| 54 | return nil, Errorf("bugsnag.panicParser: Invalid line (unpaired): %s", line) |
| 55 | } |
| 56 | |
| 57 | frame, err := parsePanicFrame(line, lines[i], createdBy) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | stack = append(stack, *frame) |
| 63 | if createdBy { |
| 64 | state = "done" |
| 65 | break |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if state == "done" || state == "parsing" { |
| 71 | return &Error{Err: uncaughtPanic{message}, frames: stack}, nil |
| 72 | } |
| 73 | return nil, Errorf("could not parse panic: %v", text) |
searching dependent graphs…