File: standard_failures.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 1,874 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
package standard

import (
	"fmt"

	"github.com/nicholas-fedor/shoutrrr/internal/failures"
)

const (
	// FailTestSetup is the FailureID used to represent an error that is part of the setup for tests.
	FailTestSetup failures.FailureID = -1
	// FailParseURL is the FailureID used to represent failing to parse the service URL.
	FailParseURL failures.FailureID = -2
	// FailServiceInit is the FailureID used to represent failure of a service.Initialize method.
	FailServiceInit failures.FailureID = -3
	// FailUnknown is the default FailureID.
	FailUnknown failures.FailureID = iota
)

type failureLike interface {
	failures.Failure
}

// Failure creates a Failure instance corresponding to the provided failureID, wrapping the provided error.
func Failure(failureID failures.FailureID, err error, args ...any) failures.Failure {
	messages := map[int]string{
		int(FailTestSetup): "test setup failed",
		int(FailParseURL):  "error parsing Service URL",
		int(FailUnknown):   "an unknown error occurred",
	}

	msg := messages[int(failureID)]
	if msg == "" {
		msg = messages[int(FailUnknown)]
	}

	// If variadic arguments are provided, format them correctly
	if len(args) > 0 {
		if format, ok := args[0].(string); ok && len(args) > 1 {
			// Treat the first argument as a format string and the rest as its arguments
			extraMsg := fmt.Sprintf(format, args[1:]...)
			msg = fmt.Sprintf("%s %s", msg, extraMsg)
		} else {
			// If no format string is provided, just append the arguments as-is
			msg = fmt.Sprintf("%s %v", msg, args)
		}
	}

	return failures.Wrap(msg, failureID, err)
}

// IsTestSetupFailure checks whether the given failure is due to the test setup being broken.
func IsTestSetupFailure(failure failureLike) (string, bool) {
	if failure != nil && failure.ID() == FailTestSetup {
		return "test setup failed: " + failure.Error(), true
	}

	return "", false
}