From 51f4ded427abb6fd96c7d65e179fb367d450448c Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 5 Jan 2022 12:28:42 -0500 Subject: [PATCH] [Fleet] Handle error correctly in fleet server host input on prem instructions (#122341) (#122351) Co-authored-by: Nicolas Chaulet --- .../fleet_server_on_prem_instructions.tsx | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx index b2eaf904ee1bb..0f8c719c2a337 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_requirements_page/components/fleet_server_on_prem_instructions.tsx @@ -295,20 +295,15 @@ export const useFleetServerInstructions = (policyId?: string) => { const addFleetServerHost = useCallback( async (host: string) => { - try { - await sendPutSettings({ - fleet_server_hosts: [host, ...(settings?.item.fleet_server_hosts || [])], - }); - await refreshSettings(); - } catch (err) { - notifications.toasts.addError(err, { - title: i18n.translate('xpack.fleet.fleetServerSetup.errorAddingFleetServerHostTitle', { - defaultMessage: 'Error adding Fleet Server host', - }), - }); + const res = await sendPutSettings({ + fleet_server_hosts: [host, ...(settings?.item.fleet_server_hosts || [])], + }); + if (res.error) { + throw res.error; } + await refreshSettings(); }, - [refreshSettings, notifications.toasts, settings?.item.fleet_server_hosts] + [refreshSettings, settings?.item.fleet_server_hosts] ); return { @@ -427,6 +422,7 @@ export const AddFleetServerHostStepContent = ({ const [isLoading, setIsLoading] = useState(false); const [fleetServerHost, setFleetServerHost] = useState(''); const [error, setError] = useState(); + const { notifications } = useStartServices(); const { getHref } = useLink(); @@ -457,10 +453,16 @@ export const AddFleetServerHostStepContent = ({ } else { setCalloutHost(''); } + } catch (err) { + notifications.toasts.addError(err, { + title: i18n.translate('xpack.fleet.fleetServerSetup.errorAddingFleetServerHostTitle', { + defaultMessage: 'Error adding Fleet Server host', + }), + }); } finally { setIsLoading(false); } - }, [fleetServerHost, addFleetServerHost, validate]); + }, [fleetServerHost, addFleetServerHost, validate, notifications.toasts]); const onChange = useCallback( (e: React.ChangeEvent) => {