...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package bigquery
16
17 import (
18 "fmt"
19 "strings"
20
21 bq "google.golang.org/api/bigquery/v2"
22 )
23
24
25
26 type Error struct {
27
28 Location, Message, Reason string
29 }
30
31 func (e Error) Error() string {
32 return fmt.Sprintf("{Location: %q; Message: %q; Reason: %q}", e.Location, e.Message, e.Reason)
33 }
34
35 func bqToError(ep *bq.ErrorProto) *Error {
36 if ep == nil {
37 return nil
38 }
39 return &Error{
40 Location: ep.Location,
41 Message: ep.Message,
42 Reason: ep.Reason,
43 }
44 }
45
46
47 type MultiError []error
48
49 func (m MultiError) Error() string {
50 switch len(m) {
51 case 0:
52 return "(0 errors)"
53 case 1:
54 return m[0].Error()
55 case 2:
56 return m[0].Error() + " (and 1 other error)"
57 }
58 return fmt.Sprintf("%s (and %d other errors)", m[0].Error(), len(m)-1)
59 }
60
61
62 type RowInsertionError struct {
63 InsertID string
64 RowIndex int
65 Errors MultiError
66 }
67
68 func (e *RowInsertionError) Error() string {
69 errFmt := "insertion of row [insertID: %q; insertIndex: %v] failed with error: %s"
70 return fmt.Sprintf(errFmt, e.InsertID, e.RowIndex, e.Errors.Error())
71 }
72
73
74
75 type PutMultiError []RowInsertionError
76
77 func (pme PutMultiError) errorDetails() string {
78 size := len(pme)
79 ellipsis := ""
80 if size == 0 {
81 return ""
82 } else if size > 3 {
83 size = 3
84 ellipsis = ", ..."
85 }
86
87 es := make([]string, size)
88 for i, e := range pme {
89 if i >= size {
90 break
91 }
92 es[i] = e.Error()
93 }
94
95 return fmt.Sprintf(" (%s%s)", strings.Join(es, ", "), ellipsis)
96 }
97
98 func (pme PutMultiError) Error() string {
99 plural := "s"
100 if len(pme) == 1 {
101 plural = ""
102 }
103
104 details := pme.errorDetails()
105 return fmt.Sprintf("%v row insertion%s failed%s", len(pme), plural, details)
106 }
107
View as plain text