refactor: throw errors directly in async functions (#39233)

This commit is contained in:
Milan Burda 2023-07-31 10:39:37 +02:00 committed by GitHub
parent 68701c4c3c
commit 724f90a2a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 20 deletions

View file

@ -211,28 +211,28 @@ WebContents.prototype.printToPDF = async function (options) {
if (options.landscape !== undefined) { if (options.landscape !== undefined) {
if (typeof options.landscape !== 'boolean') { if (typeof options.landscape !== 'boolean') {
return Promise.reject(new Error('landscape must be a Boolean')); throw new Error('landscape must be a Boolean');
} }
printSettings.landscape = options.landscape; printSettings.landscape = options.landscape;
} }
if (options.displayHeaderFooter !== undefined) { if (options.displayHeaderFooter !== undefined) {
if (typeof options.displayHeaderFooter !== 'boolean') { if (typeof options.displayHeaderFooter !== 'boolean') {
return Promise.reject(new Error('displayHeaderFooter must be a Boolean')); throw new Error('displayHeaderFooter must be a Boolean');
} }
printSettings.displayHeaderFooter = options.displayHeaderFooter; printSettings.displayHeaderFooter = options.displayHeaderFooter;
} }
if (options.printBackground !== undefined) { if (options.printBackground !== undefined) {
if (typeof options.printBackground !== 'boolean') { if (typeof options.printBackground !== 'boolean') {
return Promise.reject(new Error('printBackground must be a Boolean')); throw new Error('printBackground must be a Boolean');
} }
printSettings.shouldPrintBackgrounds = options.printBackground; printSettings.shouldPrintBackgrounds = options.printBackground;
} }
if (options.scale !== undefined) { if (options.scale !== undefined) {
if (typeof options.scale !== 'number') { if (typeof options.scale !== 'number') {
return Promise.reject(new Error('scale must be a Number')); throw new Error('scale must be a Number');
} }
printSettings.scale = options.scale; printSettings.scale = options.scale;
} }
@ -242,53 +242,53 @@ WebContents.prototype.printToPDF = async function (options) {
if (typeof pageSize === 'string') { if (typeof pageSize === 'string') {
const format = paperFormats[pageSize.toLowerCase()]; const format = paperFormats[pageSize.toLowerCase()];
if (!format) { if (!format) {
return Promise.reject(new Error(`Invalid pageSize ${pageSize}`)); throw new Error(`Invalid pageSize ${pageSize}`);
} }
printSettings.paperWidth = format.width; printSettings.paperWidth = format.width;
printSettings.paperHeight = format.height; printSettings.paperHeight = format.height;
} else if (typeof options.pageSize === 'object') { } else if (typeof options.pageSize === 'object') {
if (!pageSize.height || !pageSize.width) { if (!pageSize.height || !pageSize.width) {
return Promise.reject(new Error('height and width properties are required for pageSize')); throw new Error('height and width properties are required for pageSize');
} }
printSettings.paperWidth = pageSize.width; printSettings.paperWidth = pageSize.width;
printSettings.paperHeight = pageSize.height; printSettings.paperHeight = pageSize.height;
} else { } else {
return Promise.reject(new Error('pageSize must be a String or Object')); throw new Error('pageSize must be a String or Object');
} }
} }
const { margins } = options; const { margins } = options;
if (margins !== undefined) { if (margins !== undefined) {
if (typeof margins !== 'object') { if (typeof margins !== 'object') {
return Promise.reject(new Error('margins must be an Object')); throw new Error('margins must be an Object');
} }
if (margins.top !== undefined) { if (margins.top !== undefined) {
if (typeof margins.top !== 'number') { if (typeof margins.top !== 'number') {
return Promise.reject(new Error('margins.top must be a Number')); throw new Error('margins.top must be a Number');
} }
printSettings.marginTop = margins.top; printSettings.marginTop = margins.top;
} }
if (margins.bottom !== undefined) { if (margins.bottom !== undefined) {
if (typeof margins.bottom !== 'number') { if (typeof margins.bottom !== 'number') {
return Promise.reject(new Error('margins.bottom must be a Number')); throw new Error('margins.bottom must be a Number');
} }
printSettings.marginBottom = margins.bottom; printSettings.marginBottom = margins.bottom;
} }
if (margins.left !== undefined) { if (margins.left !== undefined) {
if (typeof margins.left !== 'number') { if (typeof margins.left !== 'number') {
return Promise.reject(new Error('margins.left must be a Number')); throw new Error('margins.left must be a Number');
} }
printSettings.marginLeft = margins.left; printSettings.marginLeft = margins.left;
} }
if (margins.right !== undefined) { if (margins.right !== undefined) {
if (typeof margins.right !== 'number') { if (typeof margins.right !== 'number') {
return Promise.reject(new Error('margins.right must be a Number')); throw new Error('margins.right must be a Number');
} }
printSettings.marginRight = margins.right; printSettings.marginRight = margins.right;
} }
@ -296,28 +296,28 @@ WebContents.prototype.printToPDF = async function (options) {
if (options.pageRanges !== undefined) { if (options.pageRanges !== undefined) {
if (typeof options.pageRanges !== 'string') { if (typeof options.pageRanges !== 'string') {
return Promise.reject(new Error('pageRanges must be a String')); throw new Error('pageRanges must be a String');
} }
printSettings.pageRanges = options.pageRanges; printSettings.pageRanges = options.pageRanges;
} }
if (options.headerTemplate !== undefined) { if (options.headerTemplate !== undefined) {
if (typeof options.headerTemplate !== 'string') { if (typeof options.headerTemplate !== 'string') {
return Promise.reject(new Error('headerTemplate must be a String')); throw new Error('headerTemplate must be a String');
} }
printSettings.headerTemplate = options.headerTemplate; printSettings.headerTemplate = options.headerTemplate;
} }
if (options.footerTemplate !== undefined) { if (options.footerTemplate !== undefined) {
if (typeof options.footerTemplate !== 'string') { if (typeof options.footerTemplate !== 'string') {
return Promise.reject(new Error('footerTemplate must be a String')); throw new Error('footerTemplate must be a String');
} }
printSettings.footerTemplate = options.footerTemplate; printSettings.footerTemplate = options.footerTemplate;
} }
if (options.preferCSSPageSize !== undefined) { if (options.preferCSSPageSize !== undefined) {
if (typeof options.preferCSSPageSize !== 'boolean') { if (typeof options.preferCSSPageSize !== 'boolean') {
return Promise.reject(new Error('preferCSSPageSize must be a Boolean')); throw new Error('preferCSSPageSize must be a Boolean');
} }
printSettings.preferCSSPageSize = options.preferCSSPageSize; printSettings.preferCSSPageSize = options.preferCSSPageSize;
} }
@ -330,8 +330,7 @@ WebContents.prototype.printToPDF = async function (options) {
} }
return pendingPromise; return pendingPromise;
} else { } else {
const error = new Error('Printing feature is disabled'); throw new Error('Printing feature is disabled');
return Promise.reject(error);
} }
}; };

View file

@ -46,7 +46,7 @@ export function log (message: string): void {
// remove a function with no replacement // remove a function with no replacement
export function removeFunction<T extends Function> (fn: T, removedName: string): T { export function removeFunction<T extends Function> (fn: T, removedName: string): T {
if (!fn) { throw Error(`'${removedName} function' is invalid or does not exist.`); } if (!fn) { throw new Error(`'${removedName} function' is invalid or does not exist.`); }
// wrap the deprecated function to warn user // wrap the deprecated function to warn user
const warn = warnOnce(`${fn.name} function`); const warn = warnOnce(`${fn.name} function`);

View file

@ -1422,7 +1422,7 @@ describe('app module', () => {
} }
} else { } else {
// return error if not clean exit // return error if not clean exit
return Promise.reject(new Error(errorData)); throw new Error(errorData);
} }
}; };
const verifyBasicGPUInfo = async (gpuInfo: any) => { const verifyBasicGPUInfo = async (gpuInfo: any) => {