Skip to content

Commit e3a31d6

Browse files
authored
Replace usage of pkg/error with standard lib
Replace usage of pkg/error with standard lib
2 parents 264301e + 67ba0f4 commit e3a31d6

File tree

23 files changed

+177
-177
lines changed

23 files changed

+177
-177
lines changed

backend/mysql/mysql.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
_ "embed"
7+
"errors"
78
"fmt"
89
"strings"
910
"time"
@@ -16,7 +17,6 @@ import (
1617
"github.com/cschleiden/go-workflows/workflow"
1718
_ "github.com/go-sql-driver/mysql"
1819
"github.com/google/uuid"
19-
"github.com/pkg/errors"
2020
)
2121

2222
//go:embed schema.sql
@@ -32,7 +32,7 @@ func NewMysqlBackend(host string, port int, user, password, database string, opt
3232
}
3333

3434
if _, err := db.Exec(schema); err != nil {
35-
panic(errors.Wrap(err, "could not initialize database"))
35+
panic(fmt.Errorf("initializing database: %w", err))
3636
}
3737

3838
if err := db.Close(); err != nil {
@@ -63,7 +63,7 @@ func (b *mysqlBackend) CreateWorkflowInstance(ctx context.Context, m history.Wor
6363
Isolation: sql.LevelReadCommitted,
6464
})
6565
if err != nil {
66-
return errors.Wrap(err, "could not start transaction")
66+
return fmt.Errorf("starting transaction: %w", err)
6767
}
6868
defer tx.Rollback()
6969

@@ -74,11 +74,11 @@ func (b *mysqlBackend) CreateWorkflowInstance(ctx context.Context, m history.Wor
7474

7575
// Initial history is empty, store only new events
7676
if err := insertNewEvents(ctx, tx, m.WorkflowInstance.InstanceID, []history.Event{m.HistoryEvent}); err != nil {
77-
return errors.Wrap(err, "could not insert new event")
77+
return fmt.Errorf("inserting new event: %w", err)
7878
}
7979

8080
if err := tx.Commit(); err != nil {
81-
return errors.Wrap(err, "could not create workflow instance")
81+
return fmt.Errorf("creating workflow instance: %w", err)
8282
}
8383

8484
return nil
@@ -99,7 +99,7 @@ func (b *mysqlBackend) CancelWorkflowInstance(ctx context.Context, instance *wor
9999

100100
// Cancel workflow instance
101101
if err := insertNewEvents(ctx, tx, instanceID, []history.Event{*event}); err != nil {
102-
return errors.Wrap(err, "could not insert cancellation event")
102+
return fmt.Errorf("inserting cancellation event: %w", err)
103103
}
104104

105105
// Recursively, find any sub-workflow instance to cancel
@@ -113,12 +113,12 @@ func (b *mysqlBackend) CancelWorkflowInstance(ctx context.Context, instance *wor
113113
break
114114
}
115115

116-
return errors.Wrap(err, "could not get workflow instance for cancelling")
116+
return fmt.Errorf("getting workflow instance for cancelling: %w", err)
117117
}
118118

119119
// Cancel sub-workflow instance
120120
if err := insertNewEvents(ctx, tx, subWorkflowInstanceID, []history.Event{*event}); err != nil {
121-
return errors.Wrap(err, "could not insert cancellation event")
121+
return fmt.Errorf("inserting cancellation event: %w", err)
122122
}
123123

124124
instanceID = subWorkflowInstanceID
@@ -150,7 +150,7 @@ func (b *mysqlBackend) GetWorkflowInstanceHistory(ctx context.Context, instance
150150
)
151151
}
152152
if err != nil {
153-
return nil, errors.Wrap(err, "could not get history")
153+
return nil, fmt.Errorf("getting history: %w", err)
154154
}
155155

156156
h := make([]history.Event, 0)
@@ -171,12 +171,12 @@ func (b *mysqlBackend) GetWorkflowInstanceHistory(ctx context.Context, instance
171171
&attributes,
172172
&historyEvent.VisibleAt,
173173
); err != nil {
174-
return nil, errors.Wrap(err, "could not scan event")
174+
return nil, fmt.Errorf("scanning event: %w", err)
175175
}
176176

177177
a, err := history.DeserializeAttributes(historyEvent.Type, attributes)
178178
if err != nil {
179-
return nil, errors.Wrap(err, "could not deserialize attributes")
179+
return nil, fmt.Errorf("deserializing attributes: %w", err)
180180
}
181181

182182
historyEvent.Attributes = a
@@ -229,7 +229,7 @@ func createInstance(ctx context.Context, tx *sql.Tx, wfi *workflow.Instance, ign
229229
parentEventID,
230230
)
231231
if err != nil {
232-
return errors.Wrap(err, "could not insert workflow instance")
232+
return fmt.Errorf("inserting workflow instance: %w", err)
233233
}
234234

235235
if !ignoreDuplicate {
@@ -263,7 +263,7 @@ func (b *mysqlBackend) SignalWorkflow(ctx context.Context, instanceID string, ev
263263
}
264264

265265
if err := insertNewEvents(ctx, tx, instanceID, []history.Event{event}); err != nil {
266-
return errors.Wrap(err, "could not insert signal event")
266+
return fmt.Errorf("inserting signal event: %w", err)
267267
}
268268

269269
return tx.Commit()
@@ -309,7 +309,7 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
309309
return nil, nil
310310
}
311311

312-
return nil, errors.Wrap(err, "could not scan workflow instance")
312+
return nil, fmt.Errorf("scanning workflow instance: %w", err)
313313
}
314314

315315
// log.Println("Acquired workflow instance", instanceID)
@@ -324,11 +324,11 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
324324
id,
325325
)
326326
if err != nil {
327-
return nil, errors.Wrap(err, "could not lock workflow instance")
327+
return nil, fmt.Errorf("locking workflow instance: %w", err)
328328
}
329329

330330
if affectedRows, err := res.RowsAffected(); err != nil {
331-
return nil, errors.Wrap(err, "could not lock workflow instance")
331+
return nil, fmt.Errorf("locking workflow instance: %w", err)
332332
} else if affectedRows == 0 {
333333
// No instance locked?
334334
return nil, nil
@@ -355,7 +355,7 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
355355
now,
356356
)
357357
if err != nil {
358-
return nil, errors.Wrap(err, "could not get new events")
358+
return nil, fmt.Errorf("getting new events: %w", err)
359359
}
360360

361361
for events.Next() {
@@ -374,12 +374,12 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
374374
&attributes,
375375
&historyEvent.VisibleAt,
376376
); err != nil {
377-
return nil, errors.Wrap(err, "could not scan event")
377+
return nil, fmt.Errorf("scanning event: %w", err)
378378
}
379379

380380
a, err := history.DeserializeAttributes(historyEvent.Type, attributes)
381381
if err != nil {
382-
return nil, errors.Wrap(err, "could not deserialize attributes")
382+
return nil, fmt.Errorf("deserializing attributes: %w", err)
383383
}
384384

385385
historyEvent.Attributes = a
@@ -398,7 +398,7 @@ func (b *mysqlBackend) GetWorkflowTask(ctx context.Context) (*task.Workflow, err
398398
&t.LastSequenceID,
399399
); err != nil {
400400
if err != sql.ErrNoRows {
401-
return nil, errors.Wrap(err, "could not get most recent sequence id")
401+
return nil, fmt.Errorf("getting most recent sequence id: %w", err)
402402
}
403403
}
404404

@@ -448,12 +448,12 @@ func (b *mysqlBackend) CompleteWorkflowTask(
448448
b.workerName,
449449
)
450450
if err != nil {
451-
return errors.Wrap(err, "could not unlock instance")
451+
return fmt.Errorf("unlocking instance: %w", err)
452452
}
453453

454454
changedRows, err := res.RowsAffected()
455455
if err != nil {
456-
return errors.Wrap(err, "could not check for unlocked workflow instances")
456+
return fmt.Errorf("checking for unlocked workflow instances: %w", err)
457457
} else if changedRows != 1 {
458458
return errors.New("could not find workflow instance to unlock")
459459
}
@@ -471,19 +471,19 @@ func (b *mysqlBackend) CompleteWorkflowTask(
471471
fmt.Sprintf(`DELETE FROM pending_events WHERE instance_id = ? AND event_id IN (?%v)`, strings.Repeat(",?", len(executedEvents)-1)),
472472
args...,
473473
); err != nil {
474-
return errors.Wrap(err, "could not delete handled new events")
474+
return fmt.Errorf("deleting handled new events: %w", err)
475475
}
476476
}
477477

478478
// Insert new events generated during this workflow execution to the history
479479
if err := insertHistoryEvents(ctx, tx, instance.InstanceID, executedEvents); err != nil {
480-
return errors.Wrap(err, "could not insert new history events")
480+
return fmt.Errorf("inserting new history events: %w", err)
481481
}
482482

483483
// Schedule activities
484484
for _, e := range activityEvents {
485485
if err := scheduleActivity(ctx, tx, instance, e); err != nil {
486-
return errors.Wrap(err, "could not schedule activity")
486+
return fmt.Errorf("scheduling activity: %w", err)
487487
}
488488
}
489489

@@ -506,12 +506,12 @@ func (b *mysqlBackend) CompleteWorkflowTask(
506506
}
507507

508508
if err := insertNewEvents(ctx, tx, targetInstance.InstanceID, events); err != nil {
509-
return errors.Wrap(err, "could not insert messages")
509+
return fmt.Errorf("inserting messages: %w", err)
510510
}
511511
}
512512

513513
if err := tx.Commit(); err != nil {
514-
return errors.Wrap(err, "could not commit complete workflow transaction")
514+
return fmt.Errorf("committing complete workflow transaction: %w", err)
515515
}
516516

517517
return nil
@@ -534,11 +534,11 @@ func (b *mysqlBackend) ExtendWorkflowTask(ctx context.Context, taskID string, in
534534
b.workerName,
535535
)
536536
if err != nil {
537-
return errors.Wrap(err, "could not extend workflow task lock")
537+
return fmt.Errorf("extending workflow task lock: %w", err)
538538
}
539539

540540
if rowsAffected, err := res.RowsAffected(); err != nil {
541-
return errors.Wrap(err, "could not determine if workflow task was extended")
541+
return fmt.Errorf("determining if workflow task was extended: %w", err)
542542
} else if rowsAffected == 0 {
543543
return errors.New("could not extend workflow task")
544544
}
@@ -578,12 +578,12 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
578578
return nil, nil
579579
}
580580

581-
return nil, errors.Wrap(err, "could not find activity task to lock")
581+
return nil, fmt.Errorf("finding activity task to lock: %w", err)
582582
}
583583

584584
a, err := history.DeserializeAttributes(event.Type, attributes)
585585
if err != nil {
586-
return nil, errors.Wrap(err, "could not deserialize attributes")
586+
return nil, fmt.Errorf("deserializing attributes: %w", err)
587587
}
588588

589589
event.Attributes = a
@@ -595,7 +595,7 @@ func (b *mysqlBackend) GetActivityTask(ctx context.Context) (*task.Activity, err
595595
b.workerName,
596596
id,
597597
); err != nil {
598-
return nil, errors.Wrap(err, "could not lock activity")
598+
return nil, fmt.Errorf("locking activity: %w", err)
599599
}
600600

601601
t := &task.Activity{
@@ -630,11 +630,11 @@ func (b *mysqlBackend) CompleteActivityTask(ctx context.Context, instance *workf
630630
instance.ExecutionID,
631631
b.workerName,
632632
); err != nil {
633-
return errors.Wrap(err, "could not complete activity")
633+
return fmt.Errorf("completing activity: %w", err)
634634
} else {
635635
affected, err := res.RowsAffected()
636636
if err != nil {
637-
return errors.Wrap(err, "could not check for completed activity")
637+
return fmt.Errorf("checking for completed activity: %w", err)
638638
}
639639

640640
if affected == 0 {
@@ -644,7 +644,7 @@ func (b *mysqlBackend) CompleteActivityTask(ctx context.Context, instance *workf
644644

645645
// Insert new event generated during this workflow execution
646646
if err := insertNewEvents(ctx, tx, instance.InstanceID, []history.Event{event}); err != nil {
647-
return errors.Wrap(err, "could not insert new events for completed activity")
647+
return fmt.Errorf("inserting new events for completed activity: %w", err)
648648
}
649649

650650
if err := tx.Commit(); err != nil {
@@ -670,11 +670,11 @@ func (b *mysqlBackend) ExtendActivityTask(ctx context.Context, activityID string
670670
b.workerName,
671671
)
672672
if err != nil {
673-
return errors.Wrap(err, "could not extend activity lock")
673+
return fmt.Errorf("extending activity lock: %w", err)
674674
}
675675

676676
if rowsAffected, err := res.RowsAffected(); err != nil {
677-
return errors.Wrap(err, "could not determine if activity was extended")
677+
return fmt.Errorf("determining if activity was extended: %w", err)
678678
} else if rowsAffected == 0 {
679679
return errors.New("could not extend activity")
680680
}

backend/mysql/mysql_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/cschleiden/go-workflows/backend"
1010
"github.com/cschleiden/go-workflows/backend/test"
1111
"github.com/google/uuid"
12-
"github.com/pkg/errors"
1312
)
1413

1514
const testUser = "root"
@@ -32,7 +31,7 @@ func Test_MysqlBackend(t *testing.T) {
3231
}
3332

3433
if _, err := db.Exec("CREATE DATABASE " + dbName); err != nil {
35-
panic(errors.Wrap(err, "could not create database"))
34+
panic(fmt.Errorf("creating database: %w", err))
3635
}
3736

3837
if err := db.Close(); err != nil {
@@ -47,7 +46,7 @@ func Test_MysqlBackend(t *testing.T) {
4746
}
4847

4948
if _, err := db.Exec("DROP DATABASE IF EXISTS " + dbName); err != nil {
50-
panic(errors.Wrap(err, "could not drop database"))
49+
panic(fmt.Errorf("dropping database: %w", err))
5150
}
5251

5352
if err := db.Close(); err != nil {

backend/redis/events.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package redis
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67

78
"github.com/cschleiden/go-workflows/internal/core"
89
"github.com/cschleiden/go-workflows/internal/history"
910
"github.com/go-redis/redis/v8"
10-
"github.com/pkg/errors"
1111
)
1212

1313
func addEventToStream(ctx context.Context, rdb redis.UniversalClient, streamKey string, event *history.Event) (*string, error) {
@@ -24,7 +24,7 @@ func addEventToStream(ctx context.Context, rdb redis.UniversalClient, streamKey
2424
},
2525
}).Result()
2626
if err != nil {
27-
return nil, errors.Wrap(err, "could not add event to stream")
27+
return nil, fmt.Errorf("adding event to stream: %w", err)
2828
}
2929

3030
return &msgID, nil
@@ -45,7 +45,7 @@ func addFutureEvent(ctx context.Context, rdb redis.UniversalClient, instance *co
4545
Member: eventData,
4646
Score: float64(event.VisibleAt.Unix()),
4747
}).Err(); err != nil {
48-
return errors.Wrap(err, "could not add future event")
48+
return fmt.Errorf("adding future event: %w", err)
4949
}
5050

5151
return nil

0 commit comments

Comments
 (0)