Skip to content

Commit

Permalink
handle empty JSON responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Rios committed Oct 11, 2023
1 parent 268f787 commit cf5cfc9
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ func newHandleFunc(fs afero.Fs, path string, r Response) func(writer http.Respon
writer.WriteHeader(r.Status)
}

tmpl, err := parseFile(fs, path, r.JsonBody)
if err != nil {
log.Error(err)
writer.WriteHeader(500)
_, _ = writer.Write([]byte(err.Error()))
return
}
if r.Latency != "" {
duration, err := time.ParseDuration(r.Latency)
if err != nil {
Expand All @@ -32,11 +25,22 @@ func newHandleFunc(fs afero.Fs, path string, r Response) func(writer http.Respon
time.Sleep(duration)
}

writer.Header().Add("Content-Type", "application/json")
err = tmpl.Execute(writer, mux.Vars(request))
if err != nil {
log.Error(err)
if r.JsonBody != "" {
tmpl, err := parseFile(fs, path, r.JsonBody)
if err != nil {
log.Error(err)
writer.WriteHeader(500)
_, _ = writer.Write([]byte(err.Error()))
return
}

writer.Header().Add("Content-Type", "application/json")
err = tmpl.Execute(writer, mux.Vars(request))
if err != nil {
log.Error(err)
}
}

}
}

Expand Down

0 comments on commit cf5cfc9

Please sign in to comment.