accounts-frontend/packages/app/services/errorsDict/errorsDict.tsx

158 lines
6.8 KiB
TypeScript
Raw Normal View History

import React, { ComponentType, ReactElement, ReactNode } from 'react';
import { Link } from 'react-router-dom';
2019-11-11 14:10:05 +05:30
import { FormattedMessage as Message } from 'react-intl';
import { RelativeTime } from 'app/components/ui';
const SuggestResetPassword: ComponentType = () => (
2020-05-24 04:38:24 +05:30
<>
<br />
<Message
key="suggestResetPassword"
defaultMessage="Have you {forgotYourPassword}?"
2020-05-24 04:38:24 +05:30
values={{
forgotYourPassword: (
<Link to="/forgot-password">
<Message key="forgotYourPassword" defaultMessage="forgot your password" />
2020-05-24 04:38:24 +05:30
</Link>
),
}}
/>
</>
);
const ResendKey: ComponentType<{ url: string }> = ({ url }) => (
<>
2020-05-24 04:38:24 +05:30
{' '}
<Link to={url}>
<Message key="doYouWantRequestKey" defaultMessage="Do you want to request a new key?" />
2020-05-24 04:38:24 +05:30
</Link>
</>
2020-05-24 04:38:24 +05:30
);
const errorsMap: Record<string, (props?: Record<string, any>) => ReactElement> = {
'error.login_required': () => <Message key="loginRequired" defaultMessage="Please enter Email or username" />,
2020-06-04 22:24:33 +05:30
'error.login_not_exist': () => (
<Message key="loginNotExist" defaultMessage="Sorry, Ely doesn't recognise your login." />
),
'error.password_required': () => <Message key="passwordRequired" defaultMessage="Please enter password" />,
2020-05-24 04:38:24 +05:30
'error.password_incorrect': (props) => (
// props are handled in validationErrorsHandler in components/auth/actions
<>
<Message key="invalidPassword" defaultMessage="You have entered wrong account password." />
2020-05-24 04:38:24 +05:30
{props && props.isGuest ? <SuggestResetPassword /> : null}
</>
),
'error.username_required': () => <Message key="usernameRequired" defaultMessage="Username is required" />,
'error.username_invalid': () => <Message key="usernameInvalid" defaultMessage="Username is invalid" />,
'error.username_too_short': () => <Message key="usernameTooShort" defaultMessage="Username is too short" />,
'error.username_too_long': () => <Message key="usernameTooLong" defaultMessage="Username is too long" />,
2020-06-04 22:24:33 +05:30
'error.username_not_available': () => (
<Message key="usernameUnavailable" defaultMessage="This username is already taken" />
),
2020-05-24 04:38:24 +05:30
'error.email_required': () => <Message key="emailRequired" defaultMessage="Email is required" />,
'error.email_too_long': () => <Message key="emailToLong" defaultMessage="Email is too long" />,
'error.email_invalid': () => <Message key="emailInvalid" defaultMessage="Email is invalid" />,
2020-06-04 22:24:33 +05:30
'error.email_is_tempmail': () => (
<Message key="emailIsTempmail" defaultMessage="Tempmail Email addresses is not allowed" />
),
2020-05-24 04:38:24 +05:30
'error.email_not_available': (props) => (
// props are handled in validationErrorsHandler in components/auth/actions
<>
<Message key="emailNotAvailable" defaultMessage="This Email is already registered." />
2020-05-24 04:38:24 +05:30
{props && props.isGuest ? <SuggestResetPassword /> : null}
</>
),
'error.totp_required': () => <Message key="totpRequired" defaultMessage="Please, enter the code" />,
'error.totp_incorrect': () => <Message key="totpIncorrect" defaultMessage="The code is incorrect" />,
2020-06-04 22:24:33 +05:30
'error.otp_already_enabled': () => (
<Message key="mfaAlreadyEnabled" defaultMessage="The two factor auth is already enabled" />
),
2020-05-24 04:38:24 +05:30
2020-06-04 22:24:33 +05:30
'error.rePassword_required': () => (
<Message key="rePasswordRequired" defaultMessage="Please retype your password" />
),
'error.password_too_short': () => (
<Message key="passwordTooShort" defaultMessage="Your password should be at least 8 characters length" />
),
'error.rePassword_does_not_match': () => (
<Message key="passwordsDoesNotMatch" defaultMessage="The passwords does not match" />
),
'error.rulesAgreement_required': () => (
<Message key="rulesAgreementRequired" defaultMessage="You must accept rules in order to create an account" />
),
'error.key_required': () => <Message key="keyRequired" defaultMessage="Please, enter an activation key" />,
2020-05-24 04:38:24 +05:30
'error.key_not_exists': (props) => (
<>
<Message key="keyNotExists" defaultMessage="The key is incorrect or has expired." />
2020-05-24 04:38:24 +05:30
{props && props.repeatUrl ? <ResendKey url={props.repeatUrl} /> : null}
</>
),
'error.key_expire': (props) => errorsMap['error.key_not_exists'](props),
2020-06-04 22:24:33 +05:30
'error.newPassword_required': () => (
<Message key="newPasswordRequired" defaultMessage="Please enter new password" />
),
'error.newRePassword_required': () => (
<Message key="newRePasswordRequired" defaultMessage="Please repeat new password" />
),
2020-05-24 04:38:24 +05:30
2020-06-04 22:24:33 +05:30
'error.account_not_activated': () => (
<Message key="accountNotActivated" defaultMessage="The account is not activated" />
),
'error.account_banned': () => <Message key="accountBanned" defaultMessage="Account is blocked" />,
2020-05-24 04:38:24 +05:30
'error.recently_sent_message': (props) => (
<Message
key="emailFrequency"
defaultMessage="Please cool down, you are requesting Emails too often. New key can be retrieved {time}."
2020-05-24 04:38:24 +05:30
values={{
// for msLeft @see AuthError.jsx
time: <RelativeTime timestamp={props!.msLeft} />,
}}
/>
),
'error.email_not_found': () => <Message key="emailNotFound" defaultMessage="Specified Email is not found" />,
2020-06-04 22:24:33 +05:30
'error.account_already_activated': () => (
<Message key="accountAlreadyActivated" defaultMessage="This account is already activated" />
),
2020-05-24 04:38:24 +05:30
'error.captcha_required': () => <Message key="captchaRequired" defaultMessage="Please, solve the captcha" />,
2020-05-24 04:38:24 +05:30
'error.captcha_invalid': (props) => errorsMap['error.captcha_required'](props),
'error.redirectUri_required': () => <Message key="redirectUriRequired" defaultMessage="Redirect URI is required" />,
'error.redirectUri_invalid': () => <Message key="redirectUriInvalid" defaultMessage="Redirect URI is invalid" />,
invalid_user_code: () => <Message key="invalidUserCode" defaultMessage="Invalid Device Code" />,
expired_token: () => (
<Message
key="expiredUserCode"
defaultMessage="The code has expired. Start the authorization flow in the application again."
/>
),
used_user_code: () => <Message key="usedUserCode" defaultMessage="This code has been already used" />,
};
interface ErrorLiteral {
2020-05-24 04:38:24 +05:30
type: string;
payload?: Record<string, any>;
}
type Error = string | ErrorLiteral;
export function resolve(error: Error): ReactNode {
2020-05-24 04:38:24 +05:30
let payload = {};
2020-05-24 04:38:24 +05:30
if (typeof error !== 'string') {
payload = error.payload || {};
error = error.type;
}
2020-05-24 04:38:24 +05:30
return errorsMap[error] ? errorsMap[error](payload) : error;
}