forked from mongodb/mongo-go-driver
/
errors.go
111 lines (97 loc) · 3.05 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) MongoDB, Inc. 2022-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package topology
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo/description"
)
// ConnectionError represents a connection error.
type ConnectionError struct {
ConnectionID string
Wrapped error
// init will be set to true if this error occurred during connection initialization or
// during a connection handshake.
init bool
message string
}
// Error implements the error interface.
func (e ConnectionError) Error() string {
message := e.message
if e.init {
fullMsg := "error occurred during connection handshake"
if message != "" {
fullMsg = fmt.Sprintf("%s: %s", fullMsg, message)
}
message = fullMsg
}
if e.Wrapped != nil && message != "" {
return fmt.Sprintf("connection(%s) %s: %s", e.ConnectionID, message, e.Wrapped.Error())
}
if e.Wrapped != nil {
return fmt.Sprintf("connection(%s) %s", e.ConnectionID, e.Wrapped.Error())
}
return fmt.Sprintf("connection(%s) %s", e.ConnectionID, message)
}
// Unwrap returns the underlying error.
func (e ConnectionError) Unwrap() error {
return e.Wrapped
}
// ServerSelectionError represents a Server Selection error.
type ServerSelectionError struct {
Desc description.Topology
Wrapped error
}
// Error implements the error interface.
func (e ServerSelectionError) Error() string {
if e.Wrapped != nil {
return fmt.Sprintf("server selection error: %s, current topology: { %s }", e.Wrapped.Error(), e.Desc.String())
}
return fmt.Sprintf("server selection error: current topology: { %s }", e.Desc.String())
}
// Unwrap returns the underlying error.
func (e ServerSelectionError) Unwrap() error {
return e.Wrapped
}
// WaitQueueTimeoutError represents a timeout when requesting a connection from the pool
type WaitQueueTimeoutError struct {
Wrapped error
PinnedCursorConnections uint64
PinnedTransactionConnections uint64
maxPoolSize uint64
totalConnectionCount int
}
// Error implements the error interface.
func (w WaitQueueTimeoutError) Error() string {
errorMsg := "timed out while checking out a connection from connection pool"
switch w.Wrapped {
case nil:
case context.Canceled:
errorMsg = fmt.Sprintf(
"%s: %s",
"canceled while checking out a connection from connection pool",
w.Wrapped.Error(),
)
default:
errorMsg = fmt.Sprintf(
"%s: %s",
errorMsg,
w.Wrapped.Error(),
)
}
return fmt.Sprintf(
"%s; maxPoolSize: %d, connections in use by cursors: %d"+
", connections in use by transactions: %d, connections in use by other operations: %d",
errorMsg,
w.maxPoolSize,
w.PinnedCursorConnections,
w.PinnedTransactionConnections,
uint64(w.totalConnectionCount)-w.PinnedCursorConnections-w.PinnedTransactionConnections)
}
// Unwrap returns the underlying error.
func (w WaitQueueTimeoutError) Unwrap() error {
return w.Wrapped
}