Skip to content

Commit

Permalink
Added detailed error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantpatil1214 committed Nov 22, 2024
1 parent 2498246 commit ea02c53
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 35 deletions.
63 changes: 47 additions & 16 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,52 +52,83 @@ routes.post('/upload', upload.single('file'), async (req, res) => {

if (!file) {
logger.error('No file uploaded');
return res.status(400).send('No file uploaded');
return res.status(400).json({
status: 'error',
code: 'FILE_MISSING',
message: 'No file uploaded'
});
}

if (!bucket) {
logger.error('No bucket provided');
return res.status(400).send('No bucket provided');
return res.status(400).json({
status: 'error',
code: 'BUCKET_MISSING',
message: 'No bucket provided'
});
}

if (!isValidFileType(file)) {
logger.error(`Invalid file type: ${file.mimetype}`);
return res.status(400).send('Invalid file type. Please upload either a CSV or JSON file');
return res.status(400).json({
status: 'error',
code: 'INVALID_FILE_TYPE',
message: 'Invalid file type. Please upload either a CSV or JSON file'
});
}

// For CSV files, validate headers
if (file.mimetype === 'text/csv') {
const headers = getCsvHeaders(file.buffer);
if (!headers) {
return res.status(400).send('Invalid CSV file format');
return res.status(400).json({
status: 'error',
code: 'INVALID_CSV_FORMAT',
message: 'Invalid CSV file format'
});
}
const fileUrl = saveCsvToTmp(file.buffer, file.originalname);
try {
const uploadResult = await uploadToMinio(fileUrl, file.originalname, bucket as string, file.mimetype);
// Clean up the temporary file
fs.unlinkSync(fileUrl);

if (uploadResult) {
return res.status(201).send(`File ${file.originalname} uploaded in bucket ${bucket}`);
if (uploadResult.success) {
return res.status(201).json({
status: 'success',
code: 'UPLOAD_SUCCESS',
message: uploadResult.message
});
} else {
return res.status(400).send(`Object ${file.originalname} already exists in bucket ${bucket}`);
return res.status(400).json({
status: 'error',
code: 'UPLOAD_FAILED',
message: uploadResult.message
});
}
} catch (error) {
// Clean up the temporary file in case of error
fs.unlinkSync(fileUrl);
} catch (error: unknown) {
logger.error('Error uploading file to Minio:', error);
return res.status(500).send('Error uploading file');
return res.status(500).json({
status: 'error',
code: 'INTERNAL_SERVER_ERROR',
message: error instanceof Error ? error.message : 'Unknown error'
});
}
} else if (file.mimetype === 'application/json') {
if (!validateJsonFile(file.buffer)) {
return res.status(400).send('Invalid JSON file format');
return res.status(400).json({
status: 'error',
code: 'INVALID_JSON_FORMAT',
message: 'Invalid JSON file format'
});
}

return res.status(200).send('JSON file is valid - Future implementation');
} else {
return res.status(400).send('Invalid file type. Please upload either a CSV or JSON file');
return res.status(200).json({
status: 'success',
code: 'JSON_VALID',
message: 'JSON file is valid - Future implementation'
});
}

});

export default routes;
47 changes: 28 additions & 19 deletions src/utils/minioClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin
logger.info(`Bucket ${bucket} created in "${bucketRegion}".`);
}


try {
const fileExists = await checkFileExists(destinationObject, bucket, fileType);
if (fileExists) {
return false;
const fileCheck = await checkFileExists(destinationObject, bucket, fileType);
if (fileCheck.exists) {
return { success: false, message: fileCheck.message };
} else {
const metaData = {
'Content-Type': fileType,
Expand All @@ -42,10 +41,11 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin
// Upload the file
await minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData);
logger.info(`File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`);
return true;
return {success: true, message: `File ${sourceFile} uploaded as object ${destinationObject} in bucket ${bucket}`};
}
} catch (error) {
console.error('Error checking file:', error);
return {success: false, message: `Error uploading file: ${error}`};
}
}

Expand All @@ -55,7 +55,7 @@ export async function uploadToMinio(sourceFile: string, destinationObject: strin
* @param {string} bucket - Bucket name
* @returns {Promise<boolean>} - Returns true if file exists, false otherwise
*/
export async function checkFileExists(fileName: string, bucket: string, fileType: string): Promise<boolean> {
export async function checkFileExists(fileName: string, bucket: string, fileType: string): Promise<{ exists: boolean, message: string }> {
const minioClient = new Minio.Client({
endPoint,
port,
Expand All @@ -68,27 +68,36 @@ export async function checkFileExists(fileName: string, bucket: string, fileType
// Check if bucket exists first
const bucketExists = await minioClient.bucketExists(bucket);
if (!bucketExists) {
logger.info(`Bucket ${bucket} does not exist`);
return false;
return {
exists: false,
message: `Bucket ${bucket} does not exist`
};
}

// Get object stats to check if file exists
const stats = await minioClient.statObject(bucket, fileName); // Optionally verify it's a CSV file by checking Content-Type
const stats = await minioClient.statObject(bucket, fileName);
if (stats.metaData && stats.metaData['content-type'] === fileType) {
logger.info(`File ${fileName} exists in bucket ${bucket}`);
return true;
return {
exists: true,
message: `File ${fileName} exists in bucket ${bucket}`
};
} else {
logger.info(`File ${fileName} does not exist in bucket ${bucket}`);
return false;
return {
exists: false,
message: `File ${fileName} does not exist in bucket ${bucket}`
};
}
} catch (err: any) {
if (err.code === 'NotFound') {
logger.debug(`File ${fileName} not found in bucket ${bucket}`);
return false;
return {
exists: false,
message: `File ${fileName} not found in bucket ${bucket}`
};
}
// For any other error, log it and rethrow
logger.error(`Error checking file existence: ${err.message}`);
throw err;
// For any other error, return error message
return {
exists: false,
message: `Error checking file existence: ${err.message}`
};
}

}

0 comments on commit ea02c53

Please sign in to comment.