2
2
3
3
package snapshot
4
4
5
- import "errors"
5
+ import (
6
+ "errors"
7
+ "strings"
8
+ )
6
9
7
10
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"`
10
13
}
11
14
12
15
type TableError struct {
13
16
Table string `json:"table"`
14
17
ErrorMsg string `json:"error"`
15
18
}
16
19
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
-
33
20
func NewErrors (err error ) * Errors {
34
21
if err == nil {
35
22
return nil
@@ -39,30 +26,44 @@ func NewErrors(err error) *Errors {
39
26
return snapshotErrs
40
27
}
41
28
return & Errors {
42
- Snapshot : err ,
29
+ SnapshotErrMsgs : [] string { err . Error ()} ,
43
30
}
44
31
}
45
32
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
+
46
46
func (e * Errors ) AddSnapshotError (err error ) {
47
47
if e == nil {
48
- e = & Errors {}
49
- e .AddSnapshotError (err )
50
48
return
51
49
}
52
-
53
- if e .Snapshot == nil {
54
- e .Snapshot = err
50
+ if e .SnapshotErrMsgs == nil {
51
+ e .SnapshotErrMsgs = []string {err .Error ()}
55
52
return
56
53
}
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
58
59
}
59
60
60
61
func (e * Errors ) IsTableError (table string ) bool {
61
62
if e == nil {
62
63
return false
63
64
}
64
65
65
- if e .Snapshot != nil {
66
+ if e .SnapshotErrMsgs != nil {
66
67
return true
67
68
}
68
69
@@ -99,5 +100,5 @@ func NewTableError(table string, err error) TableError {
99
100
}
100
101
101
102
func (e TableError ) Error () string {
102
- return "snapshot error for table " + e .Table + ": " + e .ErrorMsg
103
+ return e .Table + ": " + e .ErrorMsg
103
104
}
0 commit comments