Debian Codegraph @

client/apicallerror.go

raw
1 package client
2
3 import (
4 "errors"
5 "fmt"
6 "strings"
7 )
8
9 type ApiCallError []ApiCallRc
10
11 func (e ApiCallError) Error() string {
12 var finalErr string
13 for i, r := range e {
14 finalErr += strings.TrimSpace(r.String())
15 if i < len(e)-1 {
16 finalErr += " next error: "
17 }
18 }
19 return finalErr
20 }
21
22 // ApiCallRcErr is a LINSTOR return code mask that satisfies the error
23 // interface. Use it as the target of errors.Is to check whether an
24 // ApiCallError contains a return code matching the mask:
25 //
26 // if errors.Is(err, client.ApiCallRcErr(linstor.FailNotConnected)) {
27 // // handle "not connected"
28 // }
29 //
30 // Refer to apiconsts.go in package linstor for the list of possible mask
31 // values.
32 type ApiCallRcErr uint64
33
34 func (c ApiCallRcErr) Error() string {
35 return fmt.Sprintf("LINSTOR API return code %#018x", uint64(c))
36 }
37
38 // Is reports whether any of the contained ApiCallRcs matches target. It only
39 // matches an ApiCallRcErr target, which lets ApiCallError be used with the
40 // standard errors.Is helper.
41 func (e ApiCallError) Is(target error) bool {
42 mask, ok := target.(ApiCallRcErr)
43 if !ok {
44 return false
45 }
46
47 for _, r := range e {
48 if r.Is(uint64(mask)) {
49 return true
50 }
51 }
52
53 return false
54 }
55
56 // IsApiCallError checks if an error is a specific type of LINSTOR error.
57 // It is a convenience wrapper around errors.Is(err, ApiCallRcErr(mask)).
58 func IsApiCallError(err error, mask uint64) bool {
59 return errors.Is(err, ApiCallRcErr(mask))
60 }