|
4 | 4 | "context" |
5 | 5 | "encoding/json" |
6 | 6 | "fmt" |
| 7 | + "io" |
7 | 8 | "net/http" |
8 | 9 | "net/url" |
9 | 10 | "strings" |
@@ -207,9 +208,60 @@ func (m *Manager) doConnect() error { |
207 | 208 |
|
208 | 209 | m.logger.Debug("Dialing WebSocket", zap.String("url", u.String())) |
209 | 210 |
|
210 | | - conn, _, err := dialer.DialContext(m.ctx, u.String(), headers) |
| 211 | + conn, response, err := dialer.DialContext(m.ctx, u.String(), headers) |
211 | 212 | if err != nil { |
212 | | - return fmt.Errorf("failed to connect to subscription service: %w", err) |
| 213 | + if response != nil { |
| 214 | + // Extract detailed error information from the response |
| 215 | + statusCode := response.StatusCode |
| 216 | + statusText := response.Status |
| 217 | + |
| 218 | + var respBodyStr string |
| 219 | + if response.Body != nil { |
| 220 | + respBytes, readErr := io.ReadAll(response.Body) |
| 221 | + response.Body.Close() |
| 222 | + if readErr == nil { |
| 223 | + respBodyStr = strings.TrimSpace(string(respBytes)) |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // Create a clean error message |
| 228 | + var errorMsg strings.Builder |
| 229 | + errorMsg.WriteString(fmt.Sprintf("HTTP %d (%s)", statusCode, statusText)) |
| 230 | + |
| 231 | + if respBodyStr != "" { |
| 232 | + errorMsg.WriteString(fmt.Sprintf(": %s", respBodyStr)) |
| 233 | + } |
| 234 | + |
| 235 | + return fmt.Errorf("%s", errorMsg.String()) |
| 236 | + } |
| 237 | + |
| 238 | + // Handle cases where response is nil (e.g., network errors, bad handshake) |
| 239 | + var errorMsg strings.Builder |
| 240 | + errorMsg.WriteString("Failed to connect to subscription service") |
| 241 | + |
| 242 | + // Analyze the error type and provide helpful context |
| 243 | + errorStr := err.Error() |
| 244 | + if strings.Contains(errorStr, "bad handshake") { |
| 245 | + errorMsg.WriteString(" - WebSocket handshake failed") |
| 246 | + errorMsg.WriteString("\nThis could be due to:") |
| 247 | + errorMsg.WriteString("\n• Invalid server URL or endpoint") |
| 248 | + errorMsg.WriteString("\n• Server not supporting WebSocket connections") |
| 249 | + errorMsg.WriteString("\n• Network connectivity issues") |
| 250 | + errorMsg.WriteString("\n• Authentication problems") |
| 251 | + } else if strings.Contains(errorStr, "timeout") { |
| 252 | + errorMsg.WriteString(" - Connection timeout") |
| 253 | + errorMsg.WriteString("\nThe server may be overloaded or unreachable") |
| 254 | + } else if strings.Contains(errorStr, "refused") { |
| 255 | + errorMsg.WriteString(" - Connection refused") |
| 256 | + errorMsg.WriteString("\nThe server may be down or the port may be blocked") |
| 257 | + } else if strings.Contains(errorStr, "no such host") { |
| 258 | + errorMsg.WriteString(" - DNS resolution failed") |
| 259 | + errorMsg.WriteString("\nCheck the server hostname in your configuration") |
| 260 | + } |
| 261 | + |
| 262 | + errorMsg.WriteString(fmt.Sprintf("\nOriginal error: %v", err)) |
| 263 | + |
| 264 | + return fmt.Errorf("%s", errorMsg.String()) |
213 | 265 | } |
214 | 266 |
|
215 | 267 | m.mu.Lock() |
|
0 commit comments