update error handler
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { API_BASE_URL } from '@/app/actions/definitions';
|
import { API_BASE_URL } from '@/app/actions/definitions';
|
||||||
import { getSessionData } from '@/app/actions/session';
|
import { getSessionData } from '@/app/actions/session';
|
||||||
|
import { FormState } from '@/app/actions/auth';
|
||||||
|
import { unknown } from 'zod';
|
||||||
|
|
||||||
interface initMessageBody {
|
interface initMessageBody {
|
||||||
file_name: string,
|
file_name: string,
|
||||||
@@ -101,7 +103,12 @@ export async function cancelUpload(udloadId: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function chunkUpload(formData: FormData) {
|
export type ChunkResponse = {
|
||||||
|
errorMesage: string | null
|
||||||
|
fileId?: string
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function chunkUpload(formData: FormData): Promise<ChunkResponse> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/api/v1/files/chunk`, {
|
const response = await fetch(`${API_BASE_URL}/api/v1/files/chunk`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -111,17 +118,35 @@ export async function chunkUpload(formData: FormData) {
|
|||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
let parsed = await response.json();
|
let parsed = await response.json();
|
||||||
|
|
||||||
if (parsed.message_desc === 'Upload cancelled successfully') {
|
if (parsed.message_code === 0) {
|
||||||
return parsed.message_body;
|
console.log('code 0');
|
||||||
|
return {
|
||||||
|
errorMesage: null,
|
||||||
|
fileId: parsed.message_body.file_id
|
||||||
|
}
|
||||||
|
} else if (parsed.message_desc === 'Duplicate file upload') {
|
||||||
|
throw 'duplicate-file'
|
||||||
|
} else if (parsed.message_desc === 'Failed to upload chunk: Failed to upload chunk: User not have tokens for protect') {
|
||||||
|
throw 'user-not-have-tokens-for-protect'
|
||||||
} else {
|
} else {
|
||||||
throw parsed;
|
throw 'error'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw (`${response.status}`);
|
throw 'error'
|
||||||
|
}
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error) {
|
||||||
|
const typedError = error as any;
|
||||||
|
|
||||||
|
return {
|
||||||
|
errorMesage: typedError
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
errorMesage: 'error'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
return error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -234,15 +234,11 @@ export default function UploadSectionFile({ fileType, allowedExtensions, maxFile
|
|||||||
formData.append('upload_id', response.upload_id);
|
formData.append('upload_id', response.upload_id);
|
||||||
formData.append('chunk_number', chunkIndex.toString());
|
formData.append('chunk_number', chunkIndex.toString());
|
||||||
formData.append('chunk', chunk);
|
formData.append('chunk', chunk);
|
||||||
/* formData.append('findSimilar', '0'); */
|
|
||||||
|
|
||||||
const chunkResponse = await chunkUpload(formData);
|
const chunkResponse = await chunkUpload(formData);
|
||||||
|
|
||||||
if (chunkResponse.message_desc !== 'Chunk uploaded successfully') {
|
if (chunkResponse.errorMesage !== null) {
|
||||||
if (chunkResponse.message_desc === 'Duplicate file upload') {
|
throw chunkResponse.errorMesage;
|
||||||
throw (`duplicate-file`);
|
|
||||||
}
|
|
||||||
throw new Error(`Chunk ${chunkIndex} upload failed`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100));
|
setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100));
|
||||||
@@ -260,10 +256,16 @@ export default function UploadSectionFile({ fileType, allowedExtensions, maxFile
|
|||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!isCancelledRef.current) {
|
if (!isCancelledRef.current) {
|
||||||
if (error === 'duplicate-file') {
|
switch (error) {
|
||||||
|
case 'duplicate-file':
|
||||||
setErrorHandler(t('error-duplicate-file'));
|
setErrorHandler(t('error-duplicate-file'));
|
||||||
} else {
|
break;
|
||||||
|
case 'user-not-have-tokens-for-protect':
|
||||||
|
setErrorHandler(t('error-user-not-have-tokens-for-protect'));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
setErrorHandler(t('error-uploading-file'));
|
setErrorHandler(t('error-uploading-file'));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -196,15 +196,15 @@ export default function SectionSearchFile({ fileType, allowedExtensions, maxFile
|
|||||||
|
|
||||||
const chunkResponse = await chunkUpload(formData);
|
const chunkResponse = await chunkUpload(formData);
|
||||||
|
|
||||||
if (chunkResponse.message_desc !== 'Chunk uploaded successfully') {
|
if (chunkResponse.errorMesage !== null) {
|
||||||
throw new Error(`Chunk ${chunkIndex} upload failed`);
|
throw chunkResponse.errorMesage;
|
||||||
}
|
}
|
||||||
|
|
||||||
setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100));
|
setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100));
|
||||||
|
|
||||||
if (isLastChunk) {
|
if (isLastChunk && chunkResponse.fileId) {
|
||||||
setFileId(chunkResponse.message_body.file_id);
|
setFileId(chunkResponse.fileId);
|
||||||
document.cookie = `searchedFileId=${chunkResponse.message_body.file_id}`
|
document.cookie = `searchedFileId=${chunkResponse.fileId}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -205,7 +205,8 @@
|
|||||||
"FAILED_SAVE": "Failed save",
|
"FAILED_SAVE": "Failed save",
|
||||||
"view": "View",
|
"view": "View",
|
||||||
"are-no-violations": "There are no violations",
|
"are-no-violations": "There are no violations",
|
||||||
"error-reading-image": "Error reading image"
|
"error-reading-image": "Error reading image",
|
||||||
|
"error-user-not-have-tokens-for-protect": "User not have tokens for protect"
|
||||||
},
|
},
|
||||||
"Login-register-form": {
|
"Login-register-form": {
|
||||||
"and": "and",
|
"and": "and",
|
||||||
|
|||||||
@@ -205,7 +205,8 @@
|
|||||||
"FAILED_SAVE": "Сохранение не удалось",
|
"FAILED_SAVE": "Сохранение не удалось",
|
||||||
"view": "Посмотреть",
|
"view": "Посмотреть",
|
||||||
"are-no-violations": "Нарушений нет",
|
"are-no-violations": "Нарушений нет",
|
||||||
"error-reading-image": "Ошибка чтения изображения"
|
"error-reading-image": "Ошибка чтения изображения",
|
||||||
|
"error-user-not-have-tokens-for-protect": "У пользователя нет токенов для защиты."
|
||||||
},
|
},
|
||||||
"Login-register-form": {
|
"Login-register-form": {
|
||||||
"and": "и",
|
"and": "и",
|
||||||
|
|||||||
Reference in New Issue
Block a user