~james/core

core/xrpc/errors/errors.go -rw-r--r-- 2.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package errors

import (
	"encoding/json"
	"fmt"
)

type XrpcError struct {
	Tag     string `json:"error"`
	Message string `json:"message"`
}

func (x XrpcError) Error() string {
	if x.Message != "" {
		return fmt.Sprintf("%s: %s", x.Tag, x.Message)
	}
	return x.Tag
}

func NewXrpcError(opts ...ErrOpt) XrpcError {
	x := XrpcError{}
	for _, o := range opts {
		o(&x)
	}

	return x
}

type ErrOpt = func(xerr *XrpcError)

func WithTag(tag string) ErrOpt {
	return func(xerr *XrpcError) {
		xerr.Tag = tag
	}
}

func WithMessage[S ~string](s S) ErrOpt {
	return func(xerr *XrpcError) {
		xerr.Message = string(s)
	}
}

func WithError(e error) ErrOpt {
	return func(xerr *XrpcError) {
		xerr.Message = e.Error()
	}
}

var MissingActorDidError = NewXrpcError(
	WithTag("MissingActorDid"),
	WithMessage("actor DID not supplied"),
)

var OwnerNotFoundError = NewXrpcError(
	WithTag("OwnerNotFound"),
	WithMessage("owner not set for this service"),
)

var RepoNotFoundError = NewXrpcError(
	WithTag("RepoNotFound"),
	WithMessage("failed to access repository"),
)

var WebhookLimitExceededError = NewXrpcError(
	WithTag("WebhookLimitExceeded"),
	WithMessage("this repository or account has reached its webhook limit"),
)

var RefNotFoundError = NewXrpcError(
	WithTag("RefNotFound"),
	WithMessage("failed to access ref"),
)

var RequestTooLargeError = NewXrpcError(
	WithTag("RequestTooLarge"),
	WithMessage("request was too large"),
)

var AuthError = func(err error) XrpcError {
	return NewXrpcError(
		WithTag("Auth"),
		WithError(fmt.Errorf("signature verification failed: %w", err)),
	)
}

var InvalidRepoError = func(r string) XrpcError {
	return NewXrpcError(
		WithTag("InvalidRepo"),
		WithError(fmt.Errorf("supplied repo identifier is invalid: %s", r)),
	)
}

var GitError = func(e error) XrpcError {
	return NewXrpcError(
		WithTag("Git"),
		WithError(fmt.Errorf("git error: %w", e)),
	)
}

var AccessControlError = func(d string) XrpcError {
	return NewXrpcError(
		WithTag("AccessControl"),
		WithError(fmt.Errorf("DID does not have sufficient access permissions for this operation: %s", d)),
	)
}

var RepoExistsError = func(r string) XrpcError {
	return NewXrpcError(
		WithTag("RepoExists"),
		WithError(fmt.Errorf("repo already exists: %s", r)),
	)
}

var RecordExistsError = func(r string) XrpcError {
	return NewXrpcError(
		WithTag("RecordExists"),
		WithError(fmt.Errorf("repo already exists: %s", r)),
	)
}

func GenericError(err error) XrpcError {
	return NewXrpcError(
		WithTag("Generic"),
		WithError(err),
	)
}

func Unmarshal(errStr string) (XrpcError, error) {
	var xerr XrpcError
	err := json.Unmarshal([]byte(errStr), &xerr)
	if err != nil {
		return XrpcError{}, fmt.Errorf("failed to unmarshal XrpcError: %w", err)
	}
	return xerr, nil
}