this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

identity: improve error messages and wrapping (#901)

authored by

bnewbold and committed by
GitHub
67e23605 5e1b3940

+26 -22
+4 -3
atproto/identity/base_directory.go
··· 46 46 ident := ParseIdentity(doc) 47 47 declared, err := ident.DeclaredHandle() 48 48 if err != nil { 49 - return nil, err 49 + return nil, fmt.Errorf("could not verify handle/DID match: %w", err) 50 50 } 51 51 if declared != h { 52 - return nil, ErrHandleMismatch 52 + return nil, fmt.Errorf("%w: %s != %s", ErrHandleMismatch, declared, h) 53 53 } 54 54 ident.Handle = declared 55 55 ··· 66 66 if errors.Is(err, ErrHandleNotDeclared) { 67 67 ident.Handle = syntax.HandleInvalid 68 68 } else if err != nil { 69 - return nil, err 69 + return nil, fmt.Errorf("could not parse handle from DID document: %w", err) 70 70 } else { 71 71 // if a handle was declared, resolve it 72 72 resolvedDID, err := d.ResolveHandle(ctx, declared) ··· 99 99 } 100 100 101 101 func (d *BaseDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error { 102 + // BaseDirectory itself does not implement caching 102 103 return nil 103 104 }
+3 -3
atproto/identity/cache_directory.go
··· 93 93 94 94 func (d *CacheDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) { 95 95 if h.IsInvalidHandle() { 96 - return "", fmt.Errorf("invalid handle") 96 + return "", fmt.Errorf("can not resolve handle: %w", ErrInvalidHandle) 97 97 } 98 98 entry, ok := d.handleCache.Get(h) 99 99 if ok && !d.IsHandleStale(&entry) { ··· 230 230 231 231 declared, err := ident.DeclaredHandle() 232 232 if err != nil { 233 - return nil, hit, err 233 + return nil, hit, fmt.Errorf("could not verify handle/DID mapping: %w", err) 234 234 } 235 235 if declared != h { 236 - return nil, hit, ErrHandleMismatch 236 + return nil, hit, fmt.Errorf("%w: %s != %s", ErrHandleMismatch, declared, h) 237 237 } 238 238 return ident, hit, nil 239 239 }
+3
atproto/identity/directory.go
··· 52 52 // Indicates that DID document did not include a public key with the specified ID 53 53 var ErrKeyNotDeclared = errors.New("DID document did not declare a relevant public key") 54 54 55 + // Handle was invalid, in a situation where a valid handle is required. 56 + var ErrInvalidHandle = errors.New("Invalid Handle") 57 + 55 58 var DefaultPLCURL = "https://plc.directory" 56 59 57 60 // Returns a reasonable Directory implementation for applications
+4 -4
atproto/identity/handle.go
··· 35 35 var dnsErr *net.DNSError 36 36 if errors.As(err, &dnsErr) { 37 37 if dnsErr.IsNotFound { 38 - return "", ErrHandleNotFound 38 + return "", fmt.Errorf("%w: %s", ErrHandleNotFound, handle) 39 39 } 40 40 } 41 41 if err != nil { ··· 138 138 var dnsErr *net.DNSError 139 139 if errors.As(err, &dnsErr) { 140 140 if dnsErr.IsNotFound { 141 - return "", fmt.Errorf("%w: DNS NXDOMAIN for %s", ErrHandleNotFound, handle) 141 + return "", fmt.Errorf("%w: DNS NXDOMAIN for HTTP well-known resolution of %s", ErrHandleNotFound, handle) 142 142 } 143 143 } 144 144 return "", fmt.Errorf("%w: HTTP well-known request error: %w", ErrHandleResolutionFailed, err) ··· 162 162 line := strings.TrimSpace(string(b)) 163 163 outDid, err := syntax.ParseDID(line) 164 164 if err != nil { 165 - return outDid, fmt.Errorf("bad well-known for %s: %w", handle, err) 165 + return outDid, fmt.Errorf("%w: invalid DID in HTTP well-known for %s", ErrHandleResolutionFailed, handle) 166 166 } 167 167 return outDid, err 168 168 } ··· 173 173 var did syntax.DID 174 174 175 175 if handle.IsInvalidHandle() { 176 - return "", fmt.Errorf("invalid handle") 176 + return "", fmt.Errorf("can not resolve handle: %w", ErrInvalidHandle) 177 177 } 178 178 179 179 if !handle.AllowedTLD() {
+12 -12
atproto/identity/redisdir/redis_directory.go
··· 57 57 func NewRedisDirectory(inner identity.Directory, redisURL string, hitTTL, errTTL, invalidHandleTTL time.Duration, lruSize int) (*RedisDirectory, error) { 58 58 opt, err := redis.ParseURL(redisURL) 59 59 if err != nil { 60 - return nil, err 60 + return nil, fmt.Errorf("could not configure redis identity cache: %w", err) 61 61 } 62 62 rdb := redis.NewClient(opt) 63 63 // check redis connection 64 64 _, err = rdb.Ping(context.TODO()).Result() 65 65 if err != nil { 66 - return nil, err 66 + return nil, fmt.Errorf("could not connect to redis identity cache: %w", err) 67 67 } 68 68 handleCache := cache.New(&cache.Options{ 69 69 Redis: rdb, ··· 117 117 }) 118 118 if err != nil { 119 119 he.DID = nil 120 - he.Err = fmt.Errorf("identity cache write: %w", err) 120 + he.Err = fmt.Errorf("identity cache write failed: %w", err) 121 121 return he 122 122 } 123 123 return he ··· 142 142 }) 143 143 if err != nil { 144 144 he.DID = nil 145 - he.Err = fmt.Errorf("identity cache write: %w", err) 145 + he.Err = fmt.Errorf("identity cache write failed: %w", err) 146 146 return he 147 147 } 148 148 err = d.handleCache.Set(&cache.Item{ ··· 153 153 }) 154 154 if err != nil { 155 155 he.DID = nil 156 - he.Err = fmt.Errorf("identity cache write: %w", err) 156 + he.Err = fmt.Errorf("identity cache write failed: %w", err) 157 157 return he 158 158 } 159 159 return he ··· 161 161 162 162 func (d *RedisDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) { 163 163 if h.IsInvalidHandle() { 164 - return "", errors.New("invalid handle") 164 + return "", fmt.Errorf("can not resolve handle: %w", identity.ErrInvalidHandle) 165 165 } 166 166 var entry handleEntry 167 167 err := d.handleCache.Get(ctx, redisDirPrefix+h.String(), &entry) 168 168 if err != nil && err != cache.ErrCacheMiss { 169 - return "", fmt.Errorf("identity cache read: %w", err) 169 + return "", fmt.Errorf("identity cache read failed: %w", err) 170 170 } 171 171 if err == nil && !d.isHandleStale(&entry) { // if no error... 172 172 handleCacheHits.Inc() ··· 191 191 // The result should now be in the cache 192 192 err := d.handleCache.Get(ctx, redisDirPrefix+h.String(), entry) 193 193 if err != nil && err != cache.ErrCacheMiss { 194 - return "", fmt.Errorf("identity cache read: %w", err) 194 + return "", fmt.Errorf("identity cache read failed: %w", err) 195 195 } 196 196 if err == nil && !d.isHandleStale(&entry) { // if no error... 197 197 if entry.Err != nil { ··· 251 251 }) 252 252 if err != nil { 253 253 entry.Identity = nil 254 - entry.Err = fmt.Errorf("identity cache write: %v", err) 254 + entry.Err = fmt.Errorf("identity cache write failed: %w", err) 255 255 return entry 256 256 } 257 257 if he != nil { ··· 263 263 }) 264 264 if err != nil { 265 265 entry.Identity = nil 266 - entry.Err = fmt.Errorf("identity cache write: %v", err) 266 + entry.Err = fmt.Errorf("identity cache write failed: %w", err) 267 267 return entry 268 268 } 269 269 } ··· 279 279 var entry identityEntry 280 280 err := d.identityCache.Get(ctx, redisDirPrefix+did.String(), &entry) 281 281 if err != nil && err != cache.ErrCacheMiss { 282 - return nil, false, fmt.Errorf("identity cache read: %v", err) 282 + return nil, false, fmt.Errorf("identity cache read failed: %w", err) 283 283 } 284 284 if err == nil && !d.isIdentityStale(&entry) { // if no error... 285 285 identityCacheHits.Inc() ··· 298 298 // The result should now be in the cache 299 299 err = d.identityCache.Get(ctx, redisDirPrefix+did.String(), &entry) 300 300 if err != nil && err != cache.ErrCacheMiss { 301 - return nil, false, fmt.Errorf("identity cache read: %v", err) 301 + return nil, false, fmt.Errorf("identity cache read failed: %w", err) 302 302 } 303 303 if err == nil && !d.isIdentityStale(&entry) { // if no error... 304 304 return entry.Identity, false, entry.Err