]> Cypherpunks.ru repositories - goredo.git/blob - err.go
9240a3e2da44134e57f01fcf418ee3526e0a96fe
[goredo.git] / err.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "errors"
22         "fmt"
23         "runtime"
24 )
25
26 type ErrLineErr struct {
27         err      error
28         file     string
29         line     int
30         function string
31 }
32
33 func (err ErrLineErr) Error() string {
34         return fmt.Sprintf("%s:%d: %s: %s", err.file, err.line, err.function, err.err)
35 }
36
37 func (err ErrLineErr) Unwrap() error {
38         return err.err
39 }
40
41 func ErrLine(err error) error {
42         if err == nil {
43                 return err
44         }
45         pc := make([]uintptr, 10)
46         n := runtime.Callers(2, pc)
47         pc = pc[:n]
48         frames := runtime.CallersFrames(pc)
49         frame, _ := frames.Next()
50         var errLine ErrLineErr
51         if errors.As(err, &errLine) {
52                 errLine.function = frame.Function + "," + errLine.function
53                 return errLine
54         }
55         return ErrLineErr{err, frame.File, frame.Line, frame.Function}
56 }