feat: better error handling

This commit is contained in:
Tom Andrade 2022-05-17 14:15:33 +02:00
parent ae557079b7
commit 267d2805ab
Signed by: wolvie
GPG Key ID: 31AAB07872E82669

View File

@ -57,6 +57,9 @@ var (
Path string = "" Path string = ""
// DumpFile is the file to dump URL data // DumpFile is the file to dump URL data
DumpFile string = "urls.json" DumpFile string = "urls.json"
// Error Definitions
ErrBadRequest = fmt.Errorf("bad request")
ErrNotFound = fmt.Errorf("not found")
) )
// get executes the GET command // get executes the GET command
@ -98,7 +101,7 @@ func redirect(k string) (string, error) {
key := rgx.FindString(k) key := rgx.FindString(k)
key, status := get(key) key, status := get(key)
if !status { if !status {
return "", fmt.Errorf("Not Found") return "", ErrNotFound
} }
u, _ := url.Parse(key) u, _ := url.Parse(key)
if u.Scheme == "" { if u.Scheme == "" {
@ -115,8 +118,9 @@ func shortener(u []byte, s int) (string, error) {
us := string(u) us := string(u)
if !govalidator.IsURL(string(us)) { if !govalidator.IsURL(string(us)) {
return su, fmt.Errorf("Bad Request") return su, fmt.Errorf("Bad Request")
return su, ErrBadRequest
} }
pu, _ := url.Parse(us) pu, _ := url.Parse(u)
for { for {
su = randStringBytesMaskImprSrc(s) su = randStringBytesMaskImprSrc(s)