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

Convert ConnectionError to TypeScript #807

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
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"PresenceMessage": true,
"ProtocolMessage": true,
"Stats": true,
"ConnectionError": true,
"ConnectionErrors": true,
"MessageQueue": true,
"Protocol": true,
"ConnectionManager": true,
Expand Down
4 changes: 2 additions & 2 deletions common/lib/client/realtimechannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Message from '../types/message';
import ChannelStateChange from './channelstatechange';
import ErrorInfo from '../types/errorinfo';
import PresenceMessage from '../types/presencemessage';
import ConnectionError from '../transport/connectionerror';
import ConnectionErrors from '../transport/connectionerrors';

var RealtimeChannel = (function() {
var actions = ProtocolMessage.Action;
Expand Down Expand Up @@ -552,7 +552,7 @@ var RealtimeChannel = (function() {

default:
Logger.logAction(Logger.LOG_ERROR, 'RealtimeChannel.onMessage()', 'Fatal protocol error: unrecognised action (' + message.action + ')');
this.connectionManager.abort(ConnectionError.unknownChannelErr);
this.connectionManager.abort(ConnectionErrors.unknownChannelErr);
}
};

Expand Down
4 changes: 2 additions & 2 deletions common/lib/client/realtimepresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Logger from '../util/logger';
import PresenceMessage from '../types/presencemessage';
import ErrorInfo from '../types/errorinfo';
import RealtimeChannel from './realtimechannel';
import ConnectionError from '../transport/connectionerror';
import ConnectionErrors from '../transport/connectionerrors';
import Multicaster from '../util/multicaster';
import ChannelStateChange from './channelstatechange';

Expand Down Expand Up @@ -193,7 +193,7 @@ var RealtimePresence = (function() {
default:
/* there is no connection; therefore we let
* any entered status timeout by itself */
callback(ConnectionError.failed);
callback(ConnectionErrors.failed);
}
};

Expand Down
4 changes: 2 additions & 2 deletions common/lib/transport/comettransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ProtocolMessage from '../types/protocolmessage';
import Transport from '../transport/transport';
import Logger from '../util/logger';
import Defaults from '../util/defaults';
import ConnectionError from './connectionerror';
import ConnectionErrors from './connectionerrors';
import Auth from '../client/auth';
import ErrorInfo from '../types/errorinfo';

Expand Down Expand Up @@ -170,7 +170,7 @@ var CometTransport = (function() {
}
/* In almost all cases the transport will be finished before it's
* disposed. Finish here just to make sure. */
this.finish('disconnected', ConnectionError.disconnected);
this.finish('disconnected', ConnectionErrors.disconnected);
var self = this;
Utils.nextTick(function() {
self.emit('disposed');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ErrorInfo from '../types/errorinfo';
import * as Utils from '../util/utils';

var ConnectionError = {
const ConnectionErrors = {
disconnected: ErrorInfo.fromValues({
statusCode: 400,
code: 80003,
Expand Down Expand Up @@ -39,17 +38,17 @@ var ConnectionError = {
})
};

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

export default ConnectionError;
export default { ...ConnectionErrors, isRetriable };
10 changes: 5 additions & 5 deletions common/lib/transport/connectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import EventEmitter from '../util/eventemitter';
import MessageQueue from './messagequeue';
import Logger from '../util/logger';
import ConnectionStateChange from '../client/connectionstatechange';
import ConnectionError from '../transport/connectionerror';
import ConnectionErrors from '../transport/connectionerrors';
import ErrorInfo from '../types/errorinfo';
import Auth from '../client/auth';
import Http from 'platform-http';
Expand Down Expand Up @@ -329,7 +329,7 @@ var ConnectionManager = (function() {
self.notifyState({state: 'failed', error: wrappedErr.error});
callback(true);
} else if(wrappedErr.event === 'disconnected') {
if(!ConnectionError.isRetriable(wrappedErr.error)) {
if(!ConnectionErrors.isRetriable(wrappedErr.error)) {
/* Error received from the server that does not call for trying a fallback host, eg a rate limit */
self.notifyState({state: self.states.connecting.failState, error: wrappedErr.error});
callback(true);
Expand Down Expand Up @@ -924,7 +924,7 @@ var ConnectionManager = (function() {
};

ConnectionManager.prototype.getStateError = function() {
return ConnectionError[this.state.state];
return ConnectionErrors[this.state.state];
};

ConnectionManager.prototype.activeState = function() {
Expand Down Expand Up @@ -1061,7 +1061,7 @@ var ConnectionManager = (function() {

/* process new state */
var newState = this.states[indicated.state],
change = new ConnectionStateChange(this.state.state, newState.state, newState.retryDelay, (indicated.error || ConnectionError[newState.state]));
change = new ConnectionStateChange(this.state.state, newState.state, newState.retryDelay, (indicated.error || ConnectionErrors[newState.state]));

if(retryImmediately) {
var autoReconnect = function() {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ var ConnectionManager = (function() {
if(state == 'closing' && this.state.state == 'closed') return;

var newState = this.states[state],
change = new ConnectionStateChange(this.state.state, newState.state, null, (request.error || ConnectionError[newState.state]));
change = new ConnectionStateChange(this.state.state, newState.state, null, (request.error || ConnectionErrors[newState.state]));

this.enactStateChange(change);

Expand Down
8 changes: 4 additions & 4 deletions common/lib/transport/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ProtocolMessage from '../types/protocolmessage';
import * as Utils from '../util/utils';
import EventEmitter from '../util/eventemitter';
import Logger from '../util/logger';
import ConnectionError from '../transport/connectionerror';
import ConnectionErrors from '../transport/connectionerrors';
import ErrorInfo from '../types/errorinfo';

var Transport = (function() {
Expand Down Expand Up @@ -47,7 +47,7 @@ var Transport = (function() {
if(this.isConnected) {
this.requestClose();
}
this.finish('closed', ConnectionError.closed);
this.finish('closed', ConnectionErrors.closed);
};

Transport.prototype.disconnect = function(err) {
Expand All @@ -56,15 +56,15 @@ var Transport = (function() {
if(this.isConnected) {
this.requestDisconnect();
}
this.finish('disconnected', err || ConnectionError.disconnected);
this.finish('disconnected', err || ConnectionErrors.disconnected);
};

Transport.prototype.fail = function(err) {
/* Used for client-side-detected fatal connection issues */
if(this.isConnected) {
this.requestDisconnect();
}
this.finish('failed', err || ConnectionError.failed);
this.finish('failed', err || ConnectionErrors.failed);
};

Transport.prototype.finish = function(event, err) {
Expand Down
2 changes: 1 addition & 1 deletion nodejs/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ includeScript('../common/lib/types/protocolmessage.js');
includeScript('../common/lib/types/stats.js');
includeScript('../common/lib/types/devicedetails.js');
includeScript('../common/lib/types/pushchannelsubscription.js');
includeScript('../common/lib/transport/connectionerror.js');
includeScript('../common/lib/transport/connectionerrors.js');
includeScript('../common/lib/transport/messagequeue.js');
includeScript('../common/lib/transport/protocol.js');
includeScript('../common/lib/transport/connectionmanager.js');
Expand Down