🥅 Correct error handling

This commit is contained in:
Ada 2023-12-28 13:01:21 +01:00
parent bc35ef6428
commit 5545dc9e12
3 changed files with 40 additions and 26 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -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)