An allen wichtigen Stellen fmt.Errorf() durch http.Error() ersetzt

This commit is contained in:
Jason Streifling 2023-10-17 18:37:31 +02:00
parent 9bdc6e9f43
commit c22647edd9

View File

@ -25,7 +25,7 @@ func DisplayTable(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetAllOverviewTableData() bs, err := db.GetAllOverviewTableData()
if err != nil { if err != nil {
_ = fmt.Errorf("DisplayTable: *DB.GetAllOverviewTableData(): %v\n", err) http.Error(w, "DisplayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
} }
@ -33,9 +33,9 @@ func DisplayTable(db *data.DB) http.HandlerFunc {
func DisplaySearchResults(db *data.DB) http.HandlerFunc { func DisplaySearchResults(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.ReadByName(r.PostFormValue("search")) bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
if err != nil { if err != nil {
_ = fmt.Errorf("DisplayResults: db.ReadByName(r.PostFormValue()): %v\n", err) http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs) template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
} }
@ -47,21 +47,25 @@ func DisplayForm(i *int64) http.HandlerFunc {
} }
} }
func generateUUID() string { func generateUUID() (string, error) {
bs := make([]byte, 2) bs := make([]byte, 2)
if _, err := rand.Read(bs); err != nil { if _, err := rand.Read(bs); err != nil {
_ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err) return "", fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
return ""
} }
return hex.EncodeToString(bs) return hex.EncodeToString(bs), nil
} }
func AddParticipant(i *int64, ls *[]string) http.HandlerFunc { func AddParticipant(i *int64, ls *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
uuid, err := generateUUID()
if err != nil {
http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
}
*i++ *i++
login := fmt.Sprintf("%d", *i) + "-" + generateUUID() login := fmt.Sprintf("%d", *i) + "-" + uuid
(*ls) = append(*ls, login) (*ls) = append(*ls, login)
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login)
} }