Centralizing error handling to a helpers package

This commit is contained in:
Muyao CHEN
2024-07-03 10:03:25 +02:00
parent 0c0159734e
commit 9fc6c05d38
7 changed files with 65 additions and 20 deletions

View File

@ -5,9 +5,9 @@ import (
"errors"
"fmt"
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/helpers"
"go-udemy-web-1/internal/models"
"html/template"
"log"
"net/http"
"path/filepath"
@ -59,14 +59,14 @@ func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *mod
err := t.Execute(buf, td)
if err != nil {
log.Println(err)
helpers.ServerError(w, err)
return err
}
// render the template
_, err = buf.WriteTo(w)
if err != nil {
log.Println(err)
helpers.ServerError(w, err)
return err
}

View File

@ -4,6 +4,7 @@ import (
"encoding/gob"
"go-udemy-web-1/internal/config"
"go-udemy-web-1/internal/models"
"log"
"net/http"
"os"
"testing"
@ -32,6 +33,12 @@ func TestMain(m *testing.M) {
testApp.Session = session
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
testApp.InfoLog = infoLog
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
testApp.ErrorLog = errorLog
app = &testApp
os.Exit(m.Run())