Skip to content

Commit 0b3571a

Browse files
committed
errname and fix changes
1 parent bb5aa17 commit 0b3571a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+125
-138
lines changed

.golangci.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ issues:
231231

232232
run:
233233
timeout: 10m
234+
skip-files:
235+
- scripts/rules.go
234236

235237
# Over time, add more and more linters from
236238
# https://golangci-lint.run/usage/linters/ as the code improves.

cli/cliui/cliui.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/coder/pretty"
1313
)
1414

15-
var Canceled = xerrors.New("canceled")
15+
var ErrCanceled = xerrors.New("canceled")
1616

1717
// DefaultStyles compose visual elements of the UI.
1818
var DefaultStyles Styles

cli/cliui/prompt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) {
124124
return "", err
125125
case line := <-lineCh:
126126
if opts.IsConfirm && line != "yes" && line != "y" {
127-
return line, xerrors.Errorf("got %q: %w", line, Canceled)
127+
return line, xerrors.Errorf("got %q: %w", line, ErrCanceled)
128128
}
129129
if opts.Validate != nil {
130130
err := opts.Validate(line)
@@ -139,7 +139,7 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) {
139139
case <-interrupt:
140140
// Print a newline so that any further output starts properly on a new line.
141141
_, _ = fmt.Fprintln(inv.Stdout)
142-
return "", Canceled
142+
return "", ErrCanceled
143143
}
144144
}
145145

cli/cliui/provisionerjob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func ProvisionerJob(ctx context.Context, wr io.Writer, opts ProvisionerJobOption
204204
switch job.Status {
205205
case codersdk.ProvisionerJobCanceled:
206206
jobMutex.Unlock()
207-
return Canceled
207+
return ErrCanceled
208208
case codersdk.ProvisionerJobSucceeded:
209209
jobMutex.Unlock()
210210
return nil

cli/cliui/provisionerjob_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func newProvisionerJob(t *testing.T) provisionerJobTest {
250250
defer close(done)
251251
err := inv.WithContext(context.Background()).Run()
252252
if err != nil {
253-
assert.ErrorIs(t, err, cliui.Canceled)
253+
assert.ErrorIs(t, err, cliui.ErrCanceled)
254254
}
255255
}()
256256
t.Cleanup(func() {

cli/cliui/select.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func Select(inv *serpent.Invocation, opts SelectOptions) (string, error) {
147147
}
148148

149149
if model.canceled {
150-
return "", Canceled
150+
return "", ErrCanceled
151151
}
152152

153153
return model.selected, nil
@@ -360,7 +360,7 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er
360360
}
361361

362362
if model.canceled {
363-
return nil, Canceled
363+
return nil, ErrCanceled
364364
}
365365

366366
return model.selectedOptions(), nil

cli/configssh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (r *RootCmd) configSSH() *serpent.Command {
268268
IsConfirm: true,
269269
})
270270
if err != nil {
271-
if line == "" && xerrors.Is(err, cliui.Canceled) {
271+
if line == "" && xerrors.Is(err, cliui.ErrCanceled) {
272272
return nil
273273
}
274274
// Selecting "no" will use the last config.

cli/externalauth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fi
9191
if err != nil {
9292
return err
9393
}
94-
return cliui.Canceled
94+
return cliui.ErrCanceled
9595
}
9696
if extra != "" {
9797
if extAuth.TokenExtra == nil {

cli/externalauth_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestExternalAuth(t *testing.T) {
2929
inv.Stdout = pty.Output()
3030
waiter := clitest.StartWithWaiter(t, inv)
3131
pty.ExpectMatch("https://github.com")
32-
waiter.RequireIs(cliui.Canceled)
32+
waiter.RequireIs(cliui.ErrCanceled)
3333
})
3434
t.Run("SuccessWithToken", func(t *testing.T) {
3535
t.Parallel()

cli/gitaskpass.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (r *RootCmd) gitAskpass() *serpent.Command {
5353
cliui.Warn(inv.Stderr, "Coder was unable to handle this git request. The default git behavior will be used instead.",
5454
lines...,
5555
)
56-
return cliui.Canceled
56+
return cliui.ErrCanceled
5757
}
5858
return xerrors.Errorf("get git token: %w", err)
5959
}

cli/gitaskpass_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestGitAskpass(t *testing.T) {
5959
pty := ptytest.New(t)
6060
inv.Stderr = pty.Output()
6161
err := inv.Run()
62-
require.ErrorIs(t, err, cliui.Canceled)
62+
require.ErrorIs(t, err, cliui.ErrCanceled)
6363
pty.ExpectMatch("Nope!")
6464
})
6565

cli/login.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func promptFirstUsername(inv *serpent.Invocation) (string, error) {
4848
Text: "What " + pretty.Sprint(cliui.DefaultStyles.Field, "username") + " would you like?",
4949
Default: currentUser.Username,
5050
})
51-
if errors.Is(err, cliui.Canceled) {
51+
if errors.Is(err, cliui.ErrCanceled) {
5252
return "", nil
5353
}
5454
if err != nil {
@@ -64,7 +64,7 @@ func promptFirstName(inv *serpent.Invocation) (string, error) {
6464
Default: "",
6565
})
6666
if err != nil {
67-
if errors.Is(err, cliui.Canceled) {
67+
if errors.Is(err, cliui.ErrCanceled) {
6868
return "", nil
6969
}
7070
return "", err
@@ -508,7 +508,7 @@ func promptTrialInfo(inv *serpent.Invocation, fieldName string) (string, error)
508508
},
509509
})
510510
if err != nil {
511-
if errors.Is(err, cliui.Canceled) {
511+
if errors.Is(err, cliui.ErrCanceled) {
512512
return "", nil
513513
}
514514
return "", err

cli/open.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *RootCmd) openVSCode() *serpent.Command {
8989
})
9090
if err != nil {
9191
if xerrors.Is(err, context.Canceled) {
92-
return cliui.Canceled
92+
return cliui.ErrCanceled
9393
}
9494
return xerrors.Errorf("agent: %w", err)
9595
}

cli/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (r *RootCmd) RunWithSubcommands(subcommands []*serpent.Command) {
171171
code = exitErr.code
172172
err = exitErr.err
173173
}
174-
if errors.Is(err, cliui.Canceled) {
174+
if errors.Is(err, cliui.ErrCanceled) {
175175
//nolint:revive
176176
os.Exit(code)
177177
}

cli/ssh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (r *RootCmd) ssh() *serpent.Command {
264264
})
265265
if err != nil {
266266
if xerrors.Is(err, context.Canceled) {
267-
return cliui.Canceled
267+
return cliui.ErrCanceled
268268
}
269269
return err
270270
}

cli/ssh_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func TestSSH(t *testing.T) {
341341

342342
cmdDone := tGo(t, func() {
343343
err := inv.WithContext(ctx).Run()
344-
assert.ErrorIs(t, err, cliui.Canceled)
344+
assert.ErrorIs(t, err, cliui.ErrCanceled)
345345
})
346346
pty.ExpectMatch(wantURL)
347347
cancel()

cli/vscodessh.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (r *RootCmd) vscodeSSH() *serpent.Command {
142142
})
143143
if err != nil {
144144
if xerrors.Is(err, context.Canceled) {
145-
return cliui.Canceled
145+
return cliui.ErrCanceled
146146
}
147147
}
148148

cmd/cliui/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func main() {
8989
return nil
9090
},
9191
})
92-
if errors.Is(err, cliui.Canceled) {
92+
if errors.Is(err, cliui.ErrCanceled) {
9393
return nil
9494
}
9595
if err != nil {
@@ -100,7 +100,7 @@ func main() {
100100
Default: cliui.ConfirmYes,
101101
IsConfirm: true,
102102
})
103-
if errors.Is(err, cliui.Canceled) {
103+
if errors.Is(err, cliui.ErrCanceled) {
104104
return nil
105105
}
106106
if err != nil {

coderd/autobuild/lifecycle_executor_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1219,10 +1219,9 @@ func mustProvisionWorkspaceWithParameters(t *testing.T, client *codersdk.Client,
12191219
return coderdtest.MustWorkspace(t, client, ws.ID)
12201220
}
12211221

1222-
func mustSchedule(t *testing.T, _ string) *cron.Schedule {
1222+
func mustSchedule(t *testing.T, s string) *cron.Schedule {
12231223
t.Helper()
1224-
// Always use the same schedule string for consistency
1225-
sched, err := cron.Weekly("CRON_TZ=UTC 0 * * * *")
1224+
sched, err := cron.Weekly(s)
12261225
require.NoError(t, err)
12271226
return sched
12281227
}

coderd/coderdtest/authorize_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,11 @@ func fuzzAuthzPrep(t *testing.T, prep rbac.PreparedAuthorized, n int, action pol
116116
return pairs
117117
}
118118

119-
func fuzzAuthz(t *testing.T, sub rbac.Subject, rec rbac.Authorizer, _ int) []coderdtest.ActionObjectPair {
119+
func fuzzAuthz(t *testing.T, sub rbac.Subject, rec rbac.Authorizer, n int) []coderdtest.ActionObjectPair {
120120
t.Helper()
121-
// Always use 10 pairs for consistency
122-
const numPairs = 10
123-
pairs := make([]coderdtest.ActionObjectPair, 0, numPairs)
121+
pairs := make([]coderdtest.ActionObjectPair, 0, n)
124122

125-
for i := 0; i < numPairs; i++ {
123+
for i := 0; i < n; i++ {
126124
p := coderdtest.ActionObjectPair{Action: coderdtest.RandomRBACAction(), Object: coderdtest.RandomRBACObject()}
127125
_ = rec.Authorize(context.Background(), sub, p.Action, p.Object)
128126
pairs = append(pairs, p)

coderd/cryptokeys/cache_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,10 @@ func decodedSecret(t *testing.T, key codersdk.CryptoKey) []byte {
504504
return secret
505505
}
506506

507-
func generateKey(t *testing.T, _ int) string {
507+
func generateKey(t *testing.T, size int) string {
508508
t.Helper()
509509

510-
// Always use 64 bytes for consistency
511-
const keySize = 64
512-
key := make([]byte, keySize)
510+
key := make([]byte, size)
513511
_, err := rand.Read(key)
514512
require.NoError(t, err)
515513

coderd/database/dbauthz/dbauthz.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var _ database.Store = (*querier)(nil)
3333

3434
const wrapname = "dbauthz.querier"
3535

36-
// NoActorError is returned if no actor is present in the context.
37-
var NoActorError = xerrors.Errorf("no authorization actor in context")
36+
// ErrNoActor is returned if no actor is present in the context.
37+
var ErrNoActor = xerrors.Errorf("no authorization actor in context")
3838

3939
// NotAuthorizedError is a sentinel error that unwraps to sql.ErrNoRows.
4040
// This allows the internal error to be read by the caller if needed. Otherwise
@@ -69,7 +69,7 @@ func IsNotAuthorizedError(err error) bool {
6969
if err == nil {
7070
return false
7171
}
72-
if xerrors.Is(err, NoActorError) {
72+
if xerrors.Is(err, ErrNoActor) {
7373
return true
7474
}
7575

@@ -140,7 +140,7 @@ func (q *querier) Wrappers() []string {
140140
func (q *querier) authorizeContext(ctx context.Context, action policy.Action, object rbac.Objecter) error {
141141
act, ok := ActorFromContext(ctx)
142142
if !ok {
143-
return NoActorError
143+
return ErrNoActor
144144
}
145145

146146
err := q.auth.Authorize(ctx, act, action, object.RBACObject())
@@ -466,7 +466,7 @@ func insertWithAction[
466466
// Fetch the rbac subject
467467
act, ok := ActorFromContext(ctx)
468468
if !ok {
469-
return empty, NoActorError
469+
return empty, ErrNoActor
470470
}
471471

472472
// Authorize the action
@@ -544,7 +544,7 @@ func fetchWithAction[
544544
// Fetch the rbac subject
545545
act, ok := ActorFromContext(ctx)
546546
if !ok {
547-
return empty, NoActorError
547+
return empty, ErrNoActor
548548
}
549549

550550
// Fetch the database object
@@ -620,7 +620,7 @@ func fetchAndQuery[
620620
// Fetch the rbac subject
621621
act, ok := ActorFromContext(ctx)
622622
if !ok {
623-
return empty, NoActorError
623+
return empty, ErrNoActor
624624
}
625625

626626
// Fetch the database object
@@ -654,7 +654,7 @@ func fetchWithPostFilter[
654654
// Fetch the rbac subject
655655
act, ok := ActorFromContext(ctx)
656656
if !ok {
657-
return empty, NoActorError
657+
return empty, ErrNoActor
658658
}
659659

660660
// Fetch the database object
@@ -673,7 +673,7 @@ func fetchWithPostFilter[
673673
func prepareSQLFilter(ctx context.Context, authorizer rbac.Authorizer, action policy.Action, resourceType string) (rbac.PreparedAuthorized, error) {
674674
act, ok := ActorFromContext(ctx)
675675
if !ok {
676-
return nil, NoActorError
676+
return nil, ErrNoActor
677677
}
678678

679679
return authorizer.Prepare(ctx, act, action, resourceType)
@@ -752,7 +752,7 @@ func (*querier) convertToDeploymentRoles(names []string) []rbac.RoleIdentifier {
752752
func (q *querier) canAssignRoles(ctx context.Context, orgID uuid.UUID, added, removed []rbac.RoleIdentifier) error {
753753
actor, ok := ActorFromContext(ctx)
754754
if !ok {
755-
return NoActorError
755+
return ErrNoActor
756756
}
757757

758758
roleAssign := rbac.ResourceAssignRole
@@ -961,7 +961,7 @@ func (q *querier) customRoleEscalationCheck(ctx context.Context, actor rbac.Subj
961961
func (q *querier) customRoleCheck(ctx context.Context, role database.CustomRole) error {
962962
act, ok := ActorFromContext(ctx)
963963
if !ok {
964-
return NoActorError
964+
return ErrNoActor
965965
}
966966

967967
// Org permissions require an org role
@@ -3896,7 +3896,7 @@ func (q *querier) UpdateProvisionerJobWithCancelByID(ctx context.Context, arg da
38963896
// Only owners can cancel workspace builds
38973897
actor, ok := ActorFromContext(ctx)
38983898
if !ok {
3899-
return NoActorError
3899+
return ErrNoActor
39003900
}
39013901
if !slice.Contains(actor.Roles.Names(), rbac.RoleOwner()) {
39023902
return xerrors.Errorf("only owners can cancel workspace builds")

coderd/database/dbauthz/setup_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (s *MethodTestSuite) NoActorErrorTest(callMethod func(ctx context.Context)
252252
s.Run("AsRemoveActor", func() {
253253
// Call without any actor
254254
_, err := callMethod(context.Background())
255-
s.ErrorIs(err, dbauthz.NoActorError, "method should return NoActorError error when no actor is provided")
255+
s.ErrorIs(err, dbauthz.ErrNoActor, "method should return NoActorError error when no actor is provided")
256256
})
257257
}
258258

0 commit comments

Comments
 (0)