From 8bed5f96cd59a18fecf1de836d495a703f608c72 Mon Sep 17 00:00:00 2001 From: Sonja Krause-Harder Date: Thu, 1 Apr 2021 18:19:36 +0200 Subject: [PATCH] Correctly catch errors from _installPackage() --- .../server/services/epm/packages/install.ts | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/epm/packages/install.ts b/x-pack/plugins/fleet/server/services/epm/packages/install.ts index 8d01f4c29b9236f..1b64dc3e1f34ec2 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/install.ts @@ -286,29 +286,31 @@ async function installPackageFromRegistry({ const { paths, packageInfo } = await Registry.getRegistryPackage(pkgName, pkgVersion); // try installing the package, if there was an error, call error handler and rethrow - try { - return _installPackage({ - savedObjectsClient, - esClient, - installedPkg, - paths, - packageInfo, - installType, - installSource: 'registry', - }).then((assets) => { + // TODO: without the ts-ignore, TS complains about the type of the value of the returned InstallResult.status + // @ts-ignore + return _installPackage({ + savedObjectsClient, + esClient, + installedPkg, + paths, + packageInfo, + installType, + installSource: 'registry', + }) + .then((assets) => { return { assets, status: 'installed', installType }; + }) + .catch(async (err: Error) => { + await handleInstallPackageFailure({ + savedObjectsClient, + error: err, + pkgName, + pkgVersion, + installedPkg, + esClient, + }); + return { error: err, installType }; }); - } catch (e) { - await handleInstallPackageFailure({ - savedObjectsClient, - error: e, - pkgName, - pkgVersion, - installedPkg, - esClient, - }); - throw e; - } } catch (e) { return { error: e, @@ -361,7 +363,8 @@ async function installPackageByUpload({ version: packageInfo.version, packageInfo, }); - + // TODO: without the ts-ignore, TS complains about the type of the value of the returned InstallResult.status + // @ts-ignore return _installPackage({ savedObjectsClient, esClient, @@ -370,9 +373,13 @@ async function installPackageByUpload({ packageInfo, installType, installSource, - }).then((assets) => { - return { assets, status: 'installed', installType }; - }); + }) + .then((assets) => { + return { assets, status: 'installed', installType }; + }) + .catch(async (err: Error) => { + return { error: err, installType }; + }); } catch (e) { return { error: e, installType }; }