Skip to content

Commit db1d274

Browse files
refactor: rename ERROR to CONNECT_ERROR
The meaning is not modified: this packet type is still used by the server when the connection to a namespace is refused. But I feel the name makes more sense: ```js socket.on("connect", () => {}); socket.on("connect_error", () => {}); // instead of socket.on("error", () => {}); ```
1 parent e3d272f commit db1d274

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export enum PacketType {
1717
DISCONNECT,
1818
EVENT,
1919
ACK,
20-
ERROR,
20+
CONNECT_ERROR,
2121
BINARY_EVENT,
2222
BINARY_ACK,
2323
}
@@ -241,8 +241,8 @@ export class Decoder extends Emitter {
241241
return typeof payload === "object";
242242
case PacketType.DISCONNECT:
243243
return payload === undefined;
244-
case PacketType.ERROR:
245-
return typeof payload === "string";
244+
case PacketType.CONNECT_ERROR:
245+
return typeof payload === "string" || typeof payload === "object";
246246
case PacketType.EVENT:
247247
case PacketType.BINARY_EVENT:
248248
return Array.isArray(payload) && typeof payload[0] === "string";

test/parser.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("parser", () => {
88
expect(PacketType.DISCONNECT).to.be.a("number");
99
expect(PacketType.EVENT).to.be.a("number");
1010
expect(PacketType.ACK).to.be.a("number");
11-
expect(PacketType.ERROR).to.be.a("number");
11+
expect(PacketType.CONNECT_ERROR).to.be.a("number");
1212
expect(PacketType.BINARY_EVENT).to.be.a("number");
1313
expect(PacketType.BINARY_ACK).to.be.a("number");
1414
});
@@ -71,17 +71,30 @@ describe("parser", () => {
7171
);
7272
});
7373

74-
it("encodes an error", (done) => {
74+
it("encodes an connect error", (done) => {
7575
helpers.test(
7676
{
77-
type: PacketType.ERROR,
77+
type: PacketType.CONNECT_ERROR,
7878
data: "Unauthorized",
7979
nsp: "/",
8080
},
8181
done
8282
);
8383
});
8484

85+
it("encodes an connect error (with object)", (done) => {
86+
helpers.test(
87+
{
88+
type: PacketType.CONNECT_ERROR,
89+
data: {
90+
message: "Unauthorized",
91+
},
92+
nsp: "/",
93+
},
94+
done
95+
);
96+
});
97+
8598
it("throws an error when encoding circular objects", () => {
8699
const a = {};
87100
a.b = a;

0 commit comments

Comments
 (0)