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.
 
 
 

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