Skip to content

Commit

Permalink
[Security Solution] Failed to Fetch Rules and Timeline in Preview Fly…
Browse files Browse the repository at this point in the history
…out (elastic#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](elastic#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: elastic#146858

(cherry picked from commit 1b4c661)
  • Loading branch information
e40pud committed Dec 2, 2022
1 parent 399d39e commit 4e197cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4e197cc

Please sign in to comment.