Skip to content

Commit

Permalink
src: add SocketError
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Sep 29, 2023
1 parent abd421e commit 5f52461
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Socket {
});

this.socket.on('error', (err) => {
this.closedReject(err);
this.closedReject(new SocketError(err.message ?? err));
});

// types are wrong. fixed based on docs https://nodejs.org/dist/latest/docs/api/stream.html#streamduplextowebstreamduplex
Expand All @@ -92,10 +92,10 @@ export class Socket {

startTls(): Socket {
if (this.secureTransport !== 'starttls') {
throw new Error("secureTransport must be set to 'starttls'");
throw new SocketError("secureTransport must be set to 'starttls'");
}
if (this.startTlsCalled) {
throw new Error('can only call startTls once');
throw new SocketError('can only call startTls once');
} else {
this.startTlsCalled = true;
}
Expand All @@ -104,4 +104,10 @@ export class Socket {
}
}

export class SocketError extends TypeError {
constructor(message: any) {
super(`SocketError: ${message}`);
}
}

export type * from './types';
15 changes: 14 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import net from 'node:net';
import { once } from 'node:events';
import tap from 'tap';
import { connect } from '../src';
import { SocketError, connect } from '../src';
import {
getReaderWriterFromSocket,
listenAndGetSocketAddress,
Expand Down Expand Up @@ -130,3 +130,16 @@ for (const data of [
},
);
}

void tap.test('SocketError is thrown on connect failure', async (t) => {
t.plan(1);

try {
const socket = connect('tcp://127.0.0.1:1234');
await socket.closed;
} catch (err) {
t.same(err, new SocketError('connect ECONNREFUSED 127.0.0.1:1234'));
} finally {
t.end();
}
});
6 changes: 3 additions & 3 deletions test/tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import tls from 'node:tls';
import fs from 'node:fs';
import path from 'node:path';
import tap from 'tap';
import { connect } from '../src';
import { SocketError, connect } from '../src';
import { listenAndGetSocketAddress, writeAndReadSocket } from './utils';

function getTLSServer(): tls.Server {
Expand Down Expand Up @@ -51,7 +51,7 @@ void tap.test('Socket `connect` with TLS', (t) => {
() => {
socket.startTls();
},
new Error("secureTransport must be set to 'starttls'"),
new SocketError("secureTransport must be set to 'starttls'"),
'calling .startTls() throws an error',
);
await socket.close();
Expand Down Expand Up @@ -98,7 +98,7 @@ void tap.test('Socket `connect` with TLS', (t) => {
() => {
socket.startTls();
},
new Error('can only call startTls once'),
new SocketError('can only call startTls once'),
'second call to .startTls() throws an error',
);
await socket.close();
Expand Down

0 comments on commit 5f52461

Please sign in to comment.