Skip to content

Commit c907db8

Browse files
authored
Merge pull request #148 from xataio/fix-snapshot-errors
Treat snapshot errors as strings for recording
2 parents 118b391 + c66b142 commit c907db8

File tree

6 files changed

+401
-47
lines changed

6 files changed

+401
-47
lines changed

pkg/snapshot/errors.go

+29-28
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,21 @@
22

33
package snapshot
44

5-
import "errors"
5+
import (
6+
"errors"
7+
"strings"
8+
)
69

710
type Errors struct {
8-
Snapshot error `json:"snapshot,omitempty"`
9-
Tables []TableError `json:"tables,omitempty"`
11+
SnapshotErrMsgs []string `json:"snapshot,omitempty"`
12+
Tables []TableError `json:"tables,omitempty"`
1013
}
1114

1215
type TableError struct {
1316
Table string `json:"table"`
1417
ErrorMsg string `json:"error"`
1518
}
1619

17-
func (e *Errors) Error() string {
18-
var err error
19-
if e.Snapshot != nil {
20-
err = e.Snapshot
21-
}
22-
23-
for _, table := range e.Tables {
24-
if err == nil {
25-
err = table
26-
continue
27-
}
28-
err = errors.Join(err, table)
29-
}
30-
return err.Error()
31-
}
32-
3320
func NewErrors(err error) *Errors {
3421
if err == nil {
3522
return nil
@@ -39,30 +26,44 @@ func NewErrors(err error) *Errors {
3926
return snapshotErrs
4027
}
4128
return &Errors{
42-
Snapshot: err,
29+
SnapshotErrMsgs: []string{err.Error()},
4330
}
4431
}
4532

33+
func (e *Errors) Error() string {
34+
if e == nil {
35+
return ""
36+
}
37+
errMsgs := make([]string, 0, len(e.SnapshotErrMsgs))
38+
errMsgs = append(errMsgs, e.SnapshotErrMsgs...)
39+
40+
for _, table := range e.Tables {
41+
errMsgs = append(errMsgs, table.Error())
42+
}
43+
return strings.Join(errMsgs, ";")
44+
}
45+
4646
func (e *Errors) AddSnapshotError(err error) {
4747
if e == nil {
48-
e = &Errors{}
49-
e.AddSnapshotError(err)
5048
return
5149
}
52-
53-
if e.Snapshot == nil {
54-
e.Snapshot = err
50+
if e.SnapshotErrMsgs == nil {
51+
e.SnapshotErrMsgs = []string{err.Error()}
5552
return
5653
}
57-
e.Snapshot = errors.Join(e.Snapshot, err)
54+
e.SnapshotErrMsgs = append(e.SnapshotErrMsgs, err.Error())
55+
}
56+
57+
func (e *Errors) IsSnapshotError() bool {
58+
return e != nil && len(e.SnapshotErrMsgs) > 0
5859
}
5960

6061
func (e *Errors) IsTableError(table string) bool {
6162
if e == nil {
6263
return false
6364
}
6465

65-
if e.Snapshot != nil {
66+
if e.SnapshotErrMsgs != nil {
6667
return true
6768
}
6869

@@ -99,5 +100,5 @@ func NewTableError(table string, err error) TableError {
99100
}
100101

101102
func (e TableError) Error() string {
102-
return "snapshot error for table " + e.Table + ": " + e.ErrorMsg
103+
return e.Table + ": " + e.ErrorMsg
103104
}

0 commit comments

Comments
 (0)