mirror of
https://github.com/aykhans/oh-my-url.git
synced 2025-04-08 15:04:01 +00:00
🐛 Fix error handling and logging
This commit is contained in:
parent
6265458bc3
commit
5af2714abb
@ -1,10 +1,10 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/aykhans/oh-my-url/app/errors"
|
||||
"github.com/aykhans/oh-my-url/app/utils"
|
||||
"github.com/gocql/gocql"
|
||||
)
|
||||
@ -58,11 +58,11 @@ func (c *Cassandra) CreateURL(url string) (string, error) {
|
||||
applied, err := c.db.Query(query, id, key, url, 0).Consistency(gocql.All).MapScanCAS(m)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return "", err
|
||||
return "", errors.ErrDBCreateURL
|
||||
}
|
||||
if !applied {
|
||||
log.Println("Failed to insert unique key")
|
||||
return "", errors.New("an error occurred, please try again later")
|
||||
return "", errors.ErrDBCreateURL
|
||||
}
|
||||
c.currentID.ID = id
|
||||
|
||||
@ -76,7 +76,11 @@ func (c *Cassandra) GetURL(key string) (string, error) {
|
||||
Consistency(gocql.One).
|
||||
Scan(&url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
log.Println(err)
|
||||
if err == gocql.ErrNotFound {
|
||||
return "", errors.ErrDBURLNotFound
|
||||
}
|
||||
return "", errors.ErrDBGetURL
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/aykhans/oh-my-url/app/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -9,10 +12,10 @@ type Postgres struct {
|
||||
}
|
||||
|
||||
type url struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Key string `gorm:"unique;not null;size:15;default:null"`
|
||||
URL string `gorm:"not null;default:null"`
|
||||
Count int `gorm:"not null;default:0"`
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Key string `gorm:"unique;not null;size:15;default:null"`
|
||||
URL string `gorm:"not null;default:null"`
|
||||
Count int `gorm:"not null;default:0"`
|
||||
}
|
||||
|
||||
func (p *Postgres) Init() {
|
||||
@ -30,7 +33,8 @@ func (p *Postgres) CreateURL(mainUrl string) (string, error) {
|
||||
url := url{URL: mainUrl}
|
||||
tx := p.gormDB.Create(&url)
|
||||
if tx.Error != nil {
|
||||
return "", tx.Error
|
||||
log.Println(tx.Error)
|
||||
return "", errors.ErrDBCreateURL
|
||||
}
|
||||
return url.Key, nil
|
||||
}
|
||||
@ -39,7 +43,11 @@ func (p *Postgres) GetURL(key string) (string, error) {
|
||||
var result url
|
||||
tx := p.gormDB.Where("key = ?", key).First(&result)
|
||||
if tx.Error != nil {
|
||||
return "", tx.Error
|
||||
log.Println(tx.Error)
|
||||
if tx.Error == gorm.ErrRecordNotFound {
|
||||
return "", errors.ErrDBURLNotFound
|
||||
}
|
||||
return "", errors.ErrDBGetURL
|
||||
}
|
||||
result.Count++
|
||||
p.gormDB.Save(&result)
|
||||
|
9
app/errors/api.go
Normal file
9
app/errors/api.go
Normal file
@ -0,0 +1,9 @@
|
||||
package errors
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrAPITemplateParsing = errors.New("an error occured while parsing template please try again later")
|
||||
ErrAPIInvalidURL = errors.New("invalid URL")
|
||||
ErrAPI503 = errors.New("service unavailable please try again later")
|
||||
)
|
9
app/errors/db.go
Normal file
9
app/errors/db.go
Normal file
@ -0,0 +1,9 @@
|
||||
package errors
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrDBCreateURL = errors.New("an error occured please try again later")
|
||||
ErrDBGetURL = errors.New("an error occured please try again later")
|
||||
ErrDBURLNotFound = errors.New("url not found")
|
||||
)
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
24
app/main.go
24
app/main.go
@ -1,11 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/aykhans/oh-my-url/app/config"
|
||||
"github.com/aykhans/oh-my-url/app/db"
|
||||
"github.com/aykhans/oh-my-url/app/http_handlers"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -27,11 +29,25 @@ func main() {
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
panic(http.ListenAndServe(":"+appConfig.LISTEN_PORT_CREATE, urlCreateMux))
|
||||
for {
|
||||
err := http.ListenAndServe(":"+appConfig.LISTEN_PORT_CREATE, urlCreateMux)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
panic(http.ListenAndServe(":"+appConfig.LISTEN_PORT_FORWARD, urlReadMux))
|
||||
for {
|
||||
err := http.ListenAndServe(":"+appConfig.LISTEN_PORT_FORWARD, urlReadMux)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
}
|
||||
|
@ -93,7 +93,7 @@
|
||||
<h1>Oh My URL!</h1>
|
||||
<form action="/" method="post">
|
||||
{{ if .Error }}
|
||||
<label for="urlInput" style="color: red;">{{ .Error }}</label>
|
||||
<label for="urlInput" style="color: red;">{{ .Error.Error }}</label>
|
||||
{{ end }}
|
||||
<input type="url" id="urlInput" name="url" placeholder="https://example.com/" value="{{ if .MainURL }}{{ .MainURL }}{{ end }}" required>
|
||||
{{ if .ShortedURL }}
|
||||
|
Loading…
x
Reference in New Issue
Block a user