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

errors.go 6.7 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package mongo
  7. import (
  8. "bytes"
  9. "errors"
  10. "fmt"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/x/mongo/driver"
  13. "go.mongodb.org/mongo-driver/x/mongo/driver/topology"
  14. )
  15. // ErrUnacknowledgedWrite is returned from functions that have an unacknowledged
  16. // write concern.
  17. var ErrUnacknowledgedWrite = errors.New("unacknowledged write")
  18. // ErrClientDisconnected is returned when a user attempts to call a method on a
  19. // disconnected client
  20. var ErrClientDisconnected = errors.New("client is disconnected")
  21. // ErrNilDocument is returned when a user attempts to pass a nil document or filter
  22. // to a function where the field is required.
  23. var ErrNilDocument = errors.New("document is nil")
  24. // ErrEmptySlice is returned when a user attempts to pass an empty slice as input
  25. // to a function wehere the field is required.
  26. var ErrEmptySlice = errors.New("must provide at least one element in input slice")
  27. func replaceErrors(err error) error {
  28. if err == topology.ErrTopologyClosed {
  29. return ErrClientDisconnected
  30. }
  31. if de, ok := err.(driver.Error); ok {
  32. return CommandError{Code: de.Code, Message: de.Message, Labels: de.Labels, Name: de.Name}
  33. }
  34. if qe, ok := err.(driver.QueryFailureError); ok {
  35. // qe.Message is "command failure"
  36. ce := CommandError{Name: qe.Message}
  37. dollarErr, err := qe.Response.LookupErr("$err")
  38. if err == nil {
  39. ce.Message, _ = dollarErr.StringValueOK()
  40. }
  41. code, err := qe.Response.LookupErr("code")
  42. if err == nil {
  43. ce.Code, _ = code.Int32OK()
  44. }
  45. return ce
  46. }
  47. return err
  48. }
  49. // CommandError represents an error in execution of a command against the database.
  50. type CommandError struct {
  51. Code int32
  52. Message string
  53. Labels []string
  54. Name string
  55. }
  56. // Error implements the error interface.
  57. func (e CommandError) Error() string {
  58. if e.Name != "" {
  59. return fmt.Sprintf("(%v) %v", e.Name, e.Message)
  60. }
  61. return e.Message
  62. }
  63. // HasErrorLabel returns true if the error contains the specified label.
  64. func (e CommandError) HasErrorLabel(label string) bool {
  65. if e.Labels != nil {
  66. for _, l := range e.Labels {
  67. if l == label {
  68. return true
  69. }
  70. }
  71. }
  72. return false
  73. }
  74. // IsMaxTimeMSExpiredError indicates if the error is a MaxTimeMSExpiredError.
  75. func (e CommandError) IsMaxTimeMSExpiredError() bool {
  76. return e.Code == 50 || e.Name == "MaxTimeMSExpired"
  77. }
  78. // WriteError is a non-write concern failure that occurred as a result of a write
  79. // operation.
  80. type WriteError struct {
  81. Index int
  82. Code int
  83. Message string
  84. }
  85. func (we WriteError) Error() string { return we.Message }
  86. // WriteErrors is a group of non-write concern failures that occurred as a result
  87. // of a write operation.
  88. type WriteErrors []WriteError
  89. func (we WriteErrors) Error() string {
  90. var buf bytes.Buffer
  91. fmt.Fprint(&buf, "write errors: [")
  92. for idx, err := range we {
  93. if idx != 0 {
  94. fmt.Fprintf(&buf, ", ")
  95. }
  96. fmt.Fprintf(&buf, "{%s}", err)
  97. }
  98. fmt.Fprint(&buf, "]")
  99. return buf.String()
  100. }
  101. func writeErrorsFromDriverWriteErrors(errs driver.WriteErrors) WriteErrors {
  102. wes := make(WriteErrors, 0, len(errs))
  103. for _, err := range errs {
  104. wes = append(wes, WriteError{Index: int(err.Index), Code: int(err.Code), Message: err.Message})
  105. }
  106. return wes
  107. }
  108. // WriteConcernError is a write concern failure that occurred as a result of a
  109. // write operation.
  110. type WriteConcernError struct {
  111. Name string
  112. Code int
  113. Message string
  114. Details bson.Raw
  115. }
  116. func (wce WriteConcernError) Error() string {
  117. if wce.Name != "" {
  118. return fmt.Sprintf("(%v) %v", wce.Name, wce.Message)
  119. }
  120. return wce.Message
  121. }
  122. // WriteException is an error for a non-bulk write operation.
  123. type WriteException struct {
  124. WriteConcernError *WriteConcernError
  125. WriteErrors WriteErrors
  126. }
  127. func (mwe WriteException) Error() string {
  128. var buf bytes.Buffer
  129. fmt.Fprint(&buf, "multiple write errors: [")
  130. fmt.Fprintf(&buf, "{%s}, ", mwe.WriteErrors)
  131. fmt.Fprintf(&buf, "{%s}]", mwe.WriteConcernError)
  132. return buf.String()
  133. }
  134. func convertDriverWriteConcernError(wce *driver.WriteConcernError) *WriteConcernError {
  135. if wce == nil {
  136. return nil
  137. }
  138. return &WriteConcernError{Code: int(wce.Code), Message: wce.Message, Details: bson.Raw(wce.Details)}
  139. }
  140. // BulkWriteError is an error for one operation in a bulk write.
  141. type BulkWriteError struct {
  142. WriteError
  143. Request WriteModel
  144. }
  145. func (bwe BulkWriteError) Error() string {
  146. var buf bytes.Buffer
  147. fmt.Fprintf(&buf, "{%s}", bwe.WriteError)
  148. return buf.String()
  149. }
  150. // BulkWriteException is an error for a bulk write operation.
  151. type BulkWriteException struct {
  152. WriteConcernError *WriteConcernError
  153. WriteErrors []BulkWriteError
  154. }
  155. func (bwe BulkWriteException) Error() string {
  156. var buf bytes.Buffer
  157. fmt.Fprint(&buf, "bulk write error: [")
  158. fmt.Fprintf(&buf, "{%s}, ", bwe.WriteErrors)
  159. fmt.Fprintf(&buf, "{%s}]", bwe.WriteConcernError)
  160. return buf.String()
  161. }
  162. // returnResult is used to determine if a function calling processWriteError should return
  163. // the result or return nil. Since the processWriteError function is used by many different
  164. // methods, both *One and *Many, we need a way to differentiate if the method should return
  165. // the result and the error.
  166. type returnResult int
  167. const (
  168. rrNone returnResult = 1 << iota // None means do not return the result ever.
  169. rrOne // One means return the result if this was called by a *One method.
  170. rrMany // Many means return the result is this was called by a *Many method.
  171. rrAll returnResult = rrOne | rrMany // All means always return the result.
  172. )
  173. // processWriteError handles processing the result of a write operation. If the retrunResult matches
  174. // the calling method's type, it should return the result object in addition to the error.
  175. // This function will wrap the errors from other packages and return them as errors from this package.
  176. //
  177. // WriteConcernError will be returned over WriteErrors if both are present.
  178. func processWriteError(err error) (returnResult, error) {
  179. switch {
  180. case err == driver.ErrUnacknowledgedWrite:
  181. return rrAll, ErrUnacknowledgedWrite
  182. case err != nil:
  183. switch tt := err.(type) {
  184. case driver.WriteCommandError:
  185. return rrMany, WriteException{
  186. WriteConcernError: convertDriverWriteConcernError(tt.WriteConcernError),
  187. WriteErrors: writeErrorsFromDriverWriteErrors(tt.WriteErrors),
  188. }
  189. default:
  190. return rrNone, replaceErrors(err)
  191. }
  192. default:
  193. return rrAll, nil
  194. }
  195. }