How to handle "MongoServerError" which is causing server crash

How to handle MongoServerError: you are over your space quota, using 514 MB of 512 MB
in nodejs server while uploading a file using multer-gridfs-storage library to avoid server crash

Here is my code:

const storage = new GridFsStorage({
    url: process.env.MONGO_CONN_URL,
    file: (req, file) => {
      return new Promise( async (resolve, reject) => {

        const filename = file.originalname;
        const shortname = await hashName();

        console.log(req.body);

        const expTime = new Date(req.body.expiryTime);
        const noOfDown = parseInt(req.body.noOfDownload);
        const isPublic = req.body.isPublic==='true';
        const passwordValue = req.body.password;

        var uploadedBy = 'anonymous';
        if(req.cookies!==undefined && req.cookies.pass!==undefined){
          try{
            const token = req.cookies.pass;
            const payload=jwt.verify(token,process.env.JWT_PRIVATE_KEY);
            uploadedBy = payload.userId;
          }catch(err){
            console.log('catch block executed');
            
          }
        }

        const fileInfo = {
          filename: filename,
          metadata:{
            shortname: shortname,
            expiryTime: expTime,
            noOfDownload: noOfDown,
            isPublic: isPublic,
            password: passwordValue,
            uploadedBy: uploadedBy
          },
          bucketName: "newBucket"
        };
        resolve(fileInfo);
      });
    }
  });  

  const upload = multer({
    storage
  });

app.post('/upload',upload.single("fileUpload"),async (req,res)=>{

    const file = req.file;
    
    console.log("File has been uploaded. File   id: "+file.metadata.shortname);
    res.send(file.metadata.shortname)

});

Github link: https://github.com/Dy-123/File_Sharing/blob/master/backend/index.js

I tried storage.on streamError and dbError but it is not working

Hey @Divyanshu_N_A1,

Welcome to the MongoDB Community forums!

When your storage exceeds the allocated limit, it triggers an error with an Atlas error code 8000. To handle this error, you can capture the error code and integrate the following Node.js code snippet into your application

try {
  // ... your file upload logic
} catch (error) {
  if (error instanceof MongoServerError && error.code === 8000) {
    return res.status(500).json({ error: 'MongoDB space quota exceeded' });
  } else {
    // Handle other errors
    console.error('An error occurred during file upload:', error);
    return res.status(500).json({ error: 'An error occurred during file upload' });
  }
}

If you require additional storage capacity, you can expand it by upgrading your Atlas Tier. Additionally, considering that you are uploading images, I recommend storing these images in an AWS S3 bucket or similar object storage solution and storing references to them within your documents for better scalability and cost-efficiency.

However, in case of further assistance, please share your use case and details related to it, which will help the community to assist you better.

Best regards,
Kushagra