From 10772f828902987e4a52cd68c29f1e450db1252c Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 14 Oct 2014 16:12:50 +0100 Subject: [PATCH] Better error-checking --- server/http.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server/http.go b/server/http.go index 35697c5c..910651fb 100644 --- a/server/http.go +++ b/server/http.go @@ -37,12 +37,20 @@ func ListenHttp(db Zone, port int) { } weave_cidr := weave_ipstr + "/" + prefix weave_ip, subnet, err := net.ParseCIDR(weave_cidr) - if err == nil { - log.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr) - db.AddRecord(identifier, name, ip, weave_ip, subnet) - } else if err != nil { + if err != nil { log.Printf("Invalid CIDR in request: %s", weave_cidr) + http.Error(w, fmt.Sprintf("Invalid CIDR: %s", weave_cidr), http.StatusBadRequest) + return } + log.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr) + err = db.AddRecord(identifier, name, ip, weave_ip, subnet) + if err != nil { + http.Error(w, err.Error(), http.StatusConflict) + return + } + default: + log.Println("Unexpected http method", r.Method) + http.Error(w, "Unexpected http method: "+r.Method, http.StatusBadRequest) } }) address := fmt.Sprintf(":%d", port)