Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add SocketError #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ export class Socket {
});

this.socket.on('error', (err) => {
this.closedReject(err);
if (err instanceof Error) {
this.closedReject(new SocketError(err.message));
} else {
this.closedReject(new SocketError(err as string));
}
});

// types are wrong. fixed based on docs https://nodejs.org/dist/latest/docs/api/stream.html#streamduplextowebstreamduplex
Expand All @@ -92,10 +96,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 +108,10 @@ export class Socket {
}
}

export class SocketError extends TypeError {
constructor(message: string) {
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