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

@ -0,0 +1,26 @@
package helpers
import (
"fmt"
"go-udemy-web-1/internal/config"
"net/http"
"runtime/debug"
)
var app *config.AppConfig
// NewHelpers sets up app config for helpers
func NewHelpers(a *config.AppConfig) {
app = a
}
func ClientError(w http.ResponseWriter, status int) {
app.InfoLog.Println("Client error with status of", status)
http.Error(w, http.StatusText(status), status)
}
func ServerError(w http.ResponseWriter, err error) {
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
app.ErrorLog.Println(trace)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}