Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

Error catch bypass? #196

Closed ChadWKirk closed 1 year ago

ChadWKirk commented 1 year ago

How do I bypass the default error handling and make my own? I would like to show the user an error message when they try to upload a file that is not an image. I see the error handling is in index.js but how do I access that to change it? It's not in my node modules folder in vs code.

Thanks!

Donaldcwl commented 1 year ago

You can use a try-catch block to wrap the function call and write you own error handling codes.

try {
    const compressedFile = await imageCompression(imageFile, options);
    console.log('compressedFile instanceof Blob', compressedFile instanceof Blob); // true
    console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`); // smaller than maxSizeMB

    await uploadToServer(compressedFile); 
  } catch (error) {
    console.log(error);
    // write your own error handling
  }