From 202b26487f7ed0bf94d84f49de0a360c39d202ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 2 Dec 2024 13:28:20 +0100 Subject: [PATCH] Do not stringify errors when logging them Converting errors to string drops their stack trace, making it more difficult to debug their actual reason. We can instead pass the error objects as-is to console.warn/error, so that Firefox/Chrome devtools will show both the stack trace of the console.warn/error call, and the original stack trace of the error. This commit also enables the `unicorn/no-console-spaces` ESLint rule, which avoids accidental extra spaces when passing multiple parameters to `console.*` methods. --- eslint.config.mjs | 1 + test/downloadutils.mjs | 2 +- test/driver.js | 2 +- test/unit/testreporter.js | 2 +- web/app.js | 10 +++++----- web/download_manager.js | 2 +- web/firefoxcom.js | 2 +- web/l10n.js | 2 +- web/pdf_page_view.js | 10 +++++----- web/pdf_rendering_queue.js | 2 +- web/pdf_scripting_manager.js | 4 ++-- web/viewer.js | 2 +- 12 files changed, 21 insertions(+), 20 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index caf6c01a4..ea65d380b 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -112,6 +112,7 @@ export default [ "perfectionist/sort-named-exports": "error", "unicorn/no-abusive-eslint-disable": "error", "unicorn/no-array-push-push": "error", + "unicorn/no-console-spaces": "error", "unicorn/no-instanceof-array": "error", "unicorn/no-invalid-remove-event-listener": "error", "unicorn/no-new-buffer": "error", diff --git a/test/downloadutils.mjs b/test/downloadutils.mjs index 682e916e3..6d80e2153 100644 --- a/test/downloadutils.mjs +++ b/test/downloadutils.mjs @@ -86,7 +86,7 @@ async function downloadManifestFiles(manifest) { try { await downloadFile(file, url); } catch (ex) { - console.error(`Error during downloading of ${url}: ${ex}`); + console.error(`Error during downloading of ${url}:`, ex); fs.writeFileSync(file, ""); // making it empty file fs.writeFileSync(`${file}.error`, ex); } diff --git a/test/driver.js b/test/driver.js index 75bc8a916..15c6c5952 100644 --- a/test/driver.js +++ b/test/driver.js @@ -1192,7 +1192,7 @@ class Driver { resolve(); }) .catch(reason => { - console.warn(`Driver._send failed (${url}): ${reason}`); + console.warn(`Driver._send failed (${url}):`, reason); this.inFlightRequests--; resolve(); diff --git a/test/unit/testreporter.js b/test/unit/testreporter.js index 05abc5184..4c5b6b890 100644 --- a/test/unit/testreporter.js +++ b/test/unit/testreporter.js @@ -18,7 +18,7 @@ const TestReporter = function (browser) { resolve(); }) .catch(reason => { - console.warn(`TestReporter - send failed (${action}): ${reason}`); + console.warn(`TestReporter - send failed (${action}):`, reason); resolve(); send(action, json); diff --git a/web/app.js b/web/app.js index aa4bba755..fdae4bffb 100644 --- a/web/app.js +++ b/web/app.js @@ -202,7 +202,7 @@ const PDFViewerApplication = { try { await this.preferences.initializedPromise; } catch (ex) { - console.error(`initialize: "${ex.message}".`); + console.error("initialize:", ex); } if (AppOptions.get("pdfBugEnabled")) { await this._parseHashParams(); @@ -306,7 +306,7 @@ const PDFViewerApplication = { await __non_webpack_import__(PDFWorker.workerSrc); } } catch (ex) { - console.error(`_parseHashParams: "${ex.message}".`); + console.error("_parseHashParams:", ex); } } if (params.has("textlayer")) { @@ -322,7 +322,7 @@ const PDFViewerApplication = { await loadPDFBug(); this._PDFBug.loadCSS(); } catch (ex) { - console.error(`_parseHashParams: "${ex.message}".`); + console.error("_parseHashParams:", ex); } break; } @@ -335,7 +335,7 @@ const PDFViewerApplication = { await loadPDFBug(); this._PDFBug.init(mainContainer, enabled); } catch (ex) { - console.error(`_parseHashParams: "${ex.message}".`); + console.error("_parseHashParams:", ex); } } // It is not possible to change locale for the (various) extension builds. @@ -1121,7 +1121,7 @@ const PDFViewerApplication = { this.downloadManager.download(data, this._downloadUrl, this._docFilename); } catch (reason) { // When the PDF document isn't ready, fallback to a "regular" download. - console.error(`Error when saving the document: ${reason.message}`); + console.error(`Error when saving the document:`, reason); await this.download(); } finally { await this.pdfScriptingManager.dispatchDidSave(); diff --git a/web/download_manager.js b/web/download_manager.js index 255806f03..26d6200b3 100644 --- a/web/download_manager.js +++ b/web/download_manager.js @@ -93,7 +93,7 @@ class DownloadManager { window.open(viewerUrl); return true; } catch (ex) { - console.error(`openOrDownloadData: ${ex}`); + console.error("openOrDownloadData:", ex); // Release the `blobUrl`, since opening it failed, and fallback to // downloading the PDF file. URL.revokeObjectURL(blobUrl); diff --git a/web/firefoxcom.js b/web/firefoxcom.js index f8e711686..5b98d17a6 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -121,7 +121,7 @@ class DownloadManager { window.open(viewerUrl); return true; } catch (ex) { - console.error(`openOrDownloadData: ${ex}`); + console.error("openOrDownloadData:", ex); // Release the `blobUrl`, since opening it failed, and fallback to // downloading the PDF file. URL.revokeObjectURL(blobUrl); diff --git a/web/l10n.js b/web/l10n.js index 053614e98..ad469a526 100644 --- a/web/l10n.js +++ b/web/l10n.js @@ -85,7 +85,7 @@ class L10n { try { await this.#l10n.translateElements([element]); } catch (ex) { - console.error(`translateOnce: "${ex}".`); + console.error("translateOnce:", ex); } } diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 010aa18ae..d0f546ceb 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -401,7 +401,7 @@ class PDFPageView { "display" ); } catch (ex) { - console.error(`#renderAnnotationLayer: "${ex}".`); + console.error("#renderAnnotationLayer:", ex); error = ex; } finally { this.#dispatchLayerRendered("annotationlayerrendered", error); @@ -413,7 +413,7 @@ class PDFPageView { try { await this.annotationEditorLayer.render(this.viewport, "display"); } catch (ex) { - console.error(`#renderAnnotationEditorLayer: "${ex}".`); + console.error("#renderAnnotationEditorLayer:", ex); error = ex; } finally { this.#dispatchLayerRendered("annotationeditorlayerrendered", error); @@ -424,7 +424,7 @@ class PDFPageView { try { await this.drawLayer.render("display"); } catch (ex) { - console.error(`#renderDrawLayer: "${ex}".`); + console.error("#renderDrawLayer:", ex); } } @@ -440,7 +440,7 @@ class PDFPageView { this.#buildXfaTextContentItems(result.textDivs); } } catch (ex) { - console.error(`#renderXfaLayer: "${ex}".`); + console.error("#renderXfaLayer:", ex); error = ex; } finally { if (this.xfaLayer?.div) { @@ -465,7 +465,7 @@ class PDFPageView { if (ex instanceof AbortException) { return; } - console.error(`#renderTextLayer: "${ex}".`); + console.error("#renderTextLayer:", ex); error = ex; } this.#dispatchLayerRendered("textlayerrendered", error); diff --git a/web/pdf_rendering_queue.js b/web/pdf_rendering_queue.js index 60e885e22..620eae6c1 100644 --- a/web/pdf_rendering_queue.js +++ b/web/pdf_rendering_queue.js @@ -201,7 +201,7 @@ class PDFRenderingQueue { if (reason instanceof RenderingCancelledException) { return; } - console.error(`renderView: "${reason}"`); + console.error("renderView:", reason); }); break; } diff --git a/web/pdf_scripting_manager.js b/web/pdf_scripting_manager.js index 63d08c604..402613f87 100644 --- a/web/pdf_scripting_manager.js +++ b/web/pdf_scripting_manager.js @@ -105,7 +105,7 @@ class PDFScriptingManager { try { this.#scripting = this.#initScripting(); } catch (error) { - console.error(`setDocument: "${error.message}".`); + console.error("setDocument:", error); await this.#destroyScripting(); return; @@ -192,7 +192,7 @@ class PDFScriptingManager { eventBus.dispatch("sandboxcreated", { source: this }); } catch (error) { - console.error(`setDocument: "${error.message}".`); + console.error("setDocument:", error); await this.#destroyScripting(); return; diff --git a/web/viewer.js b/web/viewer.js index 30eefd022..5a5cc54ae 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -248,7 +248,7 @@ function webViewerLoad() { } catch (ex) { // The viewer could be in e.g. a cross-origin