diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-03-06 11:15:27 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-03-07 13:48:30 +0100 |
| commit | 07c3a6f1c07d776686fbb8842b677424bdec8fca (patch) | |
| tree | 420bf01ed88317a3c6e3501307e4bc4f393dacf6 /gui/src/renderer/lib | |
| parent | 8c4673bbf71924ae321db08d8a9b413ff06dfb50 (diff) | |
| download | mullvadvpn-07c3a6f1c07d776686fbb8842b677424bdec8fca.tar.xz mullvadvpn-07c3a6f1c07d776686fbb8842b677424bdec8fca.zip | |
Refactor AuthFailureError into parseAuthFailure()
Diffstat (limited to 'gui/src/renderer/lib')
| -rw-r--r-- | gui/src/renderer/lib/auth-failure.ts | 110 |
1 files changed, 50 insertions, 60 deletions
diff --git a/gui/src/renderer/lib/auth-failure.ts b/gui/src/renderer/lib/auth-failure.ts index 7ac9b4b527..ae381d5677 100644 --- a/gui/src/renderer/lib/auth-failure.ts +++ b/gui/src/renderer/lib/auth-failure.ts @@ -1,4 +1,3 @@ -import log from 'electron-log'; import { pgettext } from '../../shared/gettext'; export enum AuthFailureKind { @@ -8,75 +7,41 @@ export enum AuthFailureKind { unknown, } -export class AuthFailureError extends Error { - private kindValue: AuthFailureKind; - private unknownErrorMessage?: string; - - get kind(): AuthFailureKind { - return this.kindValue; - } - - 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.", - ); - - 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.', - ); - - case AuthFailureKind.tooManyConnections: - return pgettext( - 'auth-failure', - 'This account has too many simultaneous connections. Disconnect another device or try connecting again shortly.', - ); - - 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.kindValue = AuthFailureKind.unknown; - return; - } +interface IAuthFailure { + kind: AuthFailureKind; + message: string; +} - const results = /^\[(\w+)\]\s*(.*)$/.exec(reason); +export function parseAuthFailure(rawFailureMessage?: string): IAuthFailure { + if (rawFailureMessage) { + const results = /^\[(\w+)\]\s*(.*)$/.exec(rawFailureMessage); if (results && results.length === 3) { - const rawReasonId = results[1]; - const kindValue = rawReasonIdToFailureKind(rawReasonId); - - if (kindValue === AuthFailureKind.unknown) { - log.error(`Received unknown auth_failed message id - ${rawReasonId}`); - } + const kind = parseRawFailureKind(results[1]); + const message = + kind === AuthFailureKind.unknown ? rawFailureMessage : messageForFailureKind(kind); - this.kindValue = kindValue; - this.unknownErrorMessage = results[2]; + return { + kind, + message, + }; } else { - log.error(`Received invalid auth_failed message - "${reason}"`); - - this.kindValue = AuthFailureKind.unknown; - this.unknownErrorMessage = reason; + return { + kind: AuthFailureKind.unknown, + message: rawFailureMessage, + }; } + } else { + return { + kind: AuthFailureKind.unknown, + message: messageForFailureKind(AuthFailureKind.unknown), + }; } } -function rawReasonIdToFailureKind(id: string): AuthFailureKind { +function parseRawFailureKind(failureId: string): AuthFailureKind { // These strings should match up with mullvad-types/src/auth_failed.rs - switch (id) { + switch (failureId) { case 'INVALID_ACCOUNT': return AuthFailureKind.invalidAccount; @@ -90,3 +55,28 @@ function rawReasonIdToFailureKind(id: string): AuthFailureKind { return AuthFailureKind.unknown; } } + +function messageForFailureKind(kind: AuthFailureKind): string { + switch (kind) { + 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.", + ); + + 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.', + ); + + case AuthFailureKind.tooManyConnections: + return pgettext( + 'auth-failure', + 'This account has too many simultaneous connections. Disconnect another device or try connecting again shortly.', + ); + + case AuthFailureKind.unknown: + return pgettext('auth-failure', 'Account authentication failed.'); + } +} |
