From 5545dc9e12b6c0950de1da957c6e4339c27767c7 Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 28 Dec 2023 13:01:21 +0100 Subject: [PATCH] :goal_net: Correct error handling --- main.go | 6 ++++-- utils.go | 50 +++++++++++++++++++++++++++++++------------------- utils_test.go | 10 +++++----- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index c72ed78..3064b46 100644 --- a/main.go +++ b/main.go @@ -62,8 +62,10 @@ func handleRequest(w http.ResponseWriter, r *http.Request) { url := "/" + GenerateUrl() content := r.FormValue("content") rawExpiration := r.FormValue("exp") - expiration := parseExpiration(rawExpiration) - if expiration == 0 { + expiration, err := ParseExpiration(rawExpiration) + if err != nil { + log.Println(err) + } else if expiration == 0 { insertPaste(url, content, secret, -1) } else if expiration == -1 { w.WriteHeader(http.StatusBadRequest) diff --git a/utils.go b/utils.go index 7fd630c..5d23dce 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package main import ( "crypto/rand" "encoding/hex" + "fmt" "log" mathrand "math/rand" "strconv" @@ -37,44 +38,55 @@ func VerifySecret(url string, secret string) bool { return secret == db.HGet(ctx, url, "secret").Val() } -func parseIntBeforeSeparator(source *string, sep string) int { // return -1 if error, only accept positive number +func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return -1 if error, only accept positive number var value int var err error if strings.Contains(*source, sep) { value, err = strconv.Atoi(strings.Split(*source, sep)[0]) if err != nil { log.Println(err) - return -1 + return -1, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : cannot parse value as int", *source) } if value < 0 { // Only positive value is correct - return -1 + return -1, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : format only take positive value", *source) } *source = strings.Join(strings.Split(*source, sep)[1:], "") } - return value + return value, nil } -func parseExpiration(source string) int { // return -1 if error +func ParseExpiration(source string) (int, error) { // return -1 if error var expiration int + var tempOutput int + var err error + errMessage := "ParseExpiration : \"%s\" : invalid syntax" if source == "0" { - return 0 + return 0, nil } - expiration = 86400 * parseIntBeforeSeparator(&source, "d") - if expiration < 0 { - return -1 + tempOutput, err = parseIntBeforeSeparator(&source, "d") + expiration = tempOutput * 86400 + if err != nil { + log.Println(err) + return -1, fmt.Errorf(errMessage, source) } - expiration += 3600 * parseIntBeforeSeparator(&source, "h") - if expiration < 0 { - return -1 + tempOutput, err = parseIntBeforeSeparator(&source, "h") + expiration += tempOutput * 3600 + if err != nil { + log.Println(err) + return -1, fmt.Errorf(errMessage, source) } - expiration += 60 * parseIntBeforeSeparator(&source, "m") - if expiration < 0 { - return -1 + tempOutput, err = parseIntBeforeSeparator(&source, "m") + expiration += tempOutput * 60 + if err != nil { + log.Println(err) + return -1, fmt.Errorf(errMessage, source) } - expiration += parseIntBeforeSeparator(&source, "s") - if expiration < 0 { - return -1 + tempOutput, err = parseIntBeforeSeparator(&source, "s") + expiration += tempOutput * 1 + if err != nil { + log.Println(err) + return -1, fmt.Errorf(errMessage, source) } - return expiration + return expiration, nil } diff --git a/utils_test.go b/utils_test.go index f25fe37..3588315 100644 --- a/utils_test.go +++ b/utils_test.go @@ -3,7 +3,7 @@ package main import "testing" func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator - result := parseExpiration("2d1h3m47s") + result, _ := ParseExpiration("2d1h3m47s") correctValue := 176627 if result != correctValue { t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result) @@ -11,7 +11,7 @@ func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with al } func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator - result := parseExpiration("1h47s") + result, _ := ParseExpiration("1h47s") correctValue := 3647 if result != correctValue { t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result) @@ -19,7 +19,7 @@ func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with } func TestParseExpirationNull(t *testing.T) { // test parseExpirationFull with all valid separator - result := parseExpiration("0") + result, _ := ParseExpiration("0") correctValue := 0 if result != correctValue { t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result) @@ -27,7 +27,7 @@ func TestParseExpirationNull(t *testing.T) { // test parseExpirationFull with al } func TestParseExpirationNegative(t *testing.T) { // test parseExpirationFull with all valid separator - result := parseExpiration("-42h1m1d4s") + result, _ := ParseExpiration("-42h1m4s") correctValue := -1 if result != correctValue { t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result) @@ -35,7 +35,7 @@ func TestParseExpirationNegative(t *testing.T) { // test parseExpirationFull wit } func TestParseExpirationInvalid(t *testing.T) { // test parseExpirationFull with all valid separator - result := parseExpiration("8h42h1m1d4s") + result, _ := ParseExpiration("8h42h1m1d4s") correctValue := -1 if result != correctValue { t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)