B: Fix oss filtering and error messages
Some checks failed
Backend CI / build (3.12) (push) Has been cancelled
Frontend CI / build (22.x) (push) Has been cancelled

This commit is contained in:
Ivan 2024-08-18 12:55:41 +03:00
parent dee49b789a
commit 00934d5716
3 changed files with 27 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import { AxiosError, AxiosRequestConfig } from 'axios';
import { toast } from 'react-toastify';
import { ErrorData } from '@/components/info/InfoError';
import { extractErrorMessage } from '@/utils/utils';
import { axiosInstance } from './apiConfiguration';
@ -50,7 +51,7 @@ export function AxiosGet<ResponseData>({ endpoint, request, options }: IAxiosReq
})
.catch((error: Error | AxiosError) => {
if (request.setLoading) request.setLoading(false);
if (request.showError) toast.error(error.message);
if (request.showError) toast.error(extractErrorMessage(error));
if (request.onError) request.onError(error);
});
}
@ -69,7 +70,7 @@ export function AxiosPost<RequestData, ResponseData>({
})
.catch((error: Error | AxiosError) => {
if (request.setLoading) request.setLoading(false);
if (request.showError) toast.error(error.message);
if (request.showError) toast.error(extractErrorMessage(error));
if (request.onError) request.onError(error);
});
}
@ -88,7 +89,7 @@ export function AxiosDelete<RequestData, ResponseData>({
})
.catch((error: Error | AxiosError) => {
if (request.setLoading) request.setLoading(false);
if (request.showError) toast.error(error.message);
if (request.showError) toast.error(extractErrorMessage(error));
if (request.onError) request.onError(error);
});
}
@ -108,7 +109,7 @@ export function AxiosPatch<RequestData, ResponseData>({
})
.catch((error: Error | AxiosError) => {
if (request.setLoading) request.setLoading(false);
if (request.showError) toast.error(error.message);
if (request.showError) toast.error(extractErrorMessage(error));
if (request.onError) request.onError(error);
});
}

View File

@ -31,12 +31,12 @@ export function sortItemsForOSS(oss: IOperationSchema, items: ILibraryItem[]): I
result.push(item);
}
}
for (const item of result) {
for (const item of items) {
if (item.visible && !result.includes(item)) {
result.push(item);
}
}
for (const item of result) {
for (const item of items) {
if (!result.includes(item)) {
result.push(item);
}

View File

@ -2,7 +2,7 @@
* Module: Utility functions.
*/
import { AxiosHeaderValue, AxiosResponse } from 'axios';
import axios, { AxiosError, AxiosHeaderValue, AxiosResponse } from 'axios';
import { prompts } from './labels';
@ -139,3 +139,22 @@ export function tripleToggleColor(value: boolean | undefined): string {
}
return value ? 'clr-text-green' : 'clr-text-red';
}
/**
* Extract error message from error object.
*/
export function extractErrorMessage(error: Error | AxiosError): string {
if (axios.isAxiosError(error)) {
if (error.response && error.response.status === 400) {
const data = error.response.data as Record<string, unknown>;
const keys = Object.keys(data);
if (keys.length === 1) {
const value = data[keys[0]];
if (typeof value === 'string') {
return `${keys[0]}: ${value}`;
}
}
}
}
return error.message;
}