Skip to content

Commit

Permalink
refactor: throw errors directly in async functions (electron#39233)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored Jul 31, 2023
1 parent 68701c4 commit 724f90a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
35 changes: 17 additions & 18 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,28 @@ WebContents.prototype.printToPDF = async function (options) {

if (options.landscape !== undefined) {
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;
}

if (options.displayHeaderFooter !== undefined) {
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;
}

if (options.printBackground !== undefined) {
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;
}

if (options.scale !== undefined) {
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;
}
Expand All @@ -242,82 +242,82 @@ WebContents.prototype.printToPDF = async function (options) {
if (typeof pageSize === 'string') {
const format = paperFormats[pageSize.toLowerCase()];
if (!format) {
return Promise.reject(new Error(`Invalid pageSize ${pageSize}`));
throw new Error(`Invalid pageSize ${pageSize}`);
}

printSettings.paperWidth = format.width;
printSettings.paperHeight = format.height;
} else if (typeof options.pageSize === 'object') {
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.paperHeight = pageSize.height;
} 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;
if (margins !== undefined) {
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 (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;
}

if (margins.bottom !== undefined) {
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;
}

if (margins.left !== undefined) {
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;
}

if (margins.right !== undefined) {
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;
}
}

if (options.pageRanges !== undefined) {
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;
}

if (options.headerTemplate !== undefined) {
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;
}

if (options.footerTemplate !== undefined) {
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;
}

if (options.preferCSSPageSize !== undefined) {
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;
}
Expand All @@ -330,8 +330,7 @@ WebContents.prototype.printToPDF = async function (options) {
}
return pendingPromise;
} else {
const error = new Error('Printing feature is disabled');
return Promise.reject(error);
throw new Error('Printing feature is disabled');
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/common/deprecate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function log (message: string): void {

// remove a function with no replacement
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
const warn = warnOnce(`${fn.name} function`);
Expand Down
2 changes: 1 addition & 1 deletion spec/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ describe('app module', () => {
}
} else {
// return error if not clean exit
return Promise.reject(new Error(errorData));
throw new Error(errorData);
}
};
const verifyBasicGPUInfo = async (gpuInfo: any) => {
Expand Down

0 comments on commit 724f90a

Please sign in to comment.