Skip to content

Commit 981440d

Browse files
authored
serviceerror: add error checking helpers (#227)
1 parent cf9c3e8 commit 981440d

28 files changed

+223
-0
lines changed

serviceerror/aborted.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -47,3 +48,10 @@ func newAborted(st *status.Status) error {
4748
st: st,
4849
}
4950
}
51+
52+
// IsAborted returns whether any error in the provided error's chain is an
53+
// Aborted error.
54+
func IsAborted(err error) bool {
55+
var serr *Aborted
56+
return errors.As(err, &serr)
57+
}

serviceerror/already_exists.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -53,3 +54,10 @@ func newAlreadyExists(st *status.Status) error {
5354
st: st,
5455
}
5556
}
57+
58+
// IsAlreadyExists returns whether any error in the provided error's chain is an
59+
// AlreadyExists error.
60+
func IsAlreadyExists(err error) bool {
61+
var serr *AlreadyExists
62+
return errors.As(err, &serr)
63+
}

serviceerror/canceled.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newCanceled(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsCanceled returns whether any error in the provided error's chain is a
54+
// Canceled error.
55+
func IsCanceled(err error) bool {
56+
var serr *Canceled
57+
return errors.As(err, &serr)
58+
}

serviceerror/cancellation_already_requested.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -54,3 +55,10 @@ func newCancellationAlreadyRequested(st *status.Status) error {
5455
st: st,
5556
}
5657
}
58+
59+
// IsCancellationAlreadyRequested returns whether any error in the provided error's chain is a
60+
// CancellationAlreadyRequested error.
61+
func IsCancellationAlreadyRequested(err error) bool {
62+
var serr *CancellationAlreadyRequested
63+
return errors.As(err, &serr)
64+
}

serviceerror/client_version_not_supported.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -70,3 +71,10 @@ func newClientVersionNotSupported(st *status.Status, errDetails *errordetails.Cl
7071
st: st,
7172
}
7273
}
74+
75+
// IsClientVersionNotSupported returns whether any error in the provided error's chain is a
76+
// ClientVersionNotSupported error.
77+
func IsClientVersionNotSupported(err error) bool {
78+
var serr *ClientVersionNotSupported
79+
return errors.As(err, &serr)
80+
}

serviceerror/data_loss.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newDataLoss(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsDataLoss returns whether any error in the provided error's chain is a
54+
// DataLoss error.
55+
func IsDataLoss(err error) bool {
56+
var serr *DataLoss
57+
return errors.As(err, &serr)
58+
}

serviceerror/deadline_exceeded.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newDeadlineExceeded(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsDeadlineExceeded returns whether any error in the provided error's chain is a
54+
// DeadlineExceeded error.
55+
func IsDeadlineExceeded(err error) bool {
56+
var serr *DeadlineExceeded
57+
return errors.As(err, &serr)
58+
}

serviceerror/failed_precondition.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newFailedPrecondition(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsFailedPrecondition returns whether any error in the provided error's chain is a
54+
// FailedPrecondition error.
55+
func IsFailedPrecondition(err error) bool {
56+
var serr *FailedPrecondition
57+
return errors.As(err, &serr)
58+
}

serviceerror/internal.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newInternal(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsInternal returns whether any error in the provided error's chain is an
54+
// Internal error.
55+
func IsInternal(err error) bool {
56+
var serr *Internal
57+
return errors.As(err, &serr)
58+
}

serviceerror/invalid_argument.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serviceerror
22

33
import (
4+
"errors"
45
"fmt"
56

67
"google.golang.org/grpc/codes"
@@ -48,3 +49,10 @@ func newInvalidArgument(st *status.Status) error {
4849
st: st,
4950
}
5051
}
52+
53+
// IsInvalidArgument returns whether any error in the provided error's chain is an
54+
// InvalidArgument error.
55+
func IsInvalidArgument(err error) bool {
56+
var serr *InvalidArgument
57+
return errors.As(err, &serr)
58+
}

0 commit comments

Comments
 (0)