From c2f277b6cb3bd2eeb22993cb6d52f96e9b0d6037 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 23 Sep 2023 10:47:37 +0100 Subject: [PATCH] fix(index): expose error codes for pdffonts and pdfinfo --- src/index.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 51e351df..dc5feb25 100644 --- a/src/index.js +++ b/src/index.js @@ -254,11 +254,23 @@ class Poppler { stdErr += data; }); - child.on("close", () => { + child.on("close", (code) => { + /* istanbul ignore else */ if (stdOut !== "") { resolve(stdOut.trim()); + } else if (code === 0) { + resolve(errorMessages[code]); + } else if (stdErr !== "") { + reject(new Error(stdErr.trim())); } else { - reject(new Error(stdErr ? stdErr.trim() : undefined)); + reject( + new Error( + errorMessages[code] || + `pdffonts ${args.join( + " " + )} exited with code ${code}` + ) + ); } }); }); @@ -479,7 +491,8 @@ class Poppler { stdErr += data; }); - child.on("close", () => { + child.on("close", (code) => { + /* istanbul ignore else */ if (stdOut !== "") { if (fileSize) { stdOut = stdOut.replace( @@ -505,8 +518,19 @@ class Poppler { } else { resolve(stdOut.trim()); } + } else if (code === 0) { + resolve(errorMessages[code]); + } else if (stdErr !== "") { + reject(new Error(stdErr.trim())); } else { - reject(new Error(stdErr ? stdErr.trim() : undefined)); + reject( + new Error( + errorMessages[code] || + `pdfinfo ${args.join( + " " + )} exited with code ${code}` + ) + ); } }); });