Add missing error returns

This commit is contained in:
Jan-Lukas Else 2020-11-22 20:30:02 +01:00
parent 954c3be6c3
commit 899caf9aaa
4 changed files with 18 additions and 10 deletions

View File

@ -116,18 +116,20 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) {
var p *post
if ct := r.Header.Get(contentType); strings.Contains(ct, contentTypeWWWForm) || strings.Contains(ct, contentTypeMultipartForm) {
var err error
r.ParseForm()
if strings.Contains(ct, contentTypeMultipartForm) {
err := r.ParseMultipartForm(0)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = r.ParseMultipartForm(0)
} else {
err = r.ParseForm()
}
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if action := micropubAction(r.Form.Get("action")); action != "" {
u, err := url.Parse(r.Form.Get("url"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if action == actionDelete {
micropubDelete(w, r, u)
@ -152,6 +154,7 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(parsedMfItem.URL)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if parsedMfItem.Action == actionDelete {
micropubDelete(w, r, u)
@ -175,6 +178,7 @@ func serveMicropubPost(w http.ResponseWriter, r *http.Request) {
}
if !strings.Contains(r.Context().Value("scope").(string), "create") {
http.Error(w, "create scope missing", http.StatusForbidden)
return
}
err := p.create()
if err != nil {
@ -412,19 +416,20 @@ func (p *post) computeExtraPostParameters() error {
func micropubDelete(w http.ResponseWriter, r *http.Request, u *url.URL) {
if !strings.Contains(r.Context().Value("scope").(string), "delete") {
http.Error(w, "delete scope missing", http.StatusForbidden)
return
}
err := deletePost(u.Path)
if err != nil {
if err := deletePost(u.Path); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusNoContent)
return
}
w.WriteHeader(http.StatusNoContent)
return
}
func micropubUpdate(w http.ResponseWriter, r *http.Request, u *url.URL, mf *microformatItem) {
if !strings.Contains(r.Context().Value("scope").(string), "update") {
http.Error(w, "update scope missing", http.StatusForbidden)
return
}
p, err := getPost(u.Path)
if err != nil {

View File

@ -23,6 +23,7 @@ const micropubMediaSubPath = "/media"
func serveMicropubMedia(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Context().Value("scope").(string), "media") {
http.Error(w, "media scope missing", http.StatusForbidden)
return
}
if appConfig.Micropub.MediaStorage == nil {
http.Error(w, "Not configured", http.StatusNotImplemented)

View File

@ -234,6 +234,7 @@ func render(w http.ResponseWriter, template string, data *renderData) {
err := templates[template].ExecuteTemplate(&buffer, template, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set content type (needed for minification middleware
w.Header().Set(contentType, contentTypeHTMLUTF8)

View File

@ -14,6 +14,7 @@ func serveSitemap(w http.ResponseWriter, r *http.Request) {
posts, err := getPosts(&postsRequestConfig{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sm := sitemap.New()
sm.Minify = true