summaryrefslogtreecommitdiffhomepage
path: root/gui
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2019-03-01 16:21:11 +0100
committerAndrej Mihajlov <and@mullvad.net>2019-03-01 16:45:51 +0100
commit631f7f0981961a046401d8cf217b19ca4ea2f2b0 (patch)
tree4cf192b84879aa71d83e6a993efe08d618c40f5b /gui
parentac728cbef306c08698c66c3e3f1515e8cb636ea3 (diff)
downloadmullvadvpn-631f7f0981961a046401d8cf217b19ca4ea2f2b0.tar.xz
mullvadvpn-631f7f0981961a046401d8cf217b19ca4ea2f2b0.zip
Convert AuthFailure to Error subclass and dynamically query gettext strings
Diffstat (limited to 'gui')
-rw-r--r--gui/src/renderer/components/NotificationArea.tsx4
-rw-r--r--gui/src/renderer/lib/auth-failure.ts109
-rw-r--r--gui/test/auth-failure.spec.ts22
3 files changed, 74 insertions, 61 deletions
diff --git a/gui/src/renderer/components/NotificationArea.tsx b/gui/src/renderer/components/NotificationArea.tsx
index bf2969480b..543ba90edf 100644
--- a/gui/src/renderer/components/NotificationArea.tsx
+++ b/gui/src/renderer/components/NotificationArea.tsx
@@ -16,7 +16,7 @@ import {
import { BlockReason, TunnelStateTransition } from '../../shared/daemon-rpc-types';
import AccountExpiry from '../lib/account-expiry';
-import { AuthFailure } from '../lib/auth-failure';
+import { AuthFailureError } from '../lib/auth-failure';
import { IVersionReduxState } from '../redux/version/reducers';
interface IProps {
@@ -43,7 +43,7 @@ type State = NotificationAreaPresentation & {
function getBlockReasonMessage(blockReason: BlockReason): string {
switch (blockReason.reason) {
case 'auth_failed': {
- return new AuthFailure(blockReason.details).show();
+ return new AuthFailureError(blockReason.details).message;
}
case 'ipv6_unavailable':
return pgettext(
diff --git a/gui/src/renderer/lib/auth-failure.ts b/gui/src/renderer/lib/auth-failure.ts
index f200ed824d..7ac9b4b527 100644
--- a/gui/src/renderer/lib/auth-failure.ts
+++ b/gui/src/renderer/lib/auth-failure.ts
@@ -1,79 +1,92 @@
import log from 'electron-log';
import { pgettext } from '../../shared/gettext';
-export type AuthFailureKind =
- | 'INVALID_ACCOUNT'
- | 'EXPIRED_ACCOUNT'
- | 'TOO_MANY_CONNECTIONS'
- | 'UNKNOWN';
+export enum AuthFailureKind {
+ invalidAccount,
+ expiredAccount,
+ tooManyConnections,
+ unknown,
+}
-// These strings should match up with mullvad-types/src/auth_failed.rs
+export class AuthFailureError extends Error {
+ private kindValue: AuthFailureKind;
+ private unknownErrorMessage?: string;
-const GENERIC_FAILURE_MSG = pgettext('auth-failure', 'Account authentication failed.');
+ get kind(): AuthFailureKind {
+ return this.kindValue;
+ }
-const INVALID_ACCOUNT_MSG = pgettext(
- 'auth-failure',
- "You've logged in with an account number that is not valid. Please log out and try another one.",
-);
+ get message(): string {
+ switch (this.kindValue) {
+ case AuthFailureKind.invalidAccount:
+ return pgettext(
+ 'auth-failure',
+ "You've logged in with an account number that is not valid. Please log out and try another one.",
+ );
-const EXPIRED_ACCOUNT_MSG = pgettext(
- 'auth-failure',
- 'You have no more VPN time left on this account. Please log in on our website to buy more credit.',
-);
+ case AuthFailureKind.expiredAccount:
+ return pgettext(
+ 'auth-failure',
+ 'You have no more VPN time left on this account. Please log in on our website to buy more credit.',
+ );
-const TOO_MANY_CONNECTIONS_MSG = pgettext(
- 'auth-failure',
- 'This account has too many simultaneous connections. Disconnect another device or try connecting again shortly.',
-);
+ case AuthFailureKind.tooManyConnections:
+ return pgettext(
+ 'auth-failure',
+ 'This account has too many simultaneous connections. Disconnect another device or try connecting again shortly.',
+ );
-export class AuthFailure {
- private reasonId: AuthFailureKind;
- private message: string;
+ case AuthFailureKind.unknown:
+ return (
+ this.unknownErrorMessage || pgettext('auth-failure', 'Account authentication failed.')
+ );
+ }
+ }
constructor(reason?: string) {
+ super();
+
if (!reason) {
log.error('Received invalid auth_failed reason: ', reason);
- this.reasonId = 'UNKNOWN';
- this.message = GENERIC_FAILURE_MSG;
+
+ this.kindValue = AuthFailureKind.unknown;
return;
}
const results = /^\[(\w+)\]\s*(.*)$/.exec(reason);
- if (!results || results.length < 3) {
- log.error(`Received invalid auth_failed message - "${reason}"`);
- this.reasonId = 'UNKNOWN';
- this.message = reason;
- return;
- }
+ if (results && results.length === 3) {
+ const rawReasonId = results[1];
+ const kindValue = rawReasonIdToFailureKind(rawReasonId);
- const idString = results[1];
- this.reasonId = strToFailureKind(idString);
- this.message = results[2] || GENERIC_FAILURE_MSG;
- }
+ if (kindValue === AuthFailureKind.unknown) {
+ log.error(`Received unknown auth_failed message id - ${rawReasonId}`);
+ }
+
+ this.kindValue = kindValue;
+ this.unknownErrorMessage = results[2];
+ } else {
+ log.error(`Received invalid auth_failed message - "${reason}"`);
- public show(): string {
- switch (this.reasonId) {
- case 'INVALID_ACCOUNT':
- return INVALID_ACCOUNT_MSG;
- case 'EXPIRED_ACCOUNT':
- return EXPIRED_ACCOUNT_MSG;
- case 'TOO_MANY_CONNECTIONS':
- return TOO_MANY_CONNECTIONS_MSG;
- case 'UNKNOWN':
- return this.message;
+ this.kindValue = AuthFailureKind.unknown;
+ this.unknownErrorMessage = reason;
}
}
}
-export function strToFailureKind(id: string): AuthFailureKind {
+function rawReasonIdToFailureKind(id: string): AuthFailureKind {
+ // These strings should match up with mullvad-types/src/auth_failed.rs
switch (id) {
case 'INVALID_ACCOUNT':
+ return AuthFailureKind.invalidAccount;
+
case 'EXPIRED_ACCOUNT':
+ return AuthFailureKind.expiredAccount;
+
case 'TOO_MANY_CONNECTIONS':
- return id;
+ return AuthFailureKind.tooManyConnections;
+
default:
- log.error(`Received unknown auth_failed message id - ${id}`);
- return 'UNKNOWN';
+ return AuthFailureKind.unknown;
}
}
diff --git a/gui/test/auth-failure.spec.ts b/gui/test/auth-failure.spec.ts
index 4d1ad75b25..6bb643c345 100644
--- a/gui/test/auth-failure.spec.ts
+++ b/gui/test/auth-failure.spec.ts
@@ -1,27 +1,27 @@
import { expect } from 'chai';
import { it, describe } from 'mocha';
-import { AuthFailure } from '../src/renderer/lib/auth-failure';
+import { AuthFailureError, AuthFailureKind } from '../src/renderer/lib/auth-failure';
describe('auth_failed parsing', () => {
it('invalid line parsing works', () => {
- const auth_msg = new AuthFailure('invalid auth_failed message');
- expect(auth_msg._reasonId).to.be.eql('UNKNOWN');
- expect(auth_msg.show()).to.be.eql('invalid auth_failed message');
+ const auth_msg = new AuthFailureError('invalid auth_failed message');
+ expect(auth_msg.kind).to.be.equal(AuthFailureKind.unknown);
+ expect(auth_msg.message).to.be.equal('invalid auth_failed message');
});
it('valid unknown works', () => {
- const auth_msg = new AuthFailure('[valid_unknown] Message');
- expect(auth_msg._reasonId).to.be.eql('UNKNOWN');
- expect(auth_msg.show()).to.be.eql('Message');
+ const auth_msg = new AuthFailureError('[valid_unknown] Message');
+ expect(auth_msg.kind).to.be.equal(AuthFailureKind.unknown);
+ expect(auth_msg.message).to.be.equal('Message');
});
it('valid known works', () => {
- const auth_msg = new AuthFailure('[INVALID_ACCOUNT] Invalid account');
- expect(auth_msg._reasonId).to.be.eql('INVALID_ACCOUNT');
+ const auth_msg = new AuthFailureError('[INVALID_ACCOUNT] Invalid account');
+ expect(auth_msg.kind).to.be.equal(AuthFailureKind.invalidAccount);
});
it('empty message works', () => {
- const auth_msg = new AuthFailure('[INVALID_ACCOUNT]');
- expect(auth_msg._reasonId).to.be.eql('INVALID_ACCOUNT');
+ const auth_msg = new AuthFailureError('[INVALID_ACCOUNT]');
+ expect(auth_msg.kind).to.be.equal(AuthFailureKind.invalidAccount);
});
});