From 853570dd00eafdf57a44ff04a1c486fe116e1754 Mon Sep 17 00:00:00 2001 From: Connor G Meehan Date: Tue, 30 Apr 2024 12:53:52 +1000 Subject: [PATCH 1/5] fix: Brittle response parsing from notarytool --- src/notarytool.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/notarytool.ts b/src/notarytool.ts index 6b43431..5b43d1c 100644 --- a/src/notarytool.ts +++ b/src/notarytool.ts @@ -83,9 +83,17 @@ export async function notarizeAndWaitForNotaryTool(opts: NotaryToolStartOptions) ]; const result = await spawn('xcrun', notarizeArgs); - const parsed = JSON.parse(result.output.trim()); + if (result.code !== 0) { + throw new Error(`Failed to notarize via notarytool\n\n${result.output}`) + } + let parsed: any; + try { + parsed = JSON.parse(result.output.trim()); + } catch (err) { + throw new Error(`Failed to notarize via notarytool\n\nCould not parse output as JSON\n\n${result.output.trim()}`); + } - if (result.code !== 0 || !parsed.status || parsed.status !== 'Accepted') { + if (!parsed.status || parsed.status !== 'Accepted') { try { if (parsed && parsed.id) { const logResult = await spawn('xcrun', [ From a7698ef50193b6d1e105dffc98c99bcf28ef4b53 Mon Sep 17 00:00:00 2001 From: Connor G Meehan Date: Tue, 30 Apr 2024 13:30:08 +1000 Subject: [PATCH 2/5] fix: Fixes result.code check blocking notarytool log diagnostics --- src/notarytool.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/notarytool.ts b/src/notarytool.ts index 5b43d1c..c3ecaaf 100644 --- a/src/notarytool.ts +++ b/src/notarytool.ts @@ -83,9 +83,6 @@ export async function notarizeAndWaitForNotaryTool(opts: NotaryToolStartOptions) ]; const result = await spawn('xcrun', notarizeArgs); - if (result.code !== 0) { - throw new Error(`Failed to notarize via notarytool\n\n${result.output}`) - } let parsed: any; try { parsed = JSON.parse(result.output.trim()); From 514cb3dd20d21cf22be52e10422400bfffaef59a Mon Sep 17 00:00:00 2001 From: Connor G Meehan Date: Wed, 8 May 2024 18:21:10 +1000 Subject: [PATCH 3/5] fix: Concat notarytool error with notarytool log result --- src/notarytool.ts | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/notarytool.ts b/src/notarytool.ts index c3ecaaf..311607b 100644 --- a/src/notarytool.ts +++ b/src/notarytool.ts @@ -9,6 +9,7 @@ import { isNotaryToolApiKeyCredentials, } from './validate-args'; import { NotaryToolCredentials, NotaryToolStartOptions } from './types'; +import { parse } from 'path'; const d = debug('electron-notarize:notarytool'); @@ -83,16 +84,25 @@ export async function notarizeAndWaitForNotaryTool(opts: NotaryToolStartOptions) ]; const result = await spawn('xcrun', notarizeArgs); - let parsed: any; - try { - parsed = JSON.parse(result.output.trim()); - } catch (err) { - throw new Error(`Failed to notarize via notarytool\n\nCould not parse output as JSON\n\n${result.output.trim()}`); - } - if (!parsed.status || parsed.status !== 'Accepted') { + if (result.code !== 0) { + let parsed: any; try { - if (parsed && parsed.id) { + parsed = JSON.parse(result.output.trim()); + } catch (err) { + throw new Error( + `Failed to notarize via notarytool. Failed with unexpected result: \n\n${result.output.trim()}`, + ); + } + + let logId: undefined | string; + if (!parsed.status || parsed.status !== 'Accepted') { + logId = parsed.id; + } + + let logOutput: undefined | string; + if (logId) { + try { const logResult = await spawn('xcrun', [ 'notarytool', 'log', @@ -100,12 +110,19 @@ export async function notarizeAndWaitForNotaryTool(opts: NotaryToolStartOptions) ...authorizationArgs(opts), ]); d('notarization log', logResult.output); + logOutput = logResult.output; + } catch (e) { + d('failed to pull notarization logs', e); } - } catch (e) { - d('failed to pull notarization logs', e); } - throw new Error(`Failed to notarize via notarytool\n\n${result.output}`); + + let message = `Failed to notarize via notarytool\n\n${result.output}`; + if (logOutput) { + message += `\n\nDiagnostics from notarytool log: ${logOutput}`; + } + throw new Error(message); } + d('notarization success'); }); } From 77eda121a3c78b83174089c6e59a4721708d6c38 Mon Sep 17 00:00:00 2001 From: Connor G Meehan Date: Wed, 8 May 2024 18:23:43 +1000 Subject: [PATCH 4/5] clean: Cleanup unused import --- src/notarytool.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/notarytool.ts b/src/notarytool.ts index 311607b..e1b7ef3 100644 --- a/src/notarytool.ts +++ b/src/notarytool.ts @@ -9,7 +9,6 @@ import { isNotaryToolApiKeyCredentials, } from './validate-args'; import { NotaryToolCredentials, NotaryToolStartOptions } from './types'; -import { parse } from 'path'; const d = debug('electron-notarize:notarytool'); From 195de70e57474e8bf0a2955a61e8a0a1103cdece Mon Sep 17 00:00:00 2001 From: Connor G Meehan Date: Thu, 9 May 2024 09:26:34 +1000 Subject: [PATCH 5/5] refact: Simplified notary error handling logic --- src/notarytool.ts | 64 ++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/src/notarytool.ts b/src/notarytool.ts index e1b7ef3..d2067dc 100644 --- a/src/notarytool.ts +++ b/src/notarytool.ts @@ -84,44 +84,40 @@ export async function notarizeAndWaitForNotaryTool(opts: NotaryToolStartOptions) const result = await spawn('xcrun', notarizeArgs); - if (result.code !== 0) { - let parsed: any; - try { - parsed = JSON.parse(result.output.trim()); - } catch (err) { - throw new Error( - `Failed to notarize via notarytool. Failed with unexpected result: \n\n${result.output.trim()}`, - ); - } - - let logId: undefined | string; - if (!parsed.status || parsed.status !== 'Accepted') { - logId = parsed.id; - } + if (result.code === 0) { + d('notarization success'); + return; + } - let logOutput: undefined | string; - if (logId) { - try { - const logResult = await spawn('xcrun', [ - 'notarytool', - 'log', - parsed.id, - ...authorizationArgs(opts), - ]); - d('notarization log', logResult.output); - logOutput = logResult.output; - } catch (e) { - d('failed to pull notarization logs', e); - } - } + let parsed: any; + try { + parsed = JSON.parse(result.output.trim()); + } catch (err) { + throw new Error( + `Failed to notarize via notarytool. Failed with unexpected result: \n\n${result.output.trim()}`, + ); + } - let message = `Failed to notarize via notarytool\n\n${result.output}`; - if (logOutput) { - message += `\n\nDiagnostics from notarytool log: ${logOutput}`; + let logOutput: undefined | string; + if (parsed.id) { + try { + const logResult = await spawn('xcrun', [ + 'notarytool', + 'log', + parsed.id, + ...authorizationArgs(opts), + ]); + d('notarization log', logResult.output); + logOutput = logResult.output; + } catch (e) { + d('failed to pull notarization logs', e); } - throw new Error(message); } - d('notarization success'); + let message = `Failed to notarize via notarytool\n\n${result.output}`; + if (logOutput) { + message += `\n\nDiagnostics from notarytool log: ${logOutput}`; + } + throw new Error(message); }); }