Clean up errors in micropub.go

This commit is contained in:
Peter Stuifzand 2019-03-23 20:53:52 +01:00
parent 96bade5a53
commit 3f96dcfde3
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -34,7 +34,11 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := h.pool.Get() conn := h.pool.Get()
defer conn.Close() defer conn.Close()
r.ParseForm() err := r.ParseForm()
if err != nil {
http.Error(w, "bad request", 400)
return
}
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
// show profile with endpoint urls // show profile with endpoint urls
@ -49,36 +53,36 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err := redis.String(conn.Do("HGET", "sources", sourceID)) channel, err := redis.String(conn.Do("HGET", "sources", sourceID))
if err != nil { if err != nil {
channel, err = redis.String(conn.Do("HGET", "token:"+sourceID, "channel")) channel, err = redis.String(conn.Do("HGET", "token:"+sourceID, "channel"))
if err != nil { if err != nil {
http.Error(w, "Unknown source", 400) http.Error(w, "could not find source", 400)
return return
} }
} }
var item microsub.Item var item microsub.Item
ok := false ok := false
if r.Header.Get("Content-Type") == "application/jf2+json" { contentType := r.Header.Get("Content-Type")
if contentType == "application/jf2+json" {
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)
err := dec.Decode(&item) err := dec.Decode(&item)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400) http.Error(w, fmt.Sprintf("could not decode request body as jf2: %v", err), 400)
return return
} }
ok = true ok = true
} else if r.Header.Get("Content-Type") == "application/json" { } else if contentType == "application/json" {
var mfItem microformats.Microformat var mfItem microformats.Microformat
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)
err := dec.Decode(&mfItem) err := dec.Decode(&mfItem)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400) http.Error(w, fmt.Sprintf("could not decode request body as json: %v", err), 400)
return return
} }
author := microsub.Card{} author := microsub.Card{}
item, ok = jf2.SimplifyMicroformatItem(&mfItem, author) item, ok = jf2.SimplifyMicroformatItem(&mfItem, author)
} else if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { } else if contentType == "application/x-www-form-urlencoded" {
content := r.FormValue("content") content := r.FormValue("content")
name := r.FormValue("name") name := r.FormValue("name")
item.Type = "entry" item.Type = "entry"
@ -87,7 +91,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
item.Published = time.Now().Format(time.RFC3339) item.Published = time.Now().Format(time.RFC3339)
ok = true ok = true
} else { } else {
http.Error(w, "Unsupported Content-Type", 400) http.Error(w, fmt.Sprintf("content-type %s is not supported", contentType), 400)
return return
} }
@ -98,7 +102,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err = h.Backend.channelAddItemWithMatcher(channel, item) err = h.Backend.channelAddItemWithMatcher(channel, item)
err = h.Backend.updateChannelUnreadCount(channel) err = h.Backend.updateChannelUnreadCount(channel)
if err != nil { if err != nil {
log.Printf("error: while updating channel unread count for %s: %s\n", channel, err) log.Printf("could not update channel unread content %s: %v", channel, err)
} }
} }