🥅 Correct error handling
This commit is contained in:
parent
bc35ef6428
commit
5545dc9e12
3 changed files with 40 additions and 26 deletions
6
main.go
6
main.go
|
@ -62,8 +62,10 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
url := "/" + GenerateUrl()
|
url := "/" + GenerateUrl()
|
||||||
content := r.FormValue("content")
|
content := r.FormValue("content")
|
||||||
rawExpiration := r.FormValue("exp")
|
rawExpiration := r.FormValue("exp")
|
||||||
expiration := parseExpiration(rawExpiration)
|
expiration, err := ParseExpiration(rawExpiration)
|
||||||
if expiration == 0 {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
} else if expiration == 0 {
|
||||||
insertPaste(url, content, secret, -1)
|
insertPaste(url, content, secret, -1)
|
||||||
} else if expiration == -1 {
|
} else if expiration == -1 {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
|
50
utils.go
50
utils.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -37,44 +38,55 @@ func VerifySecret(url string, secret string) bool {
|
||||||
return secret == db.HGet(ctx, url, "secret").Val()
|
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 value int
|
||||||
var err error
|
var err error
|
||||||
if strings.Contains(*source, sep) {
|
if strings.Contains(*source, sep) {
|
||||||
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
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
|
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:], "")
|
*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 expiration int
|
||||||
|
var tempOutput int
|
||||||
|
var err error
|
||||||
|
errMessage := "ParseExpiration : \"%s\" : invalid syntax"
|
||||||
if source == "0" {
|
if source == "0" {
|
||||||
return 0
|
return 0, nil
|
||||||
}
|
}
|
||||||
expiration = 86400 * parseIntBeforeSeparator(&source, "d")
|
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
||||||
if expiration < 0 {
|
expiration = tempOutput * 86400
|
||||||
return -1
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
}
|
}
|
||||||
expiration += 3600 * parseIntBeforeSeparator(&source, "h")
|
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
||||||
if expiration < 0 {
|
expiration += tempOutput * 3600
|
||||||
return -1
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
}
|
}
|
||||||
expiration += 60 * parseIntBeforeSeparator(&source, "m")
|
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
||||||
if expiration < 0 {
|
expiration += tempOutput * 60
|
||||||
return -1
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
}
|
}
|
||||||
expiration += parseIntBeforeSeparator(&source, "s")
|
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
||||||
if expiration < 0 {
|
expiration += tempOutput * 1
|
||||||
return -1
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return -1, fmt.Errorf(errMessage, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
return expiration
|
return expiration, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package main
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
result := parseExpiration("2d1h3m47s")
|
result, _ := ParseExpiration("2d1h3m47s")
|
||||||
correctValue := 176627
|
correctValue := 176627
|
||||||
if result != correctValue {
|
if result != correctValue {
|
||||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
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
|
func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
result := parseExpiration("1h47s")
|
result, _ := ParseExpiration("1h47s")
|
||||||
correctValue := 3647
|
correctValue := 3647
|
||||||
if result != correctValue {
|
if result != correctValue {
|
||||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
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
|
func TestParseExpirationNull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
result := parseExpiration("0")
|
result, _ := ParseExpiration("0")
|
||||||
correctValue := 0
|
correctValue := 0
|
||||||
if result != correctValue {
|
if result != correctValue {
|
||||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
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
|
func TestParseExpirationNegative(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
result := parseExpiration("-42h1m1d4s")
|
result, _ := ParseExpiration("-42h1m4s")
|
||||||
correctValue := -1
|
correctValue := -1
|
||||||
if result != correctValue {
|
if result != correctValue {
|
||||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
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
|
func TestParseExpirationInvalid(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||||
result := parseExpiration("8h42h1m1d4s")
|
result, _ := ParseExpiration("8h42h1m1d4s")
|
||||||
correctValue := -1
|
correctValue := -1
|
||||||
if result != correctValue {
|
if result != correctValue {
|
||||||
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
|
||||||
|
|
Loading…
Reference in a new issue