Skip to content

Commit

Permalink
Merge pull request #1206 from ably/connection-errors-no-early-init
Browse files Browse the repository at this point in the history
refactor: avoid early initialisation of connection errors
  • Loading branch information
owenpearson authored Apr 26, 2023
2 parents 1ef4028 + 50062e1 commit 94eafe0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/common/lib/client/realtimechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ class RealtimeChannel extends Channel {
'RealtimeChannel.onMessage()',
'Fatal protocol error: unrecognised action (' + message.action + ')'
);
this.connectionManager.abort(ConnectionErrors.unknownChannelErr);
this.connectionManager.abort(ConnectionErrors.unknownChannelErr());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/lib/transport/comettransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ abstract class CometTransport extends Transport {
}
/* In almost all cases the transport will be finished before it's
* disposed. Finish here just to make sure. */
this.finish('disconnected', ConnectionErrors.disconnected);
this.finish('disconnected', ConnectionErrors.disconnected());
Platform.Config.nextTick(() => {
this.emit('disposed');
});
Expand Down
95 changes: 53 additions & 42 deletions src/common/lib/transport/connectionerrors.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
import ErrorInfo from '../types/errorinfo';

const ConnectionErrorCodes = {
DISCONNECTED: 80003,
SUSPENDED: 80002,
FAILED: 80000,
CLOSING: 80017,
CLOSED: 80017,
UNKNOWN_CONNECTION_ERR: 50002,
UNKNOWN_CHANNEL_ERR: 50001,
};

const ConnectionErrors = {
disconnected: ErrorInfo.fromValues({
statusCode: 400,
code: 80003,
message: 'Connection to server temporarily unavailable',
}),
suspended: ErrorInfo.fromValues({
statusCode: 400,
code: 80002,
message: 'Connection to server unavailable',
}),
failed: ErrorInfo.fromValues({
statusCode: 400,
code: 80000,
message: 'Connection failed or disconnected by server',
}),
closing: ErrorInfo.fromValues({
statusCode: 400,
code: 80017,
message: 'Connection closing',
}),
closed: ErrorInfo.fromValues({
statusCode: 400,
code: 80017,
message: 'Connection closed',
}),
unknownConnectionErr: ErrorInfo.fromValues({
statusCode: 500,
code: 50002,
message: 'Internal connection error',
}),
unknownChannelErr: ErrorInfo.fromValues({
statusCode: 500,
code: 50001,
message: 'Internal channel error',
}),
disconnected: () =>
ErrorInfo.fromValues({
statusCode: 400,
code: ConnectionErrorCodes.DISCONNECTED,
message: 'Connection to server temporarily unavailable',
}),
suspended: () =>
ErrorInfo.fromValues({
statusCode: 400,
code: ConnectionErrorCodes.SUSPENDED,
message: 'Connection to server unavailable',
}),
failed: () =>
ErrorInfo.fromValues({
statusCode: 400,
code: ConnectionErrorCodes.FAILED,
message: 'Connection failed or disconnected by server',
}),
closing: () =>
ErrorInfo.fromValues({
statusCode: 400,
code: ConnectionErrorCodes.CLOSING,
message: 'Connection closing',
}),
closed: () =>
ErrorInfo.fromValues({
statusCode: 400,
code: ConnectionErrorCodes.CLOSED,
message: 'Connection closed',
}),
unknownConnectionErr: () =>
ErrorInfo.fromValues({
statusCode: 500,
code: ConnectionErrorCodes.UNKNOWN_CONNECTION_ERR,
message: 'Internal connection error',
}),
unknownChannelErr: () =>
ErrorInfo.fromValues({
statusCode: 500,
code: ConnectionErrorCodes.UNKNOWN_CONNECTION_ERR,
message: 'Internal channel error',
}),
};

export function isRetriable(err: ErrorInfo) {
if (!err.statusCode || !err.code || err.statusCode >= 500) {
return true;
}
let retriable = false;
Object.values(ConnectionErrors).forEach(function (connErr) {
if (connErr.code && connErr.code == err.code) {
retriable = true;
}
});
return retriable;
return Object.values(ConnectionErrorCodes).includes(err.code);
}

export default ConnectionErrors;
6 changes: 3 additions & 3 deletions src/common/lib/transport/connectionmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ class ConnectionManager extends EventEmitter {
}

getStateError(): ErrorInfo {
return (ConnectionErrors as Record<string, ErrorInfo>)[this.state.state];
return (ConnectionErrors as Record<string, () => ErrorInfo>)[this.state.state]?.();
}

activeState(): boolean | void {
Expand Down Expand Up @@ -1330,7 +1330,7 @@ class ConnectionManager extends EventEmitter {
this.state.state,
newState.state,
retryDelay,
indicated.error || (ConnectionErrors as Record<string, ErrorInfo>)[newState.state]
indicated.error || (ConnectionErrors as Partial<Record<string, () => ErrorInfo>>)[newState.state]?.()
);

if (retryImmediately) {
Expand Down Expand Up @@ -1411,7 +1411,7 @@ class ConnectionManager extends EventEmitter {
this.state.state,
newState.state,
null,
request.error || (ConnectionErrors as Record<string, ErrorInfo>)[newState.state]
request.error || (ConnectionErrors as Partial<Record<string, () => ErrorInfo>>)[newState.state]?.()
);

this.enactStateChange(change);
Expand Down
6 changes: 3 additions & 3 deletions src/common/lib/transport/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ abstract class Transport extends EventEmitter {
if (this.isConnected) {
this.requestClose();
}
this.finish('closed', ConnectionErrors.closed);
this.finish('closed', ConnectionErrors.closed());
}

disconnect(err?: Error | ErrorInfo): void {
Expand All @@ -87,15 +87,15 @@ abstract class Transport extends EventEmitter {
if (this.isConnected) {
this.requestDisconnect();
}
this.finish('disconnected', err || ConnectionErrors.disconnected);
this.finish('disconnected', err || ConnectionErrors.disconnected());
}

fail(err: ErrorInfo): void {
/* Used for client-side-detected fatal connection issues */
if (this.isConnected) {
this.requestDisconnect();
}
this.finish('failed', err || ConnectionErrors.failed);
this.finish('failed', err || ConnectionErrors.failed());
}

finish(event: string, err?: Error | ErrorInfo): void {
Expand Down

0 comments on commit 94eafe0

Please sign in to comment.