Components: ImageEditor: add error handler

This commit is contained in:
Viktor Fomin 2023-05-02 14:22:28 +03:00
parent 93fd498284
commit df4c90d838

View File

@ -76,37 +76,45 @@ const Dropzone = ({ t, setUploadedFile, isDisabled }) => {
timer.current = setTimeout(() => { timer.current = setTimeout(() => {
setLoadingFile(true); setLoadingFile(true);
}, 50); }, 50);
const imageBitMap = await createImageBitmap(file); try {
const imageBitMap = await createImageBitmap(file);
const width = imageBitMap.width; const width = imageBitMap.width;
const height = imageBitMap.height; const height = imageBitMap.height;
const canvas = resizeImage.resize2Canvas(imageBitMap, width, height); const canvas = resizeImage.resize2Canvas(imageBitMap, width, height);
resizeRecursiveAsync( resizeRecursiveAsync(
{ width, height }, { width, height },
canvas, canvas,
file.size > ONE_MEGABYTE ? COMPRESSION_RATIO : NO_COMPRESSION_RATIO file.size > ONE_MEGABYTE ? COMPRESSION_RATIO : NO_COMPRESSION_RATIO
) )
.then((file) => { .then((file) => {
if (mount.current) { if (mount.current) {
setUploadedFile(file); setUploadedFile(file);
} }
}) })
.catch((error) => { .catch((error) => {
if ( if (
error instanceof Error && error instanceof Error &&
error.message === "recursion depth exceeded" error.message === "recursion depth exceeded"
) { ) {
toastr.error(t("Common:SizeImageLarge")); toastr.error(t("Common:SizeImageLarge"));
} }
console.error(error); console.error(error);
}) })
.finally(() => { .finally(() => {
timer.current && clearTimeout(timer.current); timer.current && clearTimeout(timer.current);
if (mount.current) { if (mount.current) {
setLoadingFile(false); setLoadingFile(false);
} }
}); });
} catch (error) {
toastr.error(error);
timer.current && clearTimeout(timer.current);
if (mount.current) {
setLoadingFile(false);
}
}
}; };
const { getRootProps, getInputProps } = useDropzone({ const { getRootProps, getInputProps } = useDropzone({