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