File: errors.go

package info (click to toggle)
cfrpki 1.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,960 kB
  • sloc: makefile: 73; sh: 34
file content (101 lines) | stat: -rw-r--r-- 1,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package syncpki

import (
	"fmt"
	"github.com/getsentry/sentry-go"
	"net/http"
	"runtime"
)

const (
	ERROR_RRDP_UNKNOWN = iota
	ERROR_RRDP_FETCH
)

type stack []uintptr
type Frame uintptr

var (
	ErrorTypeToName = map[int]string{
		ERROR_RRDP_UNKNOWN: "unknown",
		ERROR_RRDP_FETCH:   "fetch",
	}
)

type RRDPError struct {
	EType int

	InnerErr error
	Message  string

	Request *http.Request

	URL, Rsync string

	Stack *stack
}

func callers() *stack {
	const depth = 32
	var pcs [depth]uintptr
	n := runtime.Callers(3, pcs[:])
	var st stack = pcs[0:n]
	return &st
}

// This function returns the Stacktrace of the error.
// The naming scheme corresponds to what Sentry fetches
// https://github.com/getsentry/sentry-go/blob/master/stacktrace.go#L49
func StackTrace(s *stack) []Frame {
	f := make([]Frame, len(*s))
	for i := 0; i < len(f); i++ {
		f[i] = Frame((*s)[i])
	}
	return f
}

func (e *RRDPError) StackTrace() []Frame {
	return StackTrace(e.Stack)
}

func (e *RRDPError) Error() string {
	repoinfo := "for repo"
	if e.URL != "" {
		repoinfo = fmt.Sprintf("for repo rrdp:%s (rsync:%s)", e.URL, e.Rsync)
	}

	var err string
	if e.InnerErr != nil {
		err = fmt.Sprintf(": %s", e.InnerErr.Error())
	}

	return fmt.Sprintf("%s %s%v", e.Message, repoinfo, err)
}

func (e *RRDPError) SetSentryScope(scope *sentry.Scope) {
	scope.SetTag("Type", ErrorTypeToName[e.EType])
	if e.URL != "" {
		scope.SetTag("Repository.RRDP", e.URL)
	}
	if e.Rsync != "" {
		scope.SetTag("Repository.rsync", e.Rsync)
	}
	if e.Request != nil {
		scope.SetRequest(e.Request)
	}
}

func (e *RRDPError) SetURL(rrdp, rsync string) {
	e.URL = rrdp
	e.Rsync = rsync
}

func NewRRDPErrorFetch(request *http.Request, err error) *RRDPError {
	return &RRDPError{
		EType:    ERROR_RRDP_FETCH,
		Request:  request,
		InnerErr: err,
		Message:  "error fetching",
		Stack:    callers(),
	}
}