forked from kikinteractive/go-bqstreamer
/
error.go
153 lines (127 loc) · 4.16 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Here be various error types sent by the streamer.
package bqstreamer
import (
"encoding/json"
"strconv"
"strings"
bigquery "google.golang.org/api/bigquery/v2"
)
// TooManyFailedInsertRetriesError is returned when an insert failed several
// time, and the streamer stops retrying it.
type TooManyFailedInsertRetriesError struct {
numFailedRetries int
projectID,
datasetID,
tableID string
}
func (err *TooManyFailedInsertRetriesError) Error() string {
// The equivalent format is "Insert table %s.%s.%s retried %d times, dropping nsert and moving on"
return strings.Join(
[]string{
"Insert table ",
err.projectID, ".",
err.datasetID, ".",
err.tableID, " ",
"retried ", strconv.Itoa(err.numFailedRetries),
" times, dropping insert and moving on"},
"")
}
func newTooManyFailedInsertRetriesError(
numFailedRetries int,
projectID, datasetID, tableID string) *TooManyFailedInsertRetriesError {
return &TooManyFailedInsertRetriesError{
numFailedRetries: numFailedRetries,
projectID: projectID,
datasetID: datasetID,
tableID: tableID,
}
}
func (err *TooManyFailedInsertRetriesError) NumFailedRetries() int { return err.numFailedRetries }
func (err *TooManyFailedInsertRetriesError) TableID() string { return err.tableID }
func (err *TooManyFailedInsertRetriesError) DatasetID() string { return err.datasetID }
func (err *TooManyFailedInsertRetriesError) ProjectID() string { return err.projectID }
// AllRowsRejectedError is returned when all rows in an insert have been rejected,
// meaning no insert retry will occur.
type AllRowsRejectedError struct {
projectID,
datasetID,
tableID string
}
func (err *AllRowsRejectedError) Error() string {
// The equivalent format is "All rows from %s.%s.%s have been rejected, moving on"
return strings.Join(
[]string{
"All rows from ",
err.projectID, ".",
err.datasetID, ".",
err.tableID, " ",
"have been rejected, moving on"},
"")
}
func newAllRowsRejectedError(projectID, datasetID, tableID string) *AllRowsRejectedError {
return &AllRowsRejectedError{
projectID: projectID,
datasetID: datasetID,
tableID: tableID}
}
func (err *AllRowsRejectedError) TableID() string { return err.tableID }
func (err *AllRowsRejectedError) DatasetID() string { return err.datasetID }
func (err *AllRowsRejectedError) ProjectID() string { return err.projectID }
// RowError is a specific row insert error,
// returned after inserting multiple rows.
type RowError struct {
// Original error information returned from BigQuery.
bqError bigquery.ErrorProto
index int64
projectID,
datasetID,
tableID string
// Row Value
jsonValue map[string]bigquery.JsonValue
}
func (err *RowError) Error() string {
// The equivalent format is "%s.%s.%s.row[%d]: %s in %s: %s: %s"
return strings.Join(
[]string{
err.projectID, ".",
err.datasetID, ".",
err.tableID, ".",
"row[", strconv.FormatInt(err.index, 10), "]: ",
err.bqError.Reason, " ",
"in ", err.bqError.Location, ": ",
err.bqError.Message, ": ",
err.jsonValueToString()},
"")
}
func newRowError(
bqError bigquery.ErrorProto,
index int64,
projectID, datasetID, tableID string,
jsonValue map[string]bigquery.JsonValue) *RowError {
return &RowError{
bqError: bqError,
index: index,
projectID: projectID,
datasetID: datasetID,
tableID: tableID,
jsonValue: jsonValue,
}
}
func (err *RowError) BQError() bigquery.ErrorProto { return err.bqError }
func (err *RowError) Index() int64 { return err.index }
func (err *RowError) TableID() string { return err.tableID }
func (err *RowError) DatasetID() string { return err.datasetID }
func (err *RowError) ProjectID() string { return err.projectID }
func (err *RowError) JsonValue() map[string]bigquery.JsonValue { return err.jsonValue }
// jsonValueToString is a helper function that marshals err.jsonValue to a JSON
// string.
// It's used in Error() string representation.
// Returns an empty string on error.
func (err *RowError) jsonValueToString() string {
var b []byte
var e error
if b, e = json.Marshal(err.jsonValue); e != nil {
return ""
}
return string(b)
}