Skip to content

Commit 342f734

Browse files
committed
type errors
1 parent 92eeb44 commit 342f734

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

notify/stages/sync_flush_stage.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const (
2222
SyncFlushActionSync SyncFlushAction = "sync"
2323
)
2424

25+
var (
26+
ErrMissingGroupKey = errors.New("groupKey missing")
27+
ErrMissingNow = errors.New("now missing")
28+
ErrMissingGroupInterval = errors.New("groupInterval missing")
29+
ErrUnexpectedEntryResultSize = errors.New("unexpected entry result size")
30+
)
31+
2532
// SyncFlushStage delays the notification pipeline execution to sync flushes between multiple instances.
2633
type SyncFlushStage struct {
2734
nflog notify.NotificationLog
@@ -69,18 +76,18 @@ func (sfs *SyncFlushStage) calculateSyncWaitTime(curPipelineTime, prevPipelineTi
6976
func (sfs *SyncFlushStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Alert) (context.Context, []*types.Alert, error) {
7077
gkey, ok := notify.GroupKey(ctx)
7178
if !ok {
72-
return ctx, nil, errors.New("group key missing")
79+
return ctx, nil, ErrMissingGroupKey
7380
}
7481

7582
// get the tick time from the context.
7683
curPipelineTime, ok := notify.Now(ctx)
7784
if !ok {
78-
return ctx, nil, errors.New("now missing")
85+
return ctx, nil, ErrMissingNow
7986
}
8087

8188
groupInterval, ok := notify.GroupInterval(ctx)
8289
if !ok {
83-
return ctx, nil, errors.New("groupWait missing")
90+
return ctx, nil, ErrMissingGroupInterval
8491
}
8592

8693
entries, err := sfs.nflog.Query(nflog.QGroupKey(gkey), nflog.QReceiver(sfs.recv))
@@ -96,7 +103,7 @@ func (sfs *SyncFlushStage) Exec(ctx context.Context, l log.Logger, alerts ...*ty
96103
case 1:
97104
entry = entries[0]
98105
default:
99-
return ctx, nil, fmt.Errorf("unexpected entry result size %d", len(entries))
106+
return ctx, nil, fmt.Errorf("%w: %d", ErrUnexpectedEntryResultSize, len(entries))
100107
}
101108

102109
// calculate next flush time based on last entry on notification log

notify/stages/sync_flush_stage_test.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -79,38 +79,37 @@ func TestSyncFlushStageExec(t *testing.T) {
7979
skipGroupKey bool
8080
skipNow bool
8181
skipGroupWait bool
82-
expectedErr bool
82+
expectedErr error
8383
}{
8484
{
8585
name: "no entries",
8686
sync: true,
8787
entries: []*nflogpb.Entry{},
8888
pipelineTime: now,
89-
expectedErr: false,
9089
},
9190
{
9291
name: "missing group key",
9392
sync: true,
9493
entries: []*nflogpb.Entry{},
9594
pipelineTime: now,
9695
skipGroupKey: true,
97-
expectedErr: true,
96+
expectedErr: ErrMissingGroupKey,
9897
},
9998
{
10099
name: "missing now",
101100
sync: true,
102101
entries: []*nflogpb.Entry{},
103102
pipelineTime: now,
104103
skipNow: true,
105-
expectedErr: true,
104+
expectedErr: ErrMissingNow,
106105
},
107106
{
108107
name: "missing group wait",
109108
sync: true,
110109
entries: []*nflogpb.Entry{},
111110
pipelineTime: now,
112111
skipGroupWait: true,
113-
expectedErr: true,
112+
expectedErr: ErrMissingGroupInterval,
114113
},
115114
{
116115
name: "entry exists but no wait needed",
@@ -122,7 +121,6 @@ func TestSyncFlushStageExec(t *testing.T) {
122121
},
123122
},
124123
pipelineTime: now,
125-
expectedErr: false,
126124
},
127125
{
128126
name: "entry exists and wait would be needed",
@@ -134,7 +132,6 @@ func TestSyncFlushStageExec(t *testing.T) {
134132
},
135133
},
136134
pipelineTime: now,
137-
expectedErr: false,
138135
},
139136
{
140137
name: "sync disabled",
@@ -146,7 +143,6 @@ func TestSyncFlushStageExec(t *testing.T) {
146143
},
147144
},
148145
pipelineTime: now,
149-
expectedErr: false,
150146
},
151147
{
152148
name: "context timeout",
@@ -159,7 +155,7 @@ func TestSyncFlushStageExec(t *testing.T) {
159155
},
160156
pipelineTime: now,
161157
contextTimeout: 50 * time.Millisecond,
162-
expectedErr: true,
158+
expectedErr: context.DeadlineExceeded,
163159
},
164160
{
165161
name: "multiple entries error",
@@ -175,7 +171,7 @@ func TestSyncFlushStageExec(t *testing.T) {
175171
},
176172
},
177173
pipelineTime: now,
178-
expectedErr: true,
174+
expectedErr: ErrUnexpectedEntryResultSize,
179175
},
180176
}
181177

@@ -212,12 +208,8 @@ func TestSyncFlushStageExec(t *testing.T) {
212208
alerts := []*types.Alert{{}, {}}
213209
_, gotAlerts, err := stage.Exec(ctx, log.NewNopLogger(), alerts...)
214210

215-
if tc.expectedErr {
216-
assert.Error(t, err)
217-
} else {
218-
assert.NoError(t, err)
219-
assert.Equal(t, alerts, gotAlerts)
220-
}
211+
assert.Equal(t, alerts, gotAlerts)
212+
assert.ErrorIs(t, err, tc.expectedErr)
221213
})
222214
}
223215
}
@@ -264,3 +256,4 @@ func TestNewSyncFlushStage(t *testing.T) {
264256
})
265257
}
266258
}
259+

0 commit comments

Comments
 (0)