Skip to content

Commit

Permalink
remove github.com/pkg/errors and use internal error wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Mar 26, 2022
1 parent d24c4fb commit 0c79826
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 23 deletions.
3 changes: 1 addition & 2 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/sharding"
"github.com/disgoorg/log"
"github.com/pkg/errors"
)

func DefaultConfig(gatewayHandlers map[discord.GatewayEventType]GatewayEventHandler, httpHandler HTTPServerEventHandler) *Config {
Expand Down Expand Up @@ -166,7 +165,7 @@ func BuildClient(token string, config Config, gatewayEventHandlerFunc func(clien
}
id, err := tokenhelper.IDFromToken(token)
if err != nil {
return nil, errors.Wrap(err, "error while getting application id from token")
return nil, fmt.Errorf("error while getting application id from token: %w", err)
}
client := &clientImpl{
token: token,
Expand Down
5 changes: 3 additions & 2 deletions discord/error.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package discord

import (
"fmt"

"github.com/disgoorg/disgo/json"
"github.com/pkg/errors"
)

type ErrorCode int
Expand All @@ -22,7 +23,7 @@ func (e *APIError) UnmarshalJSON(data []byte) error {
Errors json.RawMessage `json:"errors"`
}
if err := json.Unmarshal(data, &v); err != nil {
return errors.Wrap(err, "error unmarshalling discord error")
return fmt.Errorf("error unmarshalling discord error: %w", err)
}

e.Code = v.Code
Expand Down
2 changes: 1 addition & 1 deletion discord/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package discord

import (
"github.com/pkg/errors"
"errors"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion discord/gateway_messages.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package discord

import (
"errors"

"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake"
"github.com/pkg/errors"
)

// GatewayMessage raw GatewayMessage type
Expand Down
3 changes: 1 addition & 2 deletions discord/timestamp.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package discord

import (
"errors"
"fmt"
"strconv"
"time"

"github.com/pkg/errors"
)

// ErrNoTimestampMatch is returned when no valid Timestamp is found in the Message
Expand Down
5 changes: 2 additions & 3 deletions gateway/gateway_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/disgoorg/disgo/json"
"github.com/disgoorg/disgo/rest/route"
"github.com/disgoorg/log"
"github.com/pkg/errors"

"github.com/gorilla/websocket"
)
Expand Down Expand Up @@ -175,7 +174,7 @@ func (g *gatewayImpl) ReOpen(ctx context.Context, delay time.Duration) error {

func (g *gatewayImpl) reOpen(ctx context.Context, try int, delay time.Duration) error {
if try >= g.config.MaxReconnectTries-1 {
return errors.Errorf("failed to reconnect. exceeded max reconnect tries of %d reached", g.config.MaxReconnectTries)
return fmt.Errorf("failed to reconnect. exceeded max reconnect tries of %d reached", g.config.MaxReconnectTries)
}
timer := time.NewTimer(time.Duration(try) * delay)
defer timer.Stop()
Expand Down Expand Up @@ -388,7 +387,7 @@ func (g *gatewayImpl) parseGatewayMessage(mt int, reader io.Reader) (*discord.Ga
g.Logger().Trace(g.formatLogs("binary message received. decompressing..."))
readCloser, err := zlib.NewReader(reader)
if err != nil {
return nil, errors.Wrap(err, "failed to decompress zlib")
return nil, fmt.Errorf("failed to decompress zlib: %w", err)
}
finalReadCloser = readCloser
} else {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/disgoorg/log v1.2.0
github.com/disgoorg/snowflake v1.1.0
github.com/gorilla/websocket v1.5.0
github.com/pkg/errors v0.9.1
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b
github.com/stretchr/testify v1.7.0
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/disgoorg/snowflake v1.1.0 h1:uVF9QqI31uqo0kzbZahwvjaYQxn40SUfFK5pCNzf
github.com/disgoorg/snowflake v1.1.0/go.mod h1:wUUTKWS1jSV0gIb3TSPsdJORNF8BcjKTjVeyrULEhr8=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
Expand Down
2 changes: 1 addition & 1 deletion oauth2/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package oauth2

import (
"errors"
"fmt"

"github.com/disgoorg/snowflake"
"github.com/pkg/errors"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest"
Expand Down
16 changes: 8 additions & 8 deletions rest/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package rest
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/disgoorg/disgo/json"
"github.com/pkg/errors"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest/route"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *clientImpl) retry(cRoute *route.CompiledAPIRoute, rqBody any, rsBody an
default:
contentType = "application/json"
if rawRqBody, err = json.Marshal(rqBody); err != nil {
return errors.Wrap(err, "failed to marshal request body")
return fmt.Errorf("failed to marshal request body: %w", err)
}
}
c.Logger().Tracef("request to %s, body: %s", rqURL, string(rawRqBody))
Expand Down Expand Up @@ -126,7 +126,7 @@ func (c *clientImpl) retry(cRoute *route.CompiledAPIRoute, rqBody any, rsBody an
// wait for rate limits
err = c.RateLimiter().WaitBucket(config.Ctx, cRoute)
if err != nil {
return errors.Wrap(err, "error locking bucket in rest client")
return fmt.Errorf("error locking bucket in rest client: %w", err)
}
rq = rq.WithContext(config.Ctx)

Expand All @@ -139,18 +139,18 @@ func (c *clientImpl) retry(cRoute *route.CompiledAPIRoute, rqBody any, rsBody an

rs, err := c.HTTPClient().Do(config.Request)
if err != nil {
return errors.Wrap(err, "error doing request in rest client")
return fmt.Errorf("error doing request in rest client: %w", err)
}

if err = c.RateLimiter().UnlockBucket(cRoute, rs.Header); err != nil {
// TODO: should we maybe retry here?
return errors.Wrap(err, "error unlocking bucket in rest client")
return fmt.Errorf("error unlocking bucket in rest client: %w", err)
}

var rawRsBody []byte
if rs.Body != nil {
if rawRsBody, err = ioutil.ReadAll(rs.Body); err != nil {
return errors.Wrap(err, "error reading response body in rest client")
return fmt.Errorf("error reading response body in rest client: %w", err)
}
c.Logger().Tracef("response from %s, code %d, body: %s", rqURL, rs.StatusCode, string(rawRsBody))
}
Expand All @@ -159,7 +159,7 @@ func (c *clientImpl) retry(cRoute *route.CompiledAPIRoute, rqBody any, rsBody an
case http.StatusOK, http.StatusCreated, http.StatusNoContent:
if rsBody != nil && rs.Body != nil {
if err = json.Unmarshal(rawRsBody, rsBody); err != nil {
wErr := errors.Wrap(err, "error unmarshalling response body")
wErr := fmt.Errorf("error unmarshalling response body: %w", err)
c.Logger().Error(wErr)
return NewErrorErr(rq, rawRqBody, rs, rawRsBody, wErr)
}
Expand All @@ -178,7 +178,7 @@ func (c *clientImpl) retry(cRoute *route.CompiledAPIRoute, rqBody any, rsBody an
default:
var v discord.APIError
if err = json.Unmarshal(rawRsBody, &v); err != nil {
return errors.Wrap(err, "error unmarshalling error response body")
return fmt.Errorf("error unmarshalling error response body: %w", err)
}
return NewErrorAPIErr(rq, rawRqBody, rs, rawRsBody, v)
}
Expand Down

0 comments on commit 0c79826

Please sign in to comment.