From 1b4c66101b02dd4c01582a0e7b684eb5b499101a Mon Sep 17 00:00:00 2001 From: Ievgen Sorokopud Date: Fri, 2 Dec 2022 21:17:54 +0100 Subject: [PATCH] [Security Solution] Failed to Fetch Rules and Timeline in Preview Flyout (#146904) ## Summary These changes fixes the issue with the unexpected "Failed to Fetch Rule" error toast that appears when user opens the alert details from the Rule Preview page. The issue was introduced by [recent refactoring](https://github.com/elastic/kibana/pull/142950) where we switched from the optional toast to showing error toast always when it is not possible to fetch the rule (see `useRuleWithFallback` hook). In case of Rule Preview the rule does not exist and thus server returns 404 (not found) error. In case of Rule Preview we would like to ignore this error and build the rule from the alert. Bug Ticket: #146858 --- .../rule_management/logic/use_rule.ts | 17 +++++++++++------ .../logic/use_rule_with_fallback.ts | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts index 234ba95de54d6..f16858edf0da0 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts @@ -15,12 +15,17 @@ import * as i18n from './translations'; * @param id desired Rule ID's (not rule_id) * */ -export const useRule = (id: string) => { +export const useRule = (id: string, showToast = true) => { const { addError } = useAppToasts(); - return useFetchRuleByIdQuery(id, { - onError: (error) => { - addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE }); - }, - }); + return useFetchRuleByIdQuery( + id, + showToast + ? { + onError: (error) => { + addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE }); + }, + } + : undefined + ); }; diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts index 6290c4ae6622f..e9004d80a5f0a 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts @@ -77,7 +77,7 @@ const buildLastAlertQuery = (ruleId: string) => ({ * In that case, try to fetch the latest alert generated by the rule and retrieve the rule data from the alert (fallback). */ export const useRuleWithFallback = (ruleId: string): UseRuleWithFallback => { - const { isLoading: ruleLoading, data: ruleData, error, refetch } = useRule(ruleId); + const { isLoading: ruleLoading, data: ruleData, error, refetch } = useRule(ruleId, false); const { addError } = useAppToasts(); const isExistingRule = !isNotFoundError(error);