summaryrefslogtreecommitdiff
path: root/errors.go
blob: b03a6ddef04930d82b5422ada1ca2867aeba2b92 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package zw

import (
	"fmt"

	zwdaemonv1 "pablu.de/zw-go/gen/zwdaemon/v1"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

const zwErrorDetailTypeURL = "type.googleapis.com/zw.daemon.v1.ZwErrorDetail"

type ErrorCode = zwdaemonv1.ZwErrorCode

const (
	ErrorCodeUnspecified                = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_UNSPECIFIED
	ErrorCodeVaultSealed                = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_VAULT_SEALED
	ErrorCodeVaultAlreadyUnsealed       = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_VAULT_ALREADY_UNSEALED
	ErrorCodeEntryNotFound              = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_ENTRY_NOT_FOUND
	ErrorCodeEntryAlreadyExists         = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_ENTRY_ALREADY_EXISTS
	ErrorCodeAuthenticationOrCorruption = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_AUTHENTICATION_OR_CORRUPTION
	ErrorCodeInvalidVaultFormat         = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_INVALID_VAULT_FORMAT
	ErrorCodeUnsupportedVaultVersion    = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_UNSUPPORTED_VAULT_VERSION
	ErrorCodeUnsupportedProtocolVersion = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_UNSUPPORTED_PROTOCOL_VERSION
	ErrorCodePermissionDenied           = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_PERMISSION_DENIED
	ErrorCodeStorageFull                = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_STORAGE_FULL
	ErrorCodeCommitFailed               = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_COMMIT_FAILED
	ErrorCodeInvalidRequest             = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_INVALID_REQUEST
	ErrorCodeInternal                   = zwdaemonv1.ZwErrorCode_ZW_ERROR_CODE_INTERNAL
)

type RPCError struct {
	GRPCCode  codes.Code
	ZwCode    *ErrorCode
	Message   string
	Retryable bool

	status *status.Status
}

func (e *RPCError) Error() string {
	return e.Message
}

func (e *RPCError) GRPCStatus() *status.Status {
	if e.status != nil {
		return e.status
	}
	return status.New(e.GRPCCode, e.Message)
}

type IncompatibleAPIVersionError struct {
	ServerMajor    uint32
	ServerMinor    uint32
	SupportedMajor uint32
}

func (e *IncompatibleAPIVersionError) Error() string {
	return fmt.Sprintf(
		"daemon API version %d.%d is incompatible; this client supports major version %d",
		e.ServerMajor,
		e.ServerMinor,
		e.SupportedMajor,
	)
}

type UploadReadError struct {
	Err error
}

func (e *UploadReadError) Error() string {
	return "read upload source: " + e.Err.Error()
}

func (e *UploadReadError) Unwrap() error {
	return e.Err
}

func convertRPCError(err error) error {
	if err == nil {
		return nil
	}

	grpcStatus, ok := status.FromError(err)
	if !ok {
		return err
	}

	rpcErr := &RPCError{
		GRPCCode: grpcStatus.Code(),
		Message:  grpcStatus.Message(),
		status:   grpcStatus,
	}

	for _, detail := range grpcStatus.Proto().GetDetails() {
		if detail.GetTypeUrl() != zwErrorDetailTypeURL {
			continue
		}

		var zwDetail zwdaemonv1.ZwErrorDetail
		if err := detail.UnmarshalTo(&zwDetail); err != nil {
			continue
		}

		code := zwDetail.GetCode()
		rpcErr.ZwCode = &code
		rpcErr.Retryable = zwDetail.GetRetryable()
		break
	}

	return rpcErr
}