summaryrefslogtreecommitdiff
path: root/std/ws
diff options
context:
space:
mode:
authorOliver Lenehan <sunsetkookaburra+github@outlook.com.au>2020-03-14 12:40:13 +1100
committerGitHub <noreply@github.com>2020-03-13 21:40:13 -0400
commit0f6acf275370cae09ffb3f6950a3926424f3b024 (patch)
treee77a2115394f36dcbd899fd8f2d5496ca7bde625 /std/ws
parentaab1acaed163f91aa5e89b079c5312336abb2088 (diff)
fix(std): Use Deno.errors where possible. (#4356)
Diffstat (limited to 'std/ws')
-rw-r--r--std/ws/mod.ts30
-rw-r--r--std/ws/test.ts22
2 files changed, 26 insertions, 26 deletions
diff --git a/std/ws/mod.ts b/std/ws/mod.ts
index 4a103ab79..809654a06 100644
--- a/std/ws/mod.ts
+++ b/std/ws/mod.ts
@@ -2,7 +2,7 @@
import { decode, encode } from "../strings/mod.ts";
import { hasOwnProperty } from "../util/has_own_property.ts";
-import { BufReader, BufWriter, UnexpectedEOFError } from "../io/bufio.ts";
+import { BufReader, BufWriter } from "../io/bufio.ts";
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
import { Sha1 } from "./sha1.ts";
import { writeResponse } from "../http/io.ts";
@@ -71,12 +71,6 @@ export function append(a: Uint8Array, b: Uint8Array): Uint8Array {
return output;
}
-export class SocketClosedError extends Error {
- constructor(msg = "Socket has already been closed") {
- super(msg);
- }
-}
-
export interface WebSocketFrame {
isLastFrame: boolean;
opcode: OpCode;
@@ -91,20 +85,20 @@ export interface WebSocket {
receive(): AsyncIterableIterator<WebSocketEvent>;
/**
- * @throws SocketClosedError
+ * @throws `Deno.errors.ConnectionReset`
*/
send(data: WebSocketMessage): Promise<void>;
/**
* @param data
- * @throws SocketClosedError
+ * @throws `Deno.errors.ConnectionReset`
*/
ping(data?: WebSocketMessage): Promise<void>;
/** Close connection after sending close frame to peer.
* This is canonical way of disconnection but it may hang because of peer's response delay.
* Default close code is 1000 (Normal Closure)
- * @throws SocketClosedError
+ * @throws `Deno.errors.ConnectionReset`
*/
close(): Promise<void>;
close(code: number): Promise<void>;
@@ -164,8 +158,8 @@ export async function writeFrame(
}
/** Read websocket frame from given BufReader
- * @throws UnexpectedEOFError When peer closed connection without close frame
- * @throws Error Frame is invalid
+ * @throws `Deno.errors.UnexpectedEof` When peer closed connection without close frame
+ * @throws `Error` Frame is invalid
*/
export async function readFrame(buf: BufReader): Promise<WebSocketFrame> {
let b = assertNotEOF(await buf.readByte());
@@ -318,7 +312,7 @@ class WebSocketImpl implements WebSocket {
private enqueue(frame: WebSocketFrame): Promise<void> {
if (this._isClosed) {
- throw new SocketClosedError();
+ throw new Deno.errors.ConnectionReset("Socket has already been closed");
}
const d = deferred<void>();
this.sendQueue.push({ d, frame });
@@ -397,7 +391,11 @@ class WebSocketImpl implements WebSocket {
this._isClosed = true;
const rest = this.sendQueue;
this.sendQueue = [];
- rest.forEach(e => e.d.reject(new SocketClosedError()));
+ rest.forEach(e =>
+ e.d.reject(
+ new Deno.errors.ConnectionReset("Socket has already been closed")
+ )
+ );
}
}
}
@@ -495,7 +493,7 @@ export async function handshake(
const tpReader = new TextProtoReader(bufReader);
const statusLine = await tpReader.readLine();
if (statusLine === Deno.EOF) {
- throw new UnexpectedEOFError();
+ throw new Deno.errors.UnexpectedEof();
}
const m = statusLine.match(/^(?<version>\S+) (?<statusCode>\S+) /);
if (!m) {
@@ -513,7 +511,7 @@ export async function handshake(
const responseHeaders = await tpReader.readMIMEHeader();
if (responseHeaders === Deno.EOF) {
- throw new UnexpectedEOFError();
+ throw new Deno.errors.UnexpectedEof();
}
const expectedSecAccept = createSecAccept(key);
diff --git a/std/ws/test.ts b/std/ws/test.ts
index f6c7319c1..e0050bcf4 100644
--- a/std/ws/test.ts
+++ b/std/ws/test.ts
@@ -14,8 +14,7 @@ import {
readFrame,
unmask,
writeFrame,
- createWebSocket,
- SocketClosedError
+ createWebSocket
} from "./mod.ts";
import { encode, decode } from "../strings/mod.ts";
import Writer = Deno.Writer;
@@ -331,7 +330,7 @@ test("[ws] createSecKeyHasCorrectLength", () => {
assertEquals(atob(secKey).length, 16);
});
-test("[ws] WebSocket should throw SocketClosedError when peer closed connection without close frame", async () => {
+test("[ws] WebSocket should throw `Deno.errors.ConnectionReset` when peer closed connection without close frame", async () => {
const buf = new Buffer();
const eofReader: Deno.Reader = {
async read(_: Uint8Array): Promise<number | Deno.EOF> {
@@ -341,12 +340,15 @@ test("[ws] WebSocket should throw SocketClosedError when peer closed connection
const conn = dummyConn(eofReader, buf);
const sock = createWebSocket({ conn });
sock.closeForce();
- await assertThrowsAsync(() => sock.send("hello"), SocketClosedError);
- await assertThrowsAsync(() => sock.ping(), SocketClosedError);
- await assertThrowsAsync(() => sock.close(0), SocketClosedError);
+ await assertThrowsAsync(
+ () => sock.send("hello"),
+ Deno.errors.ConnectionReset
+ );
+ await assertThrowsAsync(() => sock.ping(), Deno.errors.ConnectionReset);
+ await assertThrowsAsync(() => sock.close(0), Deno.errors.ConnectionReset);
});
-test("[ws] WebSocket shouldn't throw UnexpectedEOFError on recive()", async () => {
+test("[ws] WebSocket shouldn't throw `Deno.errors.UnexpectedEof` on recive()", async () => {
const buf = new Buffer();
const eofReader: Deno.Reader = {
async read(_: Uint8Array): Promise<number | Deno.EOF> {
@@ -382,8 +384,8 @@ test("[ws] WebSocket should reject sending promise when connection reset forcely
sock.closeForce();
assertEquals(sock.isClosed, true);
const [a, b, c] = await p;
- assert(a instanceof SocketClosedError);
- assert(b instanceof SocketClosedError);
- assert(c instanceof SocketClosedError);
+ assert(a instanceof Deno.errors.ConnectionReset);
+ assert(b instanceof Deno.errors.ConnectionReset);
+ assert(c instanceof Deno.errors.ConnectionReset);
clearTimeout(timer);
});