🐛 Fix error handling and logging

This commit is contained in:
2024-03-13 17:26:30 +04:00
parent 6265458bc3
commit 5af2714abb
8 changed files with 92 additions and 25 deletions

View File

@ -23,3 +23,8 @@ func InternalServerError(w http.ResponseWriter, err error) {
log.Fatal(err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
func Error503(w http.ResponseWriter, err error) {
log.Println(err)
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
}

View File

@ -1,18 +1,21 @@
package httpHandlers
import (
"github.com/aykhans/oh-my-url/app/utils"
"github.com/aykhans/oh-my-url/app/config"
"html/template"
"log"
"net/http"
netUrl "net/url"
"regexp"
"github.com/aykhans/oh-my-url/app/config"
"github.com/aykhans/oh-my-url/app/errors"
"github.com/aykhans/oh-my-url/app/utils"
)
type CreateData struct {
ShortedURL string
MainURL string
Error string
Error error
}
func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
@ -23,7 +26,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles(utils.GetTemplatePaths("index.html")...)
if err != nil {
InternalServerError(w, err)
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
return
}
@ -31,7 +35,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
case http.MethodGet:
err = tmpl.Execute(w, nil)
if err != nil {
InternalServerError(w, err)
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
return
}
case http.MethodPost:
@ -41,23 +46,33 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
if !isValidUrl {
data := CreateData{
MainURL: url,
Error: "Invalid URL",
Error: errors.ErrAPIInvalidURL,
}
err = tmpl.Execute(w, data)
if err != nil {
InternalServerError(w, err)
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
}
return
}
key, err := hl.DB.CreateURL(url)
if err != nil {
InternalServerError(w, err)
data := CreateData{
MainURL: url,
Error: errors.ErrAPI503,
}
err = tmpl.Execute(w, data)
if err != nil {
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
}
return
}
shortedURL, err := netUrl.JoinPath(config.GetForwardDomain(), key)
if err != nil {
InternalServerError(w, err)
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
return
}
data := CreateData{
@ -66,7 +81,8 @@ func (hl *HandlerCreate) UrlCreate(w http.ResponseWriter, r *http.Request) {
}
err = tmpl.Execute(w, data)
if err != nil {
InternalServerError(w, err)
log.Println(err)
InternalServerError(w, errors.ErrAPITemplateParsing)
return
}