File: abort.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,340 kB
  • sloc: javascript: 65; makefile: 23; sh: 14
file content (61 lines) | stat: -rw-r--r-- 1,091 bytes parent folder | download
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
package command

import "fmt"

type AbortDetails struct {
	ExitCode  int
	Error     error
	EmitUsage bool
}

func Abort(details AbortDetails) {
	panic(details)
}

func AbortGracefullyWith(format string, args ...any) {
	Abort(AbortDetails{
		ExitCode:  0,
		Error:     fmt.Errorf(format, args...),
		EmitUsage: false,
	})
}

func AbortWith(format string, args ...any) {
	Abort(AbortDetails{
		ExitCode:  1,
		Error:     fmt.Errorf(format, args...),
		EmitUsage: false,
	})
}

func AbortWithUsage(format string, args ...any) {
	Abort(AbortDetails{
		ExitCode:  1,
		Error:     fmt.Errorf(format, args...),
		EmitUsage: true,
	})
}

func AbortIfError(preamble string, err error) {
	if err != nil {
		Abort(AbortDetails{
			ExitCode:  1,
			Error:     fmt.Errorf("%s\n%s", preamble, err.Error()),
			EmitUsage: false,
		})
	}
}

func AbortIfErrors(preamble string, errors []error) {
	if len(errors) > 0 {
		out := ""
		for _, err := range errors {
			out += err.Error()
		}
		Abort(AbortDetails{
			ExitCode:  1,
			Error:     fmt.Errorf("%s\n%s", preamble, out),
			EmitUsage: false,
		})
	}
}