Skip to content

Commit

Permalink
Merge pull request #7 from kumina/fix-codes
Browse files Browse the repository at this point in the history
Update response error codes.
  • Loading branch information
BartVerc authored Nov 2, 2021
2 parents 2f64424 + c5e27c2 commit 9b892a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Router struct {
}

type LogLine struct {
Timestamp time.Time `json:"timestamp"`
Timestamp time.Time `json:"time"`
Msg string `json:"msg,omitempty"`
Level string `json:"level"`
Details map[string]string `json:"details,inline"`
Expand Down Expand Up @@ -143,26 +143,26 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
})
if err != nil {
Error("Requestbody marshalling error.").LogJson(map[string]string{"url": fullUrl})
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusInternalServerError)
return
}

resp, err := Client.Post(a.dynamicHeaderUrl, "application/json", bytes.NewBuffer(requestBody))

if err != nil {
//a.log.Error("Request error.")
rw.WriteHeader(http.StatusNotFound)
Info(fmt.Sprintf("Request to %s failed", a.dynamicHeaderUrl)).LogJson(map[string]string{"url": fullUrl})
rw.WriteHeader(http.StatusInternalServerError)
return
}
if resp.StatusCode != 200 {
if resp.StatusCode == 409 {
Info("Ambiguous request.").LogJson(map[string]string{"url": fullUrl})
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusConflict)
return
}
Info("Unknown status code response from DynamicHeaderUrl.").
LogJson(map[string]string{"url": fullUrl, "code": strconv.Itoa(resp.StatusCode)})
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusNotImplemented)
return
}

Expand All @@ -171,14 +171,14 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Info("Could not read requests body.").LogJson(map[string]string{"url": fullUrl})
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusInternalServerError)
return
}
requested := &Requested{}
err = json.Unmarshal(body, requested)
if err != nil {
Info("Could not unmarshal requests body.").LogJson(map[string]string{"url": fullUrl})
rw.WriteHeader(http.StatusNotFound)
rw.WriteHeader(http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -216,7 +216,7 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
//a.log.WithField("path", newpath).Warn("Could not rewrite Path.")
continue
}
Warn("Apply rewrite.").LogJson(map[string]string{"url": fullUrl,
Info("Apply rewrite.").LogJson(map[string]string{"url": fullUrl,
"old_path": path, "new_path": string(newpath)})
req.RequestURI = req.URL.RequestURI()
break
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestRouter(t *testing.T) {

handler.ServeHTTP(recorder, req)

assertCode(t, recorder, 404)
assertCode(t, recorder, 409)
fmt.Println(req.RequestURI)
}

Expand Down

0 comments on commit 9b892a8

Please sign in to comment.