aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/xerrors/new.go
blob: 0749f125a71b66f0550dbcdbb2fc2057e739b8e0 (plain) (blame)
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
package xerrors

import (
	"fmt"
	"io"

	"github.com/ydb-platform/ydb/library/go/x/xruntime"
)

type newError struct {
	msg        string
	stacktrace *xruntime.StackTrace
}

var _ ErrorStackTrace = &newError{}

func New(text string) error {
	return &newError{
		msg:        text,
		stacktrace: newStackTrace(1, nil),
	}
}

func (e *newError) Error() string {
	return e.msg
}

func (e *newError) Format(s fmt.State, v rune) {
	switch v {
	case 'v':
		if s.Flag('+') && e.stacktrace != nil {
			_, _ = io.WriteString(s, e.msg)
			_, _ = io.WriteString(s, "\n")
			writeStackTrace(s, e.stacktrace)
			return
		}

		fallthrough
	case 's':
		_, _ = io.WriteString(s, e.msg)
	case 'q':
		_, _ = fmt.Fprintf(s, "%q", e.msg)
	}
}

func (e *newError) StackTrace() *xruntime.StackTrace {
	return e.stacktrace
}