1// Copyright 2015 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bigquery
16
17import (
18	"fmt"
19	"strings"
20
21	bq "google.golang.org/api/bigquery/v2"
22)
23
24// An Error contains detailed information about a failed bigquery operation.
25// Detailed description of possible Reasons can be found here: https://cloud.google.com/bigquery/troubleshooting-errors.
26type Error struct {
27	// Mirrors bq.ErrorProto, but drops DebugInfo
28	Location, Message, Reason string
29}
30
31func (e Error) Error() string {
32	return fmt.Sprintf("{Location: %q; Message: %q; Reason: %q}", e.Location, e.Message, e.Reason)
33}
34
35func 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// A MultiError contains multiple related errors.
47type MultiError []error
48
49func (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// RowInsertionError contains all errors that occurred when attempting to insert a row.
62type RowInsertionError struct {
63	InsertID string // The InsertID associated with the affected row.
64	RowIndex int    // The 0-based index of the affected row in the batch of rows being inserted.
65	Errors   MultiError
66}
67
68func (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// PutMultiError contains an error for each row which was not successfully inserted
74// into a BigQuery table.
75type PutMultiError []RowInsertionError
76
77func (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
98func (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