You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.2 KiB

  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. package bigquery
  15. import (
  16. "fmt"
  17. bq "google.golang.org/api/bigquery/v2"
  18. )
  19. // An Error contains detailed information about a failed bigquery operation.
  20. type Error struct {
  21. // Mirrors bq.ErrorProto, but drops DebugInfo
  22. Location, Message, Reason string
  23. }
  24. func (e Error) Error() string {
  25. return fmt.Sprintf("{Location: %q; Message: %q; Reason: %q}", e.Location, e.Message, e.Reason)
  26. }
  27. func bqToError(ep *bq.ErrorProto) *Error {
  28. if ep == nil {
  29. return nil
  30. }
  31. return &Error{
  32. Location: ep.Location,
  33. Message: ep.Message,
  34. Reason: ep.Reason,
  35. }
  36. }
  37. // A MultiError contains multiple related errors.
  38. type MultiError []error
  39. func (m MultiError) Error() string {
  40. switch len(m) {
  41. case 0:
  42. return "(0 errors)"
  43. case 1:
  44. return m[0].Error()
  45. case 2:
  46. return m[0].Error() + " (and 1 other error)"
  47. }
  48. return fmt.Sprintf("%s (and %d other errors)", m[0].Error(), len(m)-1)
  49. }
  50. // RowInsertionError contains all errors that occurred when attempting to insert a row.
  51. type RowInsertionError struct {
  52. InsertID string // The InsertID associated with the affected row.
  53. RowIndex int // The 0-based index of the affected row in the batch of rows being inserted.
  54. Errors MultiError
  55. }
  56. func (e *RowInsertionError) Error() string {
  57. errFmt := "insertion of row [insertID: %q; insertIndex: %v] failed with error: %s"
  58. return fmt.Sprintf(errFmt, e.InsertID, e.RowIndex, e.Errors.Error())
  59. }
  60. // PutMultiError contains an error for each row which was not successfully inserted
  61. // into a BigQuery table.
  62. type PutMultiError []RowInsertionError
  63. func (pme PutMultiError) Error() string {
  64. plural := "s"
  65. if len(pme) == 1 {
  66. plural = ""
  67. }
  68. return fmt.Sprintf("%v row insertion%s failed", len(pme), plural)
  69. }