Skip to content

Commit ad8ad42

Browse files
authored
Map Websocket error codes to short descriptions (#2601)
1 parent f311e5d commit ad8ad42

File tree

6 files changed

+232
-469
lines changed

6 files changed

+232
-469
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changes:
99
- When cloning an API instance the registry is shared with the source
1010
- Optimize derive `receivedHeartbeats` to not re-create the full object
1111
- Add `staking.stakerPrefs` derive to retrieve validatorPrefs over a range of eras
12+
- Basic map of Websocket error codes to short descriptions (where none available)
1213

1314

1415
## 1.32.1 Sep 7, 2020

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
"@babel/core": "^7.11.6",
2929
"@babel/register": "^7.11.5",
3030
"@babel/runtime": "^7.11.2",
31-
"@polkadot/dev": "^0.55.48",
32-
"@polkadot/ts": "^0.3.44",
31+
"@polkadot/dev": "^0.55.51",
32+
"@polkadot/ts": "^0.3.46",
3333
"@polkadot/typegen": "workspace:packages/typegen",
3434
"@types/jest": "^26.0.13",
3535
"@vuepress/plugin-search": "^1.5.4",
3636
"copyfiles": "^2.3.0",
3737
"typedoc": "^0.19.1",
3838
"typedoc-plugin-markdown": "^2.4.2",
39-
"typedoc-plugin-no-inherit": "^1.1.10"
39+
"typedoc-plugin-no-inherit": "^1.2.0"
4040
},
4141
"version": "1.33.0-beta.5"
4242
}

packages/rpc-provider/src/ws/Provider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { assert, isNull, isUndefined, isChildClass, logger } from '@polkadot/uti
1111

1212
import Coder from '../coder';
1313
import defaults from '../defaults';
14+
import { getWSErrorString } from './errors';
1415
import getWSClass from './getWSClass';
1516

1617
interface SubscriptionHandler {
@@ -314,7 +315,7 @@ export default class WsProvider implements ProviderInterface {
314315

315316
#onSocketClose = (event: CloseEvent): void => {
316317
if (this.#autoConnectMs > 0) {
317-
l.error(`disconnected from ${this.#endpoints[this.#endpointIndex]} code: '${event.code}' reason: '${event.reason}'`);
318+
l.error(`disconnected from ${this.#endpoints[this.#endpointIndex]}: ${event.code}:: ${event.reason || getWSErrorString(event.code)}`);
318319
}
319320

320321
this.#isConnected = false;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2017-2020 @polkadot/rpc-provider authors & contributors
2+
// This software may be modified and distributed under the terms
3+
// of the Apache-2.0 license. See the LICENSE file for details.
4+
5+
// from https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006
6+
7+
const specificStatusCodeMappings: Record<number, string> = {
8+
1000: 'Normal Closure',
9+
1001: 'Going Away',
10+
1002: 'Protocol Error',
11+
1003: 'Unsupported Data',
12+
1004: '(For future)',
13+
1005: 'No Status Received',
14+
1006: 'Abnormal Closure',
15+
1007: 'Invalid frame payload data',
16+
1008: 'Policy Violation',
17+
1009: 'Message too big',
18+
1010: 'Missing Extension',
19+
1011: 'Internal Error',
20+
1012: 'Service Restart',
21+
1013: 'Try Again Later',
22+
1014: 'Bad Gateway',
23+
1015: 'TLS Handshake'
24+
};
25+
26+
export function getWSErrorString (code: number): string {
27+
if (code >= 0 && code <= 999) {
28+
return '(Unused)';
29+
} else if (code >= 1016) {
30+
if (code <= 1999) {
31+
return '(For WebSocket standard)';
32+
} else if (code <= 2999) {
33+
return '(For WebSocket extensions)';
34+
} else if (code <= 3999) {
35+
return '(For libraries and frameworks)';
36+
} else if (code <= 4999) {
37+
return '(For applications)';
38+
}
39+
}
40+
41+
return specificStatusCodeMappings[code] || '(Unknown)';
42+
}

packages/typegen/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@polkadot/util": "^3.4.1",
4444
"handlebars": "^4.7.6",
4545
"websocket": "^1.0.32",
46-
"yargs": "^15.4.1"
46+
"yargs": "^16.0.3"
4747
},
4848
"devDependencies": {
4949
"@types/websocket": "^1.0.1",

0 commit comments

Comments
 (0)