Skip to content

Commit

Permalink
errs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 11, 2024
1 parent 5b0bf1e commit 1d9f39d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 7 additions & 0 deletions impl/pkg/server/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package server
import (
"crypto/ed25519"
"encoding/binary"
"fmt"
"io"
"net/http"
"strings"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -56,6 +58,11 @@ func (r *PkarrRouter) GetRecord(c *gin.Context) {

resp, err := r.service.GetPkarr(c, *id)
if err != nil {
// TODO(gabe): provide a more maintainable way to handle custom errors
if strings.Contains("spam", err.Error()) {
LoggingRespondErrMsg(c, fmt.Sprintf("too many requests for bad key %s", *id), http.StatusTooManyRequests)
return
}
LoggingRespondErrWithMsg(c, err, "failed to get pkarr record", http.StatusInternalServerError)
return
}
Expand Down
7 changes: 3 additions & 4 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"sync/atomic"
"time"

"github.com/goccy/go-json"
"github.com/tv42/zbase32"

ssiutil "github.com/TBD54566975/ssi-sdk/util"
"github.com/allegro/bigcache/v3"
"github.com/anacrolix/torrent/bencode"
"github.com/goccy/go-json"
"github.com/sirupsen/logrus"
"github.com/tv42/zbase32"

"github.com/TBD54566975/did-dht-method/internal/util"

Expand Down Expand Up @@ -139,7 +138,7 @@ func (s *PkarrService) GetPkarr(ctx context.Context, id string) (*pkarr.Response

// if the key is in the badGetCache, return an error
if _, err := s.badGetCache.Get(id); err == nil {
return nil, ssiutil.LoggingCtxErrorMsgf(ctx, err, "key [%s] looked up too frequently, please wait a bit before trying again", id)
return nil, ssiutil.LoggingCtxErrorMsgf(ctx, err, "bad key [%s] rate limited to prevent spam", id)
}

// first do a cache lookup
Expand Down
2 changes: 1 addition & 1 deletion impl/pkg/service/pkarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestPkarrService(t *testing.T) {

// try it again to make sure the cache is working
got, err = svc.GetPkarr(context.Background(), "uqaj3fcr9db6jg6o9pjs53iuftyj45r46aubogfaceqjbo6pp9sy")
assert.ErrorContains(t, err, "looked up too frequently, please wait a bit before trying again")
assert.ErrorContains(t, err, "rate limited to prevent spam")
assert.Empty(t, got)
})

Expand Down

0 comments on commit 1d9f39d

Please sign in to comment.