Skip to content
Snippets Groups Projects
Verified Commit 7b3eb892 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Improve error reporting

parent 291db9db
No related branches found
No related tags found
No related merge requests found
......@@ -17,8 +17,8 @@ type ImageDetailData struct {
func pageImageDetail(ctx PageContext) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := parseUser(r)
_, imageId := path.Split(r.URL.Path)
result, err := ctx.Database.Query(`
SELECT
id,
......@@ -32,15 +32,20 @@ func pageImageDetail(ctx PageContext) http.Handler {
WHERE id = $1
`, imageId)
if err != nil {
panic(err)
fmt.Printf("An error occured: %s", err.Error())
_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
return
}
var info Image
if result.Next() {
var owner string
err := result.Scan(&info.Id, &owner, &info.Title, &info.Description, &info.CreatedAt, &info.OriginalName, &info.MimeType)
if err != nil {
panic(err)
fmt.Printf("An error occured: %s", err.Error())
_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
return
}
switch r.PostFormValue("action") {
......@@ -53,10 +58,12 @@ func pageImageDetail(ctx PageContext) http.Handler {
user.Id,
)
if err != nil {
panic(err)
fmt.Printf("An error occured: %s", err.Error())
_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
return
}
if r.PostFormValue("from_js") == "true" {
returnJson(w, true)
_ = returnJson(w, true)
} else {
http.Redirect(w, r, r.URL.Path, http.StatusFound)
}
......@@ -64,12 +71,16 @@ func pageImageDetail(ctx PageContext) http.Handler {
case "delete":
_, err = ctx.Database.Exec("DELETE FROM images WHERE id = $1 AND owner = $2", info.Id, user.Id)
if err != nil {
panic(err)
fmt.Printf("An error occured: %s", err.Error())
_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
return
}
for _, definition := range ctx.Config.Sizes {
err := os.Remove(path.Join(ctx.Config.TargetFolder, fmt.Sprintf("%s%s", info.Id, definition.Suffix)))
if err != nil && !os.IsNotExist(err) {
panic(err)
fmt.Printf("An error occured: %s", err.Error())
_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
return
}
}
http.Redirect(w, r, "/me/images", http.StatusFound)
......@@ -86,7 +97,6 @@ func pageImageDetail(ctx PageContext) http.Handler {
return
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Image not found")
_ = returnError(w, http.StatusNotFound, "Image Not Found")
})
}
......@@ -73,6 +73,15 @@ func returnJson(w http.ResponseWriter, data interface{}) error {
return nil
}
func returnError(w http.ResponseWriter, code int, message string) error {
w.WriteHeader(code)
if _, err := w.Write([]byte(message)); err != nil {
return err
}
return nil
}
func formatTemplate(w http.ResponseWriter, templateName string, data interface{}) error {
pageTemplate, err := template.ParseFiles(
"templates/_base.html",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment