-
Notifications
You must be signed in to change notification settings - Fork 245
/
errors.go
67 lines (56 loc) · 1.93 KB
/
errors.go
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
60
61
62
63
64
65
66
67
package shared
import (
"errors"
"fmt"
"github.com/rs/zerolog"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/spicedb/pkg/spiceerrors"
)
// ErrServiceReadOnly is an extended GRPC error returned when a service is in read-only mode.
var ErrServiceReadOnly = mustMakeStatusReadonly()
func mustMakeStatusReadonly() error {
status, err := status.New(codes.Unavailable, "service read-only").WithDetails(&errdetails.ErrorInfo{
Reason: v1.ErrorReason_name[int32(v1.ErrorReason_ERROR_REASON_SERVICE_READ_ONLY)],
Domain: spiceerrors.Domain,
})
if err != nil {
panic("error constructing shared error type")
}
return status.Err()
}
// NewSchemaWriteDataValidationError creates a new error representing that a schema write cannot be
// completed due to existing data that would be left unreferenced.
func NewSchemaWriteDataValidationError(message string, args ...any) ErrSchemaWriteDataValidation {
return ErrSchemaWriteDataValidation{
error: fmt.Errorf(message, args...),
}
}
// ErrSchemaWriteDataValidation occurs when a schema cannot be applied due to leaving data unreferenced.
type ErrSchemaWriteDataValidation struct {
error
}
// MarshalZerologObject implements zerolog object marshalling.
func (err ErrSchemaWriteDataValidation) MarshalZerologObject(e *zerolog.Event) {
e.Err(err.error)
}
// GRPCStatus implements retrieving the gRPC status for the error.
func (err ErrSchemaWriteDataValidation) GRPCStatus() *status.Status {
return spiceerrors.WithCodeAndDetails(
err,
codes.InvalidArgument,
spiceerrors.ForReason(
v1.ErrorReason_ERROR_REASON_SCHEMA_TYPE_ERROR,
map[string]string{},
),
)
}
func AsValidationError(err error) *ErrSchemaWriteDataValidation {
var validationErr ErrSchemaWriteDataValidation
if errors.As(err, &validationErr) {
return &validationErr
}
return nil
}