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

refactor: avoid early initialisation of connection errors #1206

Merged
merged 1 commit into from
Apr 26, 2023
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
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