diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-08-02 22:01:40 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-08-02 22:01:40 -0300 |
| commit | e8b4942f5e1c0d3962975820c062d100b98745a1 (patch) | |
| tree | 6a6d66685b2d450eb40c1bd33c57329d0561c04b | |
| parent | 0abcde1cac8db5bd86080ee92a39521d3c2bd13c (diff) | |
| parent | 58e73802b39bdbad3b38fc1389105b978a6316c9 (diff) | |
| download | mullvadvpn-e8b4942f5e1c0d3962975820c062d100b98745a1.tar.xz mullvadvpn-e8b4942f5e1c0d3962975820c062d100b98745a1.zip | |
Merge branch 'refactor-error-instantiation'
| -rw-r--r-- | app/app.js | 35 | ||||
| -rw-r--r-- | app/components/Connect.js | 4 | ||||
| -rw-r--r-- | app/errors.js | 10 | ||||
| -rw-r--r-- | app/lib/daemon-rpc.js | 26 |
4 files changed, 28 insertions, 47 deletions
diff --git a/app/app.js b/app/app.js index c10df05deb..8839e3d33e 100644 --- a/app/app.js +++ b/app/app.js @@ -12,18 +12,8 @@ import { log } from './lib/platform'; import ReconnectionBackoff from './lib/reconnection-backoff'; import { DaemonRpc } from './lib/daemon-rpc'; import { setShutdownHandler } from './shutdown-handler'; -import { - RemoteError as JsonRpcTransportRemoteError, - TimeOutError as JsonRpcTransportTimeOutError, -} from './lib/jsonrpc-transport'; -import { - UnknownError, - NoAccountError, - CommunicationError, - InvalidAccountError, - NoDaemonError, -} from './errors'; +import { NoAccountError } from './errors'; import configureStore from './redux/store'; import accountActions from './redux/account/actions'; @@ -144,7 +134,7 @@ export default class AppRenderer { } catch (error) { log.error('Failed to log in,', error.message); - actions.account.loginFailed(this._rpcErrorToBackendError(error)); + actions.account.loginFailed(error); } } @@ -536,27 +526,6 @@ export default class AppRenderer { this._updateTrayIcon(connectionState); } - _rpcErrorToBackendError(e) { - if (e instanceof JsonRpcTransportRemoteError) { - switch (e.code) { - case -200: // Account doesn't exist - return new InvalidAccountError(); - case -32603: // Internal error - // We treat all internal backend errors as the user cannot reach - // api.mullvad.net. This is not always true of course, but it is - // true so often that we choose to disregard the other edge cases - // for now. - return new CommunicationError(); - } - } else if (e instanceof JsonRpcTransportTimeOutError) { - return new CommunicationError(); - } else if (e instanceof NoDaemonError) { - return e; - } - - return new UnknownError(e.message); - } - async _authenticate(sharedSecret: string) { try { await this._daemonRpc.authenticate(sharedSecret); diff --git a/app/components/Connect.js b/app/components/Connect.js index dabde569dc..d472c45b31 100644 --- a/app/components/Connect.js +++ b/app/components/Connect.js @@ -63,7 +63,7 @@ export default class Connect extends Component<ConnectProps, ConnectState> { } render() { - const error = this.displayError(); + const error = this.checkForErrors(); const child = error ? this.renderError(error) : this.renderMap(); return ( @@ -354,7 +354,7 @@ export default class Connect extends Component<ConnectProps, ConnectState> { return classes; } - displayError(): ?Error { + checkForErrors(): ?Error { // Offline? if (!this.props.connection.isOnline) { return new NoInternetError(); diff --git a/app/errors.js b/app/errors.js index 929fa52ab9..993b4d0ff1 100644 --- a/app/errors.js +++ b/app/errors.js @@ -49,13 +49,3 @@ export class CommunicationError extends Error { super('api.mullvad.net is blocked, please check your firewall'); } } - -export class UnknownError extends Error { - constructor(cause: string) { - super(`An unknown error occurred, ${cause}`); - } - - get userFriendlyTitle(): string { - return 'Something went wrong'; - } -} diff --git a/app/lib/daemon-rpc.js b/app/lib/daemon-rpc.js index 72421ed88c..fb3915e068 100644 --- a/app/lib/daemon-rpc.js +++ b/app/lib/daemon-rpc.js @@ -1,6 +1,11 @@ // @flow -import JsonRpcTransport from './jsonrpc-transport'; +import JsonRpcTransport, { + RemoteError as JsonRpcRemoteError, + TimeOutError as JsonRpcTimeOutError, +} from './jsonrpc-transport'; +import { CommunicationError, InvalidAccountError, NoDaemonError } from '../errors'; + import { object, maybe, @@ -265,7 +270,24 @@ export class DaemonRpc implements DaemonRpcProtocol { async getAccountData(accountToken: AccountToken): Promise<AccountData> { // send the IPC with 30s timeout since the backend will wait // for a HTTP request before replying - const response = await this._transport.send('get_account_data', accountToken, 30000); + let response; + try { + response = await this._transport.send('get_account_data', accountToken, 30000); + } catch (error) { + if (error instanceof JsonRpcRemoteError) { + switch (error.code) { + case -200: // Account doesn't exist + throw new InvalidAccountError(); + case -32603: // Internal error + throw new CommunicationError(); + } + } else if (error instanceof JsonRpcTimeOutError) { + throw new NoDaemonError(); + } else { + throw error; + } + } + try { return validate(AccountDataSchema, response); } catch (error) { |
