# Error Handling

## Connection Error Codes

| Status | Code              | Reason                      | Resolution                         |
| ------ | ----------------- | --------------------------- | ---------------------------------- |
| 400    | Bad Request       | Invalid `customerid` format | Use numeric characters only        |
| 401    | Unauthorized      | Missing `customerid` header | Add `customerid` to `extraHeaders` |
| 402    | Payment Required  | Account inactive or expired | Contact LSports support            |
| 403    | Forbidden         | Customer ID not found       | Verify your Customer ID            |
| 429    | Too Many Requests | Rate limit exceeded         | Wait before reconnecting           |
| 500    | Internal Error    | Server error                | Retry with backoff                 |

***

## Error Response Handling

```javascript
socket.on("connect_error", (error) => {
  console.error("Connection error:", error.message);

  const statusCode = error.data?.statusCode;

  switch (statusCode) {
    case 400:
      console.error("Invalid customerid format — use numeric characters only");
      break;
    case 401:
      console.error("Missing customerid header — add it to extraHeaders");
      break;
    case 402:
      console.error("Account inactive or expired — contact LSports support");
      break;
    case 403:
      console.error("Customer not authorized — verify your Customer ID");
      break;
    case 429:
      console.error("Rate limited — wait before retrying");
      break;
    default:
      console.error("Connection failed — will retry:", error.message);
  }
});
```

***

## WebSocket Close Codes

| Code | Reason                              |
| ---- | ----------------------------------- |
| 1000 | Normal closure                      |
| 1001 | Server going away (maintenance)     |
| 4401 | Authentication failed after upgrade |
| 4403 | Authorization revoked               |

***

## Troubleshooting

| Issue                       | Possible Cause                    | Solution                               |
| --------------------------- | --------------------------------- | -------------------------------------- |
| `Missing customerid header` | Header not included in connection | Add `customerid` to `extraHeaders`     |
| `Connection timeout`        | Network or firewall issues        | Check connectivity; whitelist WSS port |
| `Invalid customerid format` | Non-numeric customer ID           | Use numeric Customer ID only           |
| `Account inactive`          | Subscription expired              | Contact LSports support                |
| `No messages received`      | Not joined to room                | Emit `join` event after connecting     |

***

## Support

For persistent issues, contact LSports support with:

* Your Customer ID
* Error messages received
* Timestamp of the issue

***

## Related

* [Quick Start & Best Practices](https://docs.lsports.eu/u/defend/websocket-connection/quick-start-and-best-practices)
* [Code Examples](https://docs.lsports.eu/u/defend/websocket-connection/code-examples)
* [Authentication Guide](https://docs.lsports.eu/u/defend/integration-guide/authentication)
