Skip to content

Commit 0f0a95a

Browse files
committed
wip: updating errors
* Related #9 [ci skip]
1 parent e030928 commit 0f0a95a

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

src/QUICConnection.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@matrixai/async-init/dist/CreateDestroy';
1313
import Logger from '@matrixai/logger';
1414
import { Lock } from '@matrixai/async-locks';
15+
import { destroyed } from '@matrixai/async-init';
1516
import QUICStream from './QUICStream';
1617
import { quiche } from './native';
1718
import * as events from './events';
@@ -291,11 +292,7 @@ class QUICConnection extends EventTarget {
291292
await this.closedP;
292293
this.logger.debug('closeP resolved');
293294
this.connectionMap.delete(this.connectionId);
294-
// Checking for errors
295-
const localError = this.conn.localError();
296-
const peerError = this.conn.peerError();
297-
if (localError != null) this.logger.error(JSON.stringify(localError));
298-
if (peerError != null) this.logger.error(JSON.stringify(peerError));
295+
// Checking if timed out
299296
if (this.conn.isTimedOut()) {
300297
this.logger.error('Connection timed out');
301298
this.dispatchEvent(
@@ -345,11 +342,31 @@ class QUICConnection extends EventTarget {
345342
this.conn.recv(data, recvInfo);
346343
} catch (e) {
347344
this.logger.error(e.message);
348-
// Console.error(e);
349-
// console.log(this.conn.isClosed());
350345
// Depending on the exception, the `this.conn.recv`
351346
// may have automatically started closing the connection
352-
this.dispatchEvent(new events.QUICConnectionErrorEvent({ detail: e }));
347+
if (e.message === 'TlsFail') {
348+
const newError = new errors.ErrorQUICConnectionTLSFailure(undefined, {
349+
data: {
350+
localError: this.conn.localError(),
351+
peerError: this.conn.peerError(),
352+
},
353+
});
354+
this.dispatchEvent(
355+
new events.QUICConnectionErrorEvent({ detail: newError }),
356+
);
357+
} else {
358+
this.dispatchEvent(
359+
new events.QUICConnectionErrorEvent({
360+
detail: new errors.ErrorQUICConnection(e.message, {
361+
cause: e,
362+
data: {
363+
localError: this.conn.localError(),
364+
peerError: this.conn.peerError(),
365+
},
366+
}),
367+
}),
368+
);
369+
}
353370
return;
354371
}
355372
// Here we can resolve our promises!
@@ -483,7 +500,15 @@ class QUICConnection extends EventTarget {
483500
}
484501
}
485502
this.dispatchEvent(
486-
new events.QUICConnectionErrorEvent({ detail: e }),
503+
new events.QUICConnectionErrorEvent({
504+
detail: new errors.ErrorQUICConnection(e.message, {
505+
cause: e,
506+
data: {
507+
localError: this.conn.localError(),
508+
peerError: this.conn.peerError(),
509+
},
510+
}),
511+
}),
487512
);
488513
return;
489514
}
@@ -610,7 +635,7 @@ class QUICConnection extends EventTarget {
610635
`state are draining: ${this.conn.isDraining()}, closed: ${this.conn.isClosed()}`,
611636
);
612637
this.logger.debug('timeout SEND');
613-
await this.send();
638+
if (this[destroyed] === false) await this.send();
614639
this.logger.debug('timeout SENDAFTER');
615640
if (
616641
this[status] !== 'destroying' &&

src/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class ErrorQUICConnectionTimeout<T> extends ErrorQUICConnection<T> {
7070
static description = 'QUIC Connection reached idle timeout';
7171
}
7272

73+
class ErrorQUICConnectionTLSFailure<T> extends ErrorQUICConnection<T> {
74+
static description = 'QUIC connection had failure with TLS negotiation';
75+
}
76+
7377
class ErrorQUICStream<T> extends ErrorQUIC<T> {
7478
static description = 'QUIC Stream error';
7579
}
@@ -109,6 +113,7 @@ export {
109113
ErrorQUICConnection,
110114
ErrorQUICConnectionDestroyed,
111115
ErrorQUICConnectionTimeout,
116+
ErrorQUICConnectionTLSFailure,
112117
ErrorQUICStream,
113118
ErrorQUICStreamDestroyed,
114119
ErrorQUICStreamLocked,

tests/QUICClient.test.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ import * as testsUtils from './utils';
1010
import * as certFixtures from './fixtures/certFixtures';
1111
import * as tlsUtils from './tlsUtils';
1212

13-
const tlsArb = fc.oneof(
14-
certFixtures.tlsConfigExampleArb,
15-
tlsUtils.tlsConfigArb(),
16-
);
1713
const tlsConfigWithCaArb = fc
1814
.oneof(
19-
// Fc.record({
20-
// type: fc.constant('RSA'),
21-
// ca: fc.constant(certFixtures.tlsConfigMemRSACa),
22-
// tlsConfig: certFixtures.tlsConfigRSAExampleArb,
23-
// }),
24-
// fc.record({
25-
// type: fc.constant('OKP'),
26-
// ca: fc.constant(certFixtures.tlsConfigMemOKPCa),
27-
// tlsConfig: certFixtures.tlsConfigOKPExampleArb,
28-
// }),
29-
// fc.record({
30-
// type: fc.constant('ECDSA'),
31-
// ca: fc.constant(certFixtures.tlsConfigMemECDSACa),
32-
// tlsConfig: certFixtures.tlsConfigECDSAExampleArb,
33-
// }),
15+
fc.record({
16+
type: fc.constant('RSA'),
17+
ca: fc.constant(certFixtures.tlsConfigMemRSACa),
18+
tlsConfig: certFixtures.tlsConfigRSAExampleArb,
19+
}),
20+
fc.record({
21+
type: fc.constant('OKP'),
22+
ca: fc.constant(certFixtures.tlsConfigMemOKPCa),
23+
tlsConfig: certFixtures.tlsConfigOKPExampleArb,
24+
}),
25+
fc.record({
26+
type: fc.constant('ECDSA'),
27+
ca: fc.constant(certFixtures.tlsConfigMemECDSACa),
28+
tlsConfig: certFixtures.tlsConfigECDSAExampleArb,
29+
}),
3430
tlsUtils.tlsConfigArb().map(async (configProm) => {
3531
const config = await configProm;
3632
return {

0 commit comments

Comments
 (0)