~bigbes/ci-cacher

ref: a42721d666875bd9315140623854450a73265c15 ci-cacher/cmd/errors.go -rw-r--r-- 859 bytes
a42721d6 — Eugene Blikh Add install.sh one-line bootstrap, published next to the binaries 11 days ago
                                                                                
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
package cmd

import "errors"

// Sentinel errors recognized by Execute → exit code mapping.
//
// exists  → ErrNotFound  →  1
// download without --url/--exec, key missing → ErrMissNoFallback → 3
// --exec script failed → the script's own exit status
// any other error → 2
var (
	ErrNotFound       = errors.New("cacher: key not found")
	ErrMissNoFallback = errors.New("cacher: cache miss and no --url/--exec fallback")
)

func exitCodeFor(err error) int {
	// A failed --exec script reports its own status: the caller wrote
	// that script and its exit code says more than a generic 2.
	var coded interface{ ExitCode() int }
	switch {
	case err == nil:
		return 0
	case errors.Is(err, ErrNotFound):
		return 1
	case errors.Is(err, ErrMissNoFallback):
		return 3
	case errors.As(err, &coded):
		return coded.ExitCode()
	default:
		return 2
	}
}