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

feat(SPV-915) adjust to new web-backend detailed errors #82

Merged
merged 9 commits into from
Aug 27, 2024
11 changes: 2 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ErrorBar } from '@/components/ErrorBar';
import { ToastContainer } from 'react-toastify';

import 'react-toastify/dist/ReactToastify.css';
import { errorMessage } from './utils/errorMessage';

const ROUTES = [
{
Expand Down Expand Up @@ -87,22 +88,14 @@ export const App = () => {
}
})
.catch((error) => {
let errorMsg;
if (error.response.status === 401 || error.response.status === 400) {
setAuthorization(null);
navigate('/');
setLoading(false);
return;
}

if (error.response.status === 404) {
errorMsg =
error.response.data + ". If you can't log in again, please contact our support or try again later!";
} else {
errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please, try again later!';
}

setError(errorMsg);
setError(errorMessage(error.response.data));
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
12 changes: 2 additions & 10 deletions src/components/AccountSummary/AccountSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getUser } from '@/api';
import { Loader } from '@/components/Loader';
import { ErrorBar } from '@/components/ErrorBar';
import { convertSatToBsv } from '@/utils/helpers/convertSatToBsv';
import { errorMessage } from '@/utils/errorMessage';

interface CurrencyRates {
usd?: number;
Expand Down Expand Up @@ -40,16 +41,7 @@ export const AccountSummary = () => {
setDetails(accountDetails);
})
.catch((error) => {
let errorMsg;

if (error.response.status === 404) {
errorMsg =
"User's account details not found. If you can't log in again, please contact our support or try again later!";
} else {
errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please, try again later!';
}

setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
})
.finally(() => {
setLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export const ContactUpsertModal: FC<ContactUpsertModal> = ({
await upsertContact(paymail, name, phone ? { phoneNumber: phone } : undefined);
onSuccess();
} catch (error) {
console.error('error', error);
setError(errorMsg);
} finally {
setLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAutoupdate } from '@/providers/autoupdate';
import { convertSatToBsv } from '@/utils/helpers/convertSatToBsv';
import { PasswordInput } from '@/components/Input/PasswordInput';
import { modalCloseTimeout } from '../../modalCloseTimeout';
import { errorMessage } from '@/utils/errorMessage';

export interface TransactionData {
paymail: string;
Expand Down Expand Up @@ -82,17 +83,11 @@ export const TransactionConfirmModal: FC<TransactionConfirmModalProps> = ({
return;
}

if (error.response.status === 400) {
setErrors(
'Transfer was not sent. Probably you filled the form with incorrect data. Please try once again!',
);
return;
}

setErrors(
error.response.data
? error.response.data
: 'Transfer was not sent. Please verify transfer data and try once again. If problem will happen again, contact with our support.',
errorMessage(
error.response.data,
'Transfer was not sent. Please verify transfer data and try once again. If problem will happen again, contact with our support.',
),
chris-4chain marked this conversation as resolved.
Show resolved Hide resolved
);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Loader } from '@/components/Loader';
import { ErrorBar } from '@/components/ErrorBar';
import { convertSatToBsv } from '@/utils/helpers/convertSatToBsv';
import { SetPaymailButton } from '@/components/TransferForm/SetPaymailButton';
import { errorMessage } from '@/utils/errorMessage';

type TransactionDetailsProps = {
open: boolean;
Expand All @@ -33,8 +34,7 @@ export const TransactionDetailsModal: FC<TransactionDetailsProps> = ({ open, onC
setTransactionData(response);
})
.catch((error) => {
const errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please try again later';
errorMsg && setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
})
.finally(() => {
setLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useAutoupdate } from '@/providers/autoupdate';
import _ from 'lodash';
import { convertSatToBsv } from '@/utils/helpers/convertSatToBsv';
import { PaginationParams } from '@/api/types/pagination';
import { errorMessage } from '@/utils/errorMessage';

const KEY_NAME_ENTER = 'Enter';
const KEY_NAME_SPACE = 'Space';
Expand Down Expand Up @@ -91,8 +92,7 @@ export const TransactionTable = () => {
setTransactionsList(transactions.transactions);
})
.catch((error) => {
const errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please try again later';
setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
})
.finally(() => {
setLoading(false);
Expand Down
4 changes: 2 additions & 2 deletions src/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNavigate } from 'react-router-dom';
import { useAuthorization } from '@/providers';
import { logoutUser } from '@/api/requests';
import { BsvLogo } from '@/components/BsvLogo';
import { errorMessage } from '@/utils/errorMessage';

interface MenuProps {
userEmail?: string;
Expand Down Expand Up @@ -54,8 +55,7 @@ export const UserMenu: FC<MenuProps> = ({ userEmail }) => {
Navigate('/');
})
.catch((error) => {
const errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please try again!';
errorMsg && setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
})
.finally(() => {
setLoading(false);
Expand Down
14 changes: 14 additions & 0 deletions src/utils/errorMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type SPVError = {
message: string;
code: string;
};

const isSPVError = (error: unknown): error is SPVError => {
return (error as SPVError).code !== undefined && (error as SPVError).message !== undefined;
};

const DEFAULT_ERROR_MESSAGE = 'Something went wrong... Please, try again later!';

export const errorMessage = (error: SPVError | unknown, fallbackMessage = DEFAULT_ERROR_MESSAGE) => {
return isSPVError(error) ? error.message : fallbackMessage;
};
4 changes: 2 additions & 2 deletions src/views/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useAuthorization } from '@/providers';
import { useNavigate } from 'react-router-dom';
import { LoggedInUser, loginUser } from '@/api';
import { PasswordInput } from '@/components/Input/PasswordInput';
import { errorMessage } from '@/utils/errorMessage';

export const LoginPage = () => {
const [email, setEmail] = useState<string>('');
Expand Down Expand Up @@ -50,8 +51,7 @@ export const LoginPage = () => {
setLoading(false);
})
.catch((error) => {
const errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please, try again later!';
setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
setLoading(false);
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/views/SignupPage/SignupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AfterRegistrationSteps } from '@/components/StepsList/_lists/AfterRegis
import { RegisterNewUserDto } from '@/api/types/user';
import { registerUser } from '@/api';
import { PasswordInput } from '@/components/Input/PasswordInput';
import { errorMessage } from '@/utils/errorMessage';

export const SignupPage = () => {
const [email, setEmail] = useState<string>('');
Expand Down Expand Up @@ -64,8 +65,7 @@ export const SignupPage = () => {
setLoading(false);
})
.catch((error) => {
const errorMsg = error.response.data ? error.response.data : 'Something went wrong... Please, try again later!';
setErrors(errorMsg);
setErrors(errorMessage(error.response.data));
setRegistered(false);
setLoading(false);
});
Expand Down
Loading