this repo has no description
0
fork

Configure Feed

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

syntax: clearer error for trivial empty string case (#765)

Was getting "doesn't match regex" for trivial empty-string situations.
having shorter more descriptive errors helps debugging.

authored by

bnewbold and committed by
GitHub
f77c6b68 b3609c65

+66 -33
+6 -3
atproto/syntax/atidentifier.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "strings" 6 6 ) 7 7 ··· 10 10 } 11 11 12 12 func ParseAtIdentifier(raw string) (*AtIdentifier, error) { 13 + if raw == "" { 14 + return nil, errors.New("expected AT account identifier, got empty string") 15 + } 13 16 if strings.HasPrefix(raw, "did:") { 14 17 did, err := ParseDID(raw) 15 18 if err != nil { ··· 34 37 if ok { 35 38 return handle, nil 36 39 } 37 - return "", fmt.Errorf("AT Identifier is not a Handle") 40 + return "", errors.New("AT Identifier is not a Handle") 38 41 } 39 42 40 43 func (n AtIdentifier) IsDID() bool { ··· 47 50 if ok { 48 51 return did, nil 49 52 } 50 - return "", fmt.Errorf("AT Identifier is not a DID") 53 + return "", errors.New("AT Identifier is not a DID") 51 54 } 52 55 53 56 func (n AtIdentifier) Normalize() AtIdentifier {
+3 -2
atproto/syntax/aturi.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 17 18 18 19 func ParseATURI(raw string) (ATURI, error) { 19 20 if len(raw) > 8192 { 20 - return "", fmt.Errorf("ATURI is too long (8192 chars max)") 21 + return "", errors.New("ATURI is too long (8192 chars max)") 21 22 } 22 23 parts := aturiRegex.FindStringSubmatch(raw) 23 24 if parts == nil || len(parts) < 2 || parts[0] == "" { 24 - return "", fmt.Errorf("AT-URI syntax didn't validate via regex") 25 + return "", errors.New("AT-URI syntax didn't validate via regex") 25 26 } 26 27 // verify authority as either a DID or NSID 27 28 _, err := ParseAtIdentifier(parts[1])
+8 -5
atproto/syntax/cid.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 16 16 var cidRegex = regexp.MustCompile(`^[a-zA-Z0-9+=]{8,256}$`) 17 17 18 18 func ParseCID(raw string) (CID, error) { 19 + if raw == "" { 20 + return "", errors.New("expected CID, got empty string") 21 + } 19 22 if len(raw) > 256 { 20 - return "", fmt.Errorf("CID is too long (256 chars max)") 23 + return "", errors.New("CID is too long (256 chars max)") 21 24 } 22 25 if len(raw) < 8 { 23 - return "", fmt.Errorf("CID is too short (8 chars min)") 26 + return "", errors.New("CID is too short (8 chars min)") 24 27 } 25 28 26 29 if !cidRegex.MatchString(raw) { 27 - return "", fmt.Errorf("CID syntax didn't validate via regex") 30 + return "", errors.New("CID syntax didn't validate via regex") 28 31 } 29 32 if strings.HasPrefix(raw, "Qmb") { 30 - return "", fmt.Errorf("CIDv0 not allowed in this version of atproto") 33 + return "", errors.New("CIDv0 not allowed in this version of atproto") 31 34 } 32 35 return CID(raw), nil 33 36 }
+7 -3
atproto/syntax/datetime.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 24 25 var datetimeRegex = regexp.MustCompile(`^[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9](.[0-9]{1,20})?(Z|([+-][0-2][0-9]:[0-5][0-9]))$`) 25 26 26 27 func ParseDatetime(raw string) (Datetime, error) { 28 + if raw == "" { 29 + return "", errors.New("expected datetime, got empty string") 30 + } 27 31 if len(raw) > 64 { 28 - return "", fmt.Errorf("Datetime too long (max 64 chars)") 32 + return "", errors.New("Datetime too long (max 64 chars)") 29 33 } 30 34 31 35 if !datetimeRegex.MatchString(raw) { 32 - return "", fmt.Errorf("Datetime syntax didn't validate via regex") 36 + return "", errors.New("Datetime syntax didn't validate via regex") 33 37 } 34 38 if strings.HasSuffix(raw, "-00:00") { 35 - return "", fmt.Errorf("Datetime can't use '-00:00' for UTC timezone, must use '+00:00', per ISO-8601") 39 + return "", errors.New("Datetime can't use '-00:00' for UTC timezone, must use '+00:00', per ISO-8601") 36 40 } 37 41 // ensure that the datetime actually parses using golang time lib 38 42 _, err := time.Parse(time.RFC3339Nano, raw)
+6 -3
atproto/syntax/did.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 16 16 var didRegex = regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`) 17 17 18 18 func ParseDID(raw string) (DID, error) { 19 + if raw == "" { 20 + return "", errors.New("expected DID, got empty string") 21 + } 19 22 if len(raw) > 2*1024 { 20 - return "", fmt.Errorf("DID is too long (2048 chars max)") 23 + return "", errors.New("DID is too long (2048 chars max)") 21 24 } 22 25 if !didRegex.MatchString(raw) { 23 - return "", fmt.Errorf("DID syntax didn't validate via regex") 26 + return "", errors.New("DID syntax didn't validate via regex") 24 27 } 25 28 return DID(raw), nil 26 29 }
+5 -1
atproto/syntax/handle.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 21 22 type Handle string 22 23 23 24 func ParseHandle(raw string) (Handle, error) { 25 + if raw == "" { 26 + return "", errors.New("expected handle, got empty string") 27 + } 24 28 if len(raw) > 253 { 25 - return "", fmt.Errorf("handle is too long (253 chars max)") 29 + return "", errors.New("handle is too long (253 chars max)") 26 30 } 27 31 if !handleRegex.MatchString(raw) { 28 32 return "", fmt.Errorf("handle syntax didn't validate via regex: %s", raw)
+6 -3
atproto/syntax/language.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 15 15 var langRegex = regexp.MustCompile(`^(i|[a-z]{2,3})(-[a-zA-Z0-9]+)*$`) 16 16 17 17 func ParseLanguage(raw string) (Language, error) { 18 + if raw == "" { 19 + return "", errors.New("expected language code, got empty string") 20 + } 18 21 if len(raw) > 128 { 19 - return "", fmt.Errorf("Language is too long (128 chars max)") 22 + return "", errors.New("Language is too long (128 chars max)") 20 23 } 21 24 if !langRegex.MatchString(raw) { 22 - return "", fmt.Errorf("Language syntax didn't validate via regex") 25 + return "", errors.New("Language syntax didn't validate via regex") 23 26 } 24 27 return Language(raw), nil 25 28 }
+6 -3
atproto/syntax/nsid.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 16 16 type NSID string 17 17 18 18 func ParseNSID(raw string) (NSID, error) { 19 + if raw == "" { 20 + return "", errors.New("expected NSID, got empty string") 21 + } 19 22 if len(raw) > 317 { 20 - return "", fmt.Errorf("NSID is too long (317 chars max)") 23 + return "", errors.New("NSID is too long (317 chars max)") 21 24 } 22 25 if !nsidRegex.MatchString(raw) { 23 - return "", fmt.Errorf("NSID syntax didn't validate via regex") 26 + return "", errors.New("NSID syntax didn't validate via regex") 24 27 } 25 28 return NSID(raw), nil 26 29 }
+7 -4
atproto/syntax/recordkey.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 15 15 type RecordKey string 16 16 17 17 func ParseRecordKey(raw string) (RecordKey, error) { 18 + if raw == "" { 19 + return "", errors.New("expected record key, got empty string") 20 + } 18 21 if len(raw) > 512 { 19 - return "", fmt.Errorf("recordkey is too long (512 chars max)") 22 + return "", errors.New("recordkey is too long (512 chars max)") 20 23 } 21 24 if raw == "" || raw == "." || raw == ".." { 22 - return "", fmt.Errorf("recordkey can not be empty, '.', or '..'") 25 + return "", errors.New("recordkey can not be empty, '.', or '..'") 23 26 } 24 27 if !recordKeyRegex.MatchString(raw) { 25 - return "", fmt.Errorf("recordkey syntax didn't validate via regex") 28 + return "", errors.New("recordkey syntax didn't validate via regex") 26 29 } 27 30 return RecordKey(raw), nil 28 31 }
+6 -3
atproto/syntax/tid.go
··· 2 2 3 3 import ( 4 4 "encoding/base32" 5 - "fmt" 5 + "errors" 6 6 "regexp" 7 7 "strings" 8 8 "sync" ··· 27 27 var tidRegex = regexp.MustCompile(`^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$`) 28 28 29 29 func ParseTID(raw string) (TID, error) { 30 + if raw == "" { 31 + return "", errors.New("expected TID, got empty string") 32 + } 30 33 if len(raw) != 13 { 31 - return "", fmt.Errorf("TID is wrong length (expected 13 chars)") 34 + return "", errors.New("TID is wrong length (expected 13 chars)") 32 35 } 33 36 if !tidRegex.MatchString(raw) { 34 - return "", fmt.Errorf("TID syntax didn't validate via regex") 37 + return "", errors.New("TID syntax didn't validate via regex") 35 38 } 36 39 return TID(raw), nil 37 40 }
+6 -3
atproto/syntax/uri.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 13 13 type URI string 14 14 15 15 func ParseURI(raw string) (URI, error) { 16 + if raw == "" { 17 + return "", errors.New("expected URI, got empty string") 18 + } 16 19 if len(raw) > 8192 { 17 - return "", fmt.Errorf("URI is too long (8192 chars max)") 20 + return "", errors.New("URI is too long (8192 chars max)") 18 21 } 19 22 var uriRegex = regexp.MustCompile(`^[a-z][a-z.-]{0,80}:[[:graph:]]+$`) 20 23 if !uriRegex.MatchString(raw) { 21 - return "", fmt.Errorf("URI syntax didn't validate via regex") 24 + return "", errors.New("URI syntax didn't validate via regex") 22 25 } 23 26 return URI(raw), nil 24 27 }