File: apicallerror.go

package info (click to toggle)
golang-github-linbit-golinstor 0.55.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 480 kB
  • sloc: makefile: 11
file content (47 lines) | stat: -rw-r--r-- 1,072 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
package client

import (
	"strings"
)

type ApiCallError []ApiCallRc

func (e ApiCallError) Error() string {
	var finalErr string
	for i, r := range e {
		finalErr += strings.TrimSpace(r.String())
		if i < len(e)-1 {
			finalErr += " next error: "
		}
	}
	return finalErr
}

// Is is a shorthand for checking all ApiCallRcs of an ApiCallError against
// a given mask.
func (e ApiCallError) Is(mask uint64) bool {
	for _, r := range e {
		if r.Is(mask) {
			return true
		}
	}

	return false
}

// Is can be used to check the return code against a given mask. Since LINSTOR
// return codes are designed to be machine readable, this can be used to check
// for a very specific type of error.
// Refer to package apiconsts.go in package linstor for a list of possible
// mask values.
func (r ApiCallRc) Is(mask uint64) bool { return (uint64(r.RetCode) & mask) == mask }

// IsApiCallError checks if an error is a specific type of LINSTOR error.
func IsApiCallError(err error, mask uint64) bool {
	e, ok := err.(ApiCallError)
	if !ok {
		return false
	}

	return e.Is(mask)
}