43 lines
972 B
Go
43 lines
972 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"github.com/go-playground/form/v4"
|
|
)
|
|
|
|
func (app *application) serverError(w http.ResponseWriter, err error) {
|
|
trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
|
|
app.errorLog.Output(2, trace)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
}
|
|
|
|
func (app *application) clientError(w http.ResponseWriter, status int) {
|
|
http.Error(w, http.StatusText(status), status)
|
|
}
|
|
|
|
func (app *application) notFound(w http.ResponseWriter) {
|
|
app.clientError(w, http.StatusNotFound)
|
|
}
|
|
|
|
func (app *application) decodePostForm(r *http.Request, snippetForm any) error {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = app.formDecoder.Decode(&snippetForm, r.PostForm)
|
|
if err != nil {
|
|
var invalidDecoderError *form.InvalidDecoderError
|
|
if errors.As(err, &invalidDecoderError) {
|
|
panic("Invalid decoder error")
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|