Skip to content

Commit 63321ca

Browse files
committed
chore: replace github.com/pkg/errors with native error package
1 parent d72831c commit 63321ca

10 files changed

Lines changed: 143 additions & 143 deletions

File tree

acl/acl.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ package acl
88
import (
99
"context"
1010
"encoding/json"
11+
"errors"
1112
"fmt"
1213
"strings"
1314
"time"
1415

1516
"github.com/golang/glog"
16-
"github.com/pkg/errors"
1717
"github.com/spf13/viper"
1818

1919
"github.com/dgraph-io/dgo/v240"
@@ -26,7 +26,7 @@ func getUserAndGroup(conf *viper.Viper) (userId string, groupId string, err erro
2626
groupId = conf.GetString("group")
2727
if (len(userId) == 0 && len(groupId) == 0) ||
2828
(len(userId) != 0 && len(groupId) != 0) {
29-
return "", "", errors.Errorf("one of the --user or --group must be specified, but not both")
29+
return "", "", errors.New("one of the --user or --group must be specified, but not both")
3030
}
3131
return userId, groupId, nil
3232
}
@@ -47,10 +47,10 @@ func checkForbiddenOpts(conf *viper.Viper, forbiddenOpts []string) error {
4747
case bool:
4848
isSet = conf.GetBool(opt)
4949
default:
50-
return errors.Errorf("unexpected option type for %s", opt)
50+
return fmt.Errorf("unexpected option type for %s", opt)
5151
}
5252
if isSet {
53-
return errors.Errorf("the option --%s should not be set", opt)
53+
return fmt.Errorf("the option --%s should not be set", opt)
5454
}
5555
}
5656

@@ -77,7 +77,7 @@ func add(conf *viper.Viper) error {
7777
func userAdd(conf *viper.Viper, userid string, password string) error {
7878
dc, cancel, err := getClientWithAdminCtx(conf)
7979
if err != nil {
80-
return errors.Wrapf(err, "unable to get admin context")
80+
return fmt.Errorf("unable to get admin context: %w", err)
8181
}
8282
defer cancel()
8383

@@ -100,10 +100,10 @@ func userAdd(conf *viper.Viper, userid string, password string) error {
100100

101101
user, err := queryUser(ctx, txn, userid)
102102
if err != nil {
103-
return errors.Wrapf(err, "while querying user")
103+
return fmt.Errorf("while querying user: %w", err)
104104
}
105105
if user != nil {
106-
return errors.Errorf("unable to create user because of conflict: %v", userid)
106+
return fmt.Errorf("unable to create user because of conflict: %v", userid)
107107
}
108108

109109
createUserNQuads := CreateUserNQuads(userid, password)
@@ -114,7 +114,7 @@ func userAdd(conf *viper.Viper, userid string, password string) error {
114114
}
115115

116116
if _, err := txn.Mutate(ctx, mu); err != nil {
117-
return errors.Wrapf(err, "unable to create user")
117+
return fmt.Errorf("unable to create user: %w", err)
118118
}
119119

120120
fmt.Printf("Created new user with id %v\n", userid)
@@ -124,7 +124,7 @@ func userAdd(conf *viper.Viper, userid string, password string) error {
124124
func groupAdd(conf *viper.Viper, groupId string) error {
125125
dc, cancel, err := getClientWithAdminCtx(conf)
126126
if err != nil {
127-
return errors.Wrapf(err, "unable to get admin context")
127+
return fmt.Errorf("unable to get admin context: %w", err)
128128
}
129129
defer cancel()
130130

@@ -139,10 +139,10 @@ func groupAdd(conf *viper.Viper, groupId string) error {
139139

140140
group, err := queryGroup(ctx, txn, groupId)
141141
if err != nil {
142-
return errors.Wrapf(err, "while querying group")
142+
return fmt.Errorf("while querying group: %w", err)
143143
}
144144
if group != nil {
145-
return errors.Errorf("group %q already exists", groupId)
145+
return fmt.Errorf("group %q already exists", groupId)
146146
}
147147

148148
createGroupNQuads := CreateGroupNQuads(groupId)
@@ -152,7 +152,7 @@ func groupAdd(conf *viper.Viper, groupId string) error {
152152
Set: createGroupNQuads,
153153
}
154154
if _, err = txn.Mutate(ctx, mu); err != nil {
155-
return errors.Wrapf(err, "unable to create group")
155+
return fmt.Errorf("unable to create group: %w", err)
156156
}
157157

158158
fmt.Printf("Created new group with id %v\n", groupId)
@@ -191,7 +191,7 @@ func userOrGroupDel(conf *viper.Viper, userOrGroupId string,
191191
queryFn func(context.Context, *dgo.Txn, string) (AclEntity, error)) error {
192192
dc, cancel, err := getClientWithAdminCtx(conf)
193193
if err != nil {
194-
return errors.Wrapf(err, "unable to get admin context")
194+
return fmt.Errorf("unable to get admin context: %w", err)
195195
}
196196
defer cancel()
197197

@@ -209,7 +209,7 @@ func userOrGroupDel(conf *viper.Viper, userOrGroupId string,
209209
return err
210210
}
211211
if len(entity.GetUid()) == 0 {
212-
return errors.Errorf("unable to delete %q since it does not exist",
212+
return fmt.Errorf("unable to delete %q since it does not exist",
213213
userOrGroupId)
214214
}
215215

@@ -226,7 +226,7 @@ func userOrGroupDel(conf *viper.Viper, userOrGroupId string,
226226
}
227227

228228
if _, err = txn.Mutate(ctx, mu); err != nil {
229-
return errors.Wrapf(err, "unable to delete %q", userOrGroupId)
229+
return fmt.Errorf("unable to delete %q: %w", userOrGroupId, err)
230230
}
231231

232232
fmt.Printf("Successfully deleted %q\n", userOrGroupId)
@@ -249,8 +249,7 @@ func mod(conf *viper.Viper) error {
249249
groupList := conf.GetString("group_list")
250250
if (newPassword && groupList != defaultGroupList) ||
251251
(!newPassword && groupList == defaultGroupList) {
252-
return errors.Errorf(
253-
"one of --new_password or --group_list must be provided, but not both")
252+
return errors.New("one of --new_password or --group_list must be provided, but not both")
254253
}
255254

256255
if newPassword {
@@ -272,7 +271,7 @@ func changePassword(conf *viper.Viper, userId string) error {
272271
// 1. get the dgo client with appropriate access JWT
273272
dc, cancel, err := getClientWithAdminCtx(conf)
274273
if err != nil {
275-
return errors.Wrapf(err, "unable to get dgo client")
274+
return fmt.Errorf("unable to get dgo client: %w", err)
276275
}
277276
defer cancel()
278277

@@ -294,10 +293,10 @@ func changePassword(conf *viper.Viper, userId string) error {
294293
// 3. query the user's current uid
295294
user, err := queryUser(ctx, txn, userId)
296295
if err != nil {
297-
return errors.Wrapf(err, "while querying user")
296+
return fmt.Errorf("while querying user: %w", err)
298297
}
299298
if user == nil {
300-
return errors.Errorf("user %q does not exist", userId)
299+
return fmt.Errorf("user %q does not exist", userId)
301300
}
302301

303302
// 4. mutate the user's password
@@ -312,7 +311,7 @@ func changePassword(conf *viper.Viper, userId string) error {
312311
Set: chPdNQuads,
313312
}
314313
if _, err := txn.Mutate(ctx, mu); err != nil {
315-
return errors.Wrapf(err, "unable to change password for user %v", userId)
314+
return fmt.Errorf("unable to change password for user %v: %w", userId, err)
316315
}
317316
fmt.Printf("Successfully changed password for %v\n", userId)
318317
return nil
@@ -321,7 +320,7 @@ func changePassword(conf *viper.Viper, userId string) error {
321320
func userMod(conf *viper.Viper, userId string, groups string) error {
322321
dc, cancel, err := getClientWithAdminCtx(conf)
323322
if err != nil {
324-
return errors.Wrapf(err, "unable to get admin context")
323+
return fmt.Errorf("unable to get admin context: %w", err)
325324
}
326325
defer cancel()
327326

@@ -336,10 +335,10 @@ func userMod(conf *viper.Viper, userId string, groups string) error {
336335

337336
user, err := queryUser(ctx, txn, userId)
338337
if err != nil {
339-
return errors.Wrapf(err, "while querying user")
338+
return fmt.Errorf("while querying user: %w", err)
340339
}
341340
if user == nil {
342-
return errors.Errorf("user %q does not exist", userId)
341+
return fmt.Errorf("user %q does not exist", userId)
343342
}
344343

345344
targetGroupsMap := make(map[string]struct{})
@@ -384,7 +383,7 @@ func userMod(conf *viper.Viper, userId string, groups string) error {
384383
}
385384

386385
if _, err := txn.Mutate(ctx, mu); err != nil {
387-
return errors.Wrapf(err, "while mutating the group")
386+
return fmt.Errorf("while mutating the group: %w", err)
388387
}
389388
fmt.Printf("Successfully modified groups for user %v.\n", userId)
390389
fmt.Println("The latest info is:")
@@ -407,17 +406,17 @@ func chMod(conf *viper.Viper) error {
407406
perm := conf.GetInt("perm")
408407
switch {
409408
case len(groupName) == 0:
410-
return errors.Errorf("the group must not be empty")
409+
return errors.New("the group must not be empty")
411410
case len(predicate) == 0:
412-
return errors.Errorf("no predicates specified")
411+
return errors.New("no predicates specified")
413412
case perm > 7:
414-
return errors.Errorf("the perm value must be less than or equal to 7, "+
413+
return fmt.Errorf("the perm value must be less than or equal to 7, "+
415414
"the provided value is %d", perm)
416415
}
417416

418417
dc, cancel, err := getClientWithAdminCtx(conf)
419418
if err != nil {
420-
return errors.Wrapf(err, "unable to get admin context")
419+
return fmt.Errorf("unable to get admin context: %w", err)
421420
}
422421
defer cancel()
423422

@@ -502,10 +501,10 @@ func chMod(conf *viper.Viper) error {
502501

503502
uidCount, ok := jsonResp["groupUIDCount"][0]["count"]
504503
if !ok {
505-
return errors.New("Malformed output of groupUIDCount")
504+
return errors.New("malformed output of groupUIDCount")
506505
} else if uidCount == 0 {
507506
// We already have a check for multiple groups with same name at dgraph/acl/utils.go:142
508-
return errors.Errorf("Group <%s> doesn't exist", groupName)
507+
return fmt.Errorf("group <%s> doesn't exist", groupName)
509508
}
510509
return nil
511510
}
@@ -528,7 +527,7 @@ func queryUser(ctx context.Context, txn *dgo.Txn, userid string) (user *User, er
528527

529528
queryResp, err := txn.QueryWithVars(ctx, query, queryVars)
530529
if err != nil {
531-
return nil, errors.Wrapf(err, "hile query user with id %s", userid)
530+
return nil, fmt.Errorf("while query user with id %s: %w", userid, err)
532531
}
533532
user, err = UnmarshalUser(queryResp, "user")
534533
if err != nil {
@@ -544,7 +543,7 @@ func getUserModNQuad(ctx context.Context, txn *dgo.Txn, userId string,
544543
return nil, err
545544
}
546545
if group == nil {
547-
return nil, errors.Errorf("group %q does not exist", groupId)
546+
return nil, fmt.Errorf("group %q does not exist", groupId)
548547
}
549548

550549
createUserGroupNQuads := &api.NQuad{
@@ -590,7 +589,7 @@ func queryAndPrintUser(ctx context.Context, txn *dgo.Txn, userId string) error {
590589
return err
591590
}
592591
if user == nil {
593-
return errors.Errorf("The user %q does not exist.\n", userId)
592+
return fmt.Errorf("the user %q does not exist", userId)
594593
}
595594

596595
fmt.Printf("User : %s\n", userId)
@@ -608,7 +607,7 @@ func queryAndPrintGroup(ctx context.Context, txn *dgo.Txn, groupId string) error
608607
return err
609608
}
610609
if group == nil {
611-
return errors.Errorf("The group %s doesn't exist", groupId)
610+
return fmt.Errorf("the group %s doesn't exist", groupId)
612611
}
613612

614613
fmt.Printf("Group: %s\n", groupId)
@@ -636,7 +635,7 @@ func info(conf *viper.Viper) error {
636635

637636
dc, cancel, err := getClientWithAdminCtx(conf)
638637
if err != nil {
639-
return errors.Wrapf(err, "unable to get admin context")
638+
return fmt.Errorf("unable to get admin context: %w", err)
640639
}
641640
defer cancel()
642641

acl/acl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ package acl
1111
import (
1212
"context"
1313
"encoding/json"
14+
"errors"
1415
"fmt"
1516
"strconv"
1617
"testing"
1718
"time"
1819

19-
"github.com/pkg/errors"
2020
"github.com/stretchr/testify/require"
2121

2222
"github.com/dgraph-io/dgo/v240"

acl/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package acl
77

88
import (
99
"encoding/json"
10+
"fmt"
1011

1112
"github.com/golang/glog"
12-
"github.com/pkg/errors"
1313
"github.com/spf13/viper"
1414

1515
"github.com/dgraph-io/dgo/v240"
@@ -86,15 +86,15 @@ func UnmarshalUser(resp *api.Response, userKey string) (user *User, err error) {
8686

8787
err = json.Unmarshal(resp.GetJson(), &m)
8888
if err != nil {
89-
return nil, errors.Wrapf(err, "unable to unmarshal the query user response")
89+
return nil, fmt.Errorf("unable to unmarshal the query user response: %w", err)
9090
}
9191
users := m[userKey]
9292
if len(users) == 0 {
9393
// the user does not exist
9494
return nil, nil
9595
}
9696
if len(users) > 1 {
97-
return nil, errors.Errorf("Found multiple users: %s", resp.GetJson())
97+
return nil, fmt.Errorf("found multiple users: %s", resp.GetJson())
9898
}
9999
return &users[0], nil
100100
}
@@ -136,7 +136,7 @@ func UnmarshalGroup(input []byte, groupKey string) (group *Group, err error) {
136136
return nil, nil
137137
}
138138
if len(groups) > 1 {
139-
return nil, errors.Errorf("found multiple groups: %s", input)
139+
return nil, fmt.Errorf("found multiple groups: %s", input)
140140
}
141141

142142
return &groups[0], nil

audit/run.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"crypto/aes"
1010
"crypto/cipher"
1111
"encoding/binary"
12+
"errors"
1213
"fmt"
1314
"io"
1415
"os"
1516

1617
"github.com/golang/glog"
17-
"github.com/pkg/errors"
1818
"github.com/spf13/cobra"
1919
"github.com/spf13/viper"
2020

@@ -102,7 +102,7 @@ func run() error {
102102
}
103103

104104
if err := decrypt(file, outfile, block, stat.Size()); err != nil {
105-
return errors.Wrap(err, "could not decrypt audit log")
105+
return fmt.Errorf("could not decrypt audit log: %w", err)
106106
}
107107

108108
glog.Infof("decryption of audit file %s is done: decrypted file is %s",
@@ -119,14 +119,14 @@ func decrypt(file io.ReaderAt, outfile io.Writer, block cipher.Block, sz int64)
119119
iv := make([]byte, aes.BlockSize)
120120
n, err := file.ReadAt(iv, iterator) // get first iv
121121
if err != nil {
122-
return nil, 0, errors.Wrap(err, "unable to read IV")
122+
return nil, 0, fmt.Errorf("unable to read IV: %w", err)
123123
}
124124
iterator = iterator + int64(n) + 4 // length of verification text encoded in uint32
125125

126126
ct := make([]byte, len(x.VerificationText))
127127
n, err = file.ReadAt(ct, iterator)
128128
if err != nil {
129-
return nil, 0, errors.Wrap(err, "unable to read verification text")
129+
return nil, 0, fmt.Errorf("unable to read verification text: %w", err)
130130
}
131131
iterator = iterator + int64(n)
132132

@@ -145,14 +145,14 @@ func decrypt(file io.ReaderAt, outfile io.Writer, block cipher.Block, sz int64)
145145
iv := make([]byte, aes.BlockSize)
146146
n, err := file.ReadAt(iv, iterator)
147147
if err != nil {
148-
return nil, 0, errors.Wrap(err, "unable to read IV")
148+
return nil, 0, fmt.Errorf("unable to read IV: %w", err)
149149
}
150150
iterator = iterator + int64(n)
151151

152152
ct := make([]byte, len(x.VerificationTextDeprecated))
153153
n, err = file.ReadAt(ct, iterator)
154154
if err != nil {
155-
return nil, 0, errors.Wrap(err, "unable to read verification text")
155+
return nil, 0, fmt.Errorf("unable to read verification text: %w", err)
156156
}
157157
iterator = iterator + int64(n)
158158

0 commit comments

Comments
 (0)