diff --git a/bin/cli.js b/bin/cli.js index 916cfb0d..a240c77d 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -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) { diff --git a/lib/browser.js b/lib/browser.js index bd42990d..7aa7e16f 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -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; } @@ -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); } } diff --git a/lib/chart.js b/lib/chart.js index 47b28f8d..9f39f5c7 100644 --- a/lib/chart.js +++ b/lib/chart.js @@ -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 ) ); } diff --git a/lib/errors/ExportError.js b/lib/errors/ExportError.js index be659551..290e27de 100644 --- a/lib/errors/ExportError.js +++ b/lib/errors/ExportError.js @@ -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) {