Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GooDeene committed Jun 3, 2024
1 parent fa3e6a0 commit 051ac0b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/pages/not-found-page/not-found-page.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
display: flex;
justify-content: center;
width: 100%;
background-image: url('../../../public/img/no-places.png');
background-image: url('public/img/no-places.png');
background-size: cover;
background-repeat: no-repeat;
}
14 changes: 10 additions & 4 deletions src/services/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, {AxiosError, AxiosInstance, InternalAxiosRequestConfig} from 'axios';
import {getToken} from './token.ts';
import {toast} from 'react-toastify';
import {DetailMessageType, shouldDisplayError} from './error-handle.ts';
import {DetailMessageType, SERVER_MINIMAL_ERROR_CODE, shouldDisplayError} from './error-handle.ts';

const BACKEND_URL = 'https://14.design.htmlacademy.pro/six-cities';
const REQUEST_TIMEOUT = 5000;
Expand Down Expand Up @@ -29,15 +29,21 @@ export const createAPI = (): AxiosInstance => {
(response) => response,
(error: AxiosError<DetailMessageType>) => {
if (error.response && shouldDisplayError(error.response)) {
if (error.response.data.details.length) {
const messages = (error.response.data.details);
messages.map((property) => {

if (error.response.status >= SERVER_MINIMAL_ERROR_CODE) {

toast.error('Ой-ой! Ошибка соединения с сервером. Сервер не доступен!');

} else if (error.response.data.details.length) {
error.response.data.details.map((property) => {
property.messages.map((message) => {

toast.warning(message);

});
});
} else if (error.response.data.message) {

const detailMessage = (error.response.data);
toast.warn(detailMessage.message);
}
Expand Down
8 changes: 5 additions & 3 deletions src/services/error-handle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {StatusCodes} from 'http-status-codes';
import {AxiosResponse} from 'axios';

export const SERVER_MINIMAL_ERROR_CODE = 500;

export type DetailMessageType = {
errorType: string;
message: string;
Expand All @@ -13,8 +15,8 @@ export type DetailMessageType = {

const StatusCodeMapping: Record<number, boolean> = {
[StatusCodes.BAD_REQUEST]: true,
[StatusCodes.UNAUTHORIZED]: false,
[StatusCodes.NOT_FOUND]: true
[StatusCodes.NOT_FOUND]: true,
};

export const shouldDisplayError = (response: AxiosResponse) => StatusCodeMapping[response.status];
export const shouldDisplayError = (response: AxiosResponse) =>
response.status >= SERVER_MINIMAL_ERROR_CODE ? true : StatusCodeMapping[response.status];

0 comments on commit 051ac0b

Please sign in to comment.