Skip to content

Commit

Permalink
Merge pull request #903 from digma-ai/feature/handle-error-responses
Browse files Browse the repository at this point in the history
Handle error responses in Assets, Issues and Analytics
  • Loading branch information
kshmidt-digma committed Jun 19, 2024
2 parents 8ef9ac6 + 43557fd commit d67534e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 27 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/api/ActionDispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionListener } from "./types";
import { ActionListener, DigmaMessageError } from "./types";

export class ActionDispatcher {
private actions: Record<string, ActionListener[]>;
Expand All @@ -24,9 +24,14 @@ export class ActionDispatcher {
}
}

public dispatch(timeStamp: number, type: string, data?: unknown): void {
public dispatch(
timeStamp: number,
type: string,
data: unknown,
error: DigmaMessageError | undefined
): void {
if (this.actions[type]) {
this.actions[type].forEach((fn) => fn(data, timeStamp));
this.actions[type].forEach((fn) => fn(data, timeStamp, error));
}
}
}
7 changes: 6 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export const initializeDigmaMessageListener = (
null,
e.data
);
dispatcher.dispatch(e.timeStamp, e.data.action, e.data.payload);
dispatcher.dispatch(
e.timeStamp,
e.data.action,
e.data.payload,
e.data.error
);
}
};

Expand Down
11 changes: 10 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export type ActionListener = (data: unknown, timeStamp: number) => void;
export type ActionListener = (
data: unknown,
timeStamp: number,
error: DigmaMessageError | undefined
) => void;

export interface DigmaMessageError {
message: string;
}

export interface DigmaIncomingMessageData {
type: "digma";
action: string;
payload?: unknown;
error?: DigmaMessageError;
}

export interface DigmaOutgoingMessageData<T> {
Expand Down
11 changes: 9 additions & 2 deletions src/components/Assets/AssetList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useState
} from "react";
import { DefaultTheme, useTheme } from "styled-components";
import { DigmaMessageError } from "../../../api/types";
import { dispatcher } from "../../../dispatcher";
import { usePrevious } from "../../../hooks/usePrevious";
import { isEnvironment } from "../../../typeGuards/isEnvironment";
Expand Down Expand Up @@ -231,8 +232,14 @@ export const AssetList = (props: AssetListProps) => {
refreshData();
setIsInitialLoading(true);

const handleAssetsData = (data: unknown, timeStamp: number) => {
setData(data as AssetsData);
const handleAssetsData = (
data: unknown,
timeStamp: number,
error: DigmaMessageError | undefined
) => {
if (!error) {
setData(data as AssetsData);
}
setLastSetDataTimeStamp(timeStamp);
};

Expand Down
11 changes: 9 additions & 2 deletions src/components/Assets/AssetTypeList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import { DigmaMessageError } from "../../../api/types";
import { dispatcher } from "../../../dispatcher";
import { usePrevious } from "../../../hooks/usePrevious";
import { isEnvironment } from "../../../typeGuards/isEnvironment";
Expand Down Expand Up @@ -96,8 +97,14 @@ export const AssetTypeList = (props: AssetTypeListProps) => {
refreshData();
setIsInitialLoading(true);

const handleCategoriesData = (data: unknown, timeStamp: number) => {
setData(data as AssetCategoriesData);
const handleCategoriesData = (
data: unknown,
timeStamp: number,
error: DigmaMessageError | undefined
) => {
if (!error) {
setData(data as AssetCategoriesData);
}
setLastSetDataTimeStamp(timeStamp);
};

Expand Down
19 changes: 13 additions & 6 deletions src/components/Insights/useInsightsData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useContext, useEffect, useMemo, useRef, useState } from "react";
import { actions as globalActions } from "../../actions";
import { DigmaMessageError } from "../../api/types";
import { dispatcher } from "../../dispatcher";
import { usePrevious } from "../../hooks/usePrevious";
import { GetInsightStatsPayload } from "../../types";
Expand Down Expand Up @@ -104,13 +105,19 @@ export const useInsightsData = ({

setIsInitialLoading(true);
setIsLoading(true);
const handleInsightsData = (data: unknown, timeStamp: number) => {
const insightsData = data as InsightsData;
insightsData.insightsStatus = InsightsStatus.DEFAULT;
insightsData.viewMode = ViewMode.INSIGHTS;

const handleInsightsData = (
data: unknown,
timeStamp: number,
error: DigmaMessageError | undefined
) => {
if (!error) {
const insightsData = data as InsightsData;
insightsData.insightsStatus = InsightsStatus.DEFAULT;
insightsData.viewMode = ViewMode.INSIGHTS;

setData(insightsData);
}
setIsLoading(false);
setData(insightsData);
setLastSetDataTimeStamp(timeStamp);
};

Expand Down

0 comments on commit d67534e

Please sign in to comment.