Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log less #2718

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions libs/wallet/provider/uphold/uphold.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -236,25 +237,26 @@ func submit(
if err != nil {
return nil, resp, fmt.Errorf("%w: %s", errorutils.ErrFailedBodyRead, err.Error())
}
sbody := redactUnneededContent(string(body))

if logger != nil {
logger.Debug().
Str("path", "github.com/brave-intl/bat-go/wallet/provider/uphold").
Str("type", "http.Response").
Int("status", resp.StatusCode).
Str("headers", string(jsonHeaders)).
Msg(string(body))
Msg(sbody)
}

if resp.StatusCode/100 != 2 {
var uhErr upholdError
if json.Unmarshal(body, &uhErr) != nil {
return nil, resp, fmt.Errorf("Error %d, %s", resp.StatusCode, body)
if json.Unmarshal([]byte(sbody), &uhErr) != nil {
return nil, resp, fmt.Errorf("Error %d, %s", resp.StatusCode, sbody)
}
uhErr.RequestID = resp.Header.Get("Request-Id")
return nil, resp, uhErr
}
return body, resp, nil
return []byte(sbody), resp, nil
}

type createCardRequest struct {
Expand Down Expand Up @@ -1180,3 +1182,12 @@ func FundWallet(ctx context.Context, destWallet *Wallet, amount decimal.Decimal)

return balance.TotalProbi, nil
}

func redactUnneededContent(sbody string) string {
for _, p := range []string{`"description":\s*"[^"]+"\s*,?`, `"\w+Country":\s*"[^"]+"\s*,?`} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I would not advocate using globals but we are already do it in that package for regex here and vars here. Calling must which can potentially panic on every request plus the overhead seems unnecessary. In the loop we are also copying every time rather than indexing the value.

re := regexp.MustCompile(p)
sbody = re.ReplaceAllString(sbody, "")
}
sbody = strings.ReplaceAll(sbody, ",}", "}")
return sbody
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: return strings.ReplaceAll(sbody, ",}", "}")

}
12 changes: 12 additions & 0 deletions libs/wallet/provider/uphold/uphold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uphold
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"net/url"
Expand Down Expand Up @@ -351,3 +352,14 @@ func requireDonorWallet(t *testing.T) *Wallet {

return &Wallet{Info: info, PrivKey: privateKey, PubKey: publicKey}
}

func TestRedactUnneeded(t *testing.T) {
response := `{"description":"some unneeded content","UKCountry":"foo","TestCountry":"bar","NoMatch": true}`
result := `{"NoMatch": true}`
testValue := redactUnneededContent(response)
assert.Equal(t, result, testValue)
var dat map[string]interface{}
if err := json.Unmarshal([]byte(testValue), &dat); err != nil {
t.Fatal(err)
}
}
Loading