refactor: unify error handling behavior (#6755)

* refactor: unify error handling behavior

* revert

* normalize paths

* test...

* change

* does this work...

* fix...

* maybe fix

* maybe fix

* fix

* fix...
This commit is contained in:
Joshua Chen 2022-02-25 15:07:13 +08:00 committed by GitHub
parent dcbf9f644e
commit f903422617
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 1123 additions and 299 deletions

View file

@ -71,8 +71,8 @@ export default async function lqipLoader(
} else {
callback(new Error('ERROR'), undefined);
}
} catch (error) {
console.error(error);
} catch (err) {
console.error(err);
callback(new Error('ERROR'), undefined);
}
}

View file

@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import logger from '@docusaurus/logger';
import Vibrant from 'node-vibrant';
import path from 'path';
import sharp from 'sharp';
@ -22,29 +23,29 @@ const SUPPORTED_MIMES: Record<string, string> = {
};
export async function base64(file: string): Promise<string> {
let extension = path.extname(file) || '';
let extension = path.extname(file);
extension = extension.split('.').pop()!;
if (!SUPPORTED_MIMES[extension]) {
throw new Error(ERROR_EXT);
}
const data = await sharp(file).resize(10).toBuffer();
if (data) {
try {
const data = await sharp(file).resize(10).toBuffer();
return toBase64(SUPPORTED_MIMES[extension], data);
} catch (err) {
logger.error`Generation of base64 failed for image path=${file}.`;
throw err;
}
throw new Error('Unhandled promise rejection in base64 promise');
}
export async function palette(file: string): Promise<string[]> {
const vibrant = new Vibrant(file, {});
const pal = await vibrant.getPalette();
if (pal) {
try {
const pal = await vibrant.getPalette();
return toPalette(pal);
} catch (err) {
logger.error`Generation of color palette failed for image path=${file}.`;
throw err;
}
throw new Error(`Unhandled promise rejection in colorPalette ${pal}`);
}
process.on('unhandledRejection', (up) => {
throw up;
});