Skip to content

Commit

Permalink
ExportError: add statusCode to constructor, improve basic status codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jszuminski committed Sep 18, 2024
1 parent e740944 commit 184ac54
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ const start = async () => {
}
} else {
throw new ExportError(
'[cli] No valid options provided. Please check your input and try again.'
'[cli] No valid options provided. Please check your input and try again.',
400
);
}
} catch (error) {
Expand Down
7 changes: 4 additions & 3 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let browser;
*/
export function get() {
if (!browser) {
throw new ExportError('[browser] No valid browser has been created.');
throw new ExportError('[browser] No valid browser has been created.', 500);
}
return browser;
}
Expand Down Expand Up @@ -119,12 +119,13 @@ export async function create(puppeteerArgs) {
}
} catch (error) {
throw new ExportError(
'[browser] Maximum retries to open a browser instance reached.'
'[browser] Maximum retries to open a browser instance reached.',
500
).setError(error);
}

if (!browser) {
throw new ExportError('[browser] Cannot find a browser to open.');
throw new ExportError('[browser] Cannot find a browser to open.', 500);
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ const doExport = async (options, chartJson, endCallback, svg) => {
// these settings.
return endCallback(
new ExportError(
`[chart] The 'callback', 'resources' and 'customCode' options have been disabled for this server.`
`[chart] The 'callback', 'resources' and 'customCode' options have been disabled for this server.`,
400
)
);
}
Expand Down
10 changes: 9 additions & 1 deletion lib/errors/ExportError.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
class ExportError extends Error {
constructor(message) {
/**
* @param {string} message
* @param {number} [statusCode]
*/
constructor(message, statusCode) {
super();
this.message = message;
this.stackMessage = message;

if (this.statusCode) {
this.statusCode = statusCode;
}
}

setError(error) {
Expand Down

0 comments on commit 184ac54

Please sign in to comment.