Skip to content

Commit

Permalink
refactor: reduce folding ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
eiri committed Oct 30, 2024
1 parent 5b56094 commit 65fb094
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions base/error_response_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,31 @@ func transformError(resp *http.Response) error {
return nil
}

if _, ok := respError["trace"]; ok {
resp.Body = save
return nil
}

respErrorWasAugmented := false
if _, ok := respError["trace"]; !ok {
if _, ok := respError["errors"]; !ok {
err := make(map[string]string)
if m, ok := respError["error"]; ok {
err["code"] = m.(string)
err["message"] = m.(string)
if m, ok := respError["reason"]; ok && m != "" {
err["message"] += ": " + m.(string)
}
respError["errors"] = []map[string]string{err}
respErrorWasAugmented = true
if _, ok := respError["errors"]; !ok {
err := make(map[string]string)
if m, ok := respError["error"]; ok {
err["code"] = m.(string)
err["message"] = m.(string)
if m, ok := respError["reason"]; ok && m != "" {
err["message"] += ": " + m.(string)
}
}

trace := resp.Header.Get("X-Couch-Request-Id")
if _, ok := respError["errors"]; ok && trace != "" {
respError["trace"] = trace
respError["errors"] = []map[string]string{err}
respErrorWasAugmented = true
}
}

trace := resp.Header.Get("X-Couch-Request-Id")
if _, ok := respError["errors"]; ok && trace != "" {
respError["trace"] = trace
respErrorWasAugmented = true
}

if respErrorWasAugmented {
var newErrorJson bytes.Buffer
if err := json.NewEncoder(&newErrorJson).Encode(respError); err == nil {
Expand Down

0 comments on commit 65fb094

Please sign in to comment.