this repo has no description
1package errors 2 3import ( 4 "encoding/json" 5 "fmt" 6) 7 8type XrpcError struct { 9 Tag string `json:"error"` 10 Message string `json:"message"` 11} 12 13func (x XrpcError) Error() string { 14 if x.Message != "" { 15 return fmt.Sprintf("%s: %s", x.Tag, x.Message) 16 } 17 return x.Tag 18} 19 20func NewXrpcError(opts ...ErrOpt) XrpcError { 21 x := XrpcError{} 22 for _, o := range opts { 23 o(&x) 24 } 25 26 return x 27} 28 29type ErrOpt = func(xerr *XrpcError) 30 31func WithTag(tag string) ErrOpt { 32 return func(xerr *XrpcError) { 33 xerr.Tag = tag 34 } 35} 36 37func WithMessage[S ~string](s S) ErrOpt { 38 return func(xerr *XrpcError) { 39 xerr.Message = string(s) 40 } 41} 42 43func WithError(e error) ErrOpt { 44 return func(xerr *XrpcError) { 45 xerr.Message = e.Error() 46 } 47} 48 49var MissingActorDidError = NewXrpcError( 50 WithTag("MissingActorDid"), 51 WithMessage("actor DID not supplied"), 52) 53 54var OwnerNotFoundError = NewXrpcError( 55 WithTag("OwnerNotFound"), 56 WithMessage("owner not set for this service"), 57) 58 59var RepoNotFoundError = NewXrpcError( 60 WithTag("RepoNotFound"), 61 WithMessage("failed to access repository"), 62) 63 64var AuthError = func(err error) XrpcError { 65 return NewXrpcError( 66 WithTag("Auth"), 67 WithError(fmt.Errorf("signature verification failed: %w", err)), 68 ) 69} 70 71var InvalidRepoError = func(r string) XrpcError { 72 return NewXrpcError( 73 WithTag("InvalidRepo"), 74 WithError(fmt.Errorf("supplied at-uri is not a repo: %s", r)), 75 ) 76} 77 78var GitError = func(e error) XrpcError { 79 return NewXrpcError( 80 WithTag("Git"), 81 WithError(fmt.Errorf("git error: %w", e)), 82 ) 83} 84 85var AccessControlError = func(d string) XrpcError { 86 return NewXrpcError( 87 WithTag("AccessControl"), 88 WithError(fmt.Errorf("DID does not have sufficent access permissions for this operation: %s", d)), 89 ) 90} 91 92var RepoExistsError = func(r string) XrpcError { 93 return NewXrpcError( 94 WithTag("RepoExists"), 95 WithError(fmt.Errorf("repo already exists: %s", r)), 96 ) 97} 98 99var RecordExistsError = func(r string) XrpcError { 100 return NewXrpcError( 101 WithTag("RecordExists"), 102 WithError(fmt.Errorf("repo already exists: %s", r)), 103 ) 104} 105 106func GenericError(err error) XrpcError { 107 return NewXrpcError( 108 WithTag("Generic"), 109 WithError(err), 110 ) 111} 112 113func Unmarshal(errStr string) (XrpcError, error) { 114 var xerr XrpcError 115 err := json.Unmarshal([]byte(errStr), &xerr) 116 if err != nil { 117 return XrpcError{}, fmt.Errorf("failed to unmarshal XrpcError: %w", err) 118 } 119 return xerr, nil 120}