mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 05:57:05 +02:00
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:
parent
dcbf9f644e
commit
f903422617
58 changed files with 1123 additions and 299 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue