Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

2.9 KB
Raw Blame
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery } from 'react-query';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
import { Typography } from '@signozhq/ui/typography';
import { useAppLocation } from 'lib/router/useAppLocation';
import { Redirect } from 'lib/router/Redirect';
import getByErrorType from 'api/errors/getByErrorTypeAndService';
import getById from 'api/errors/getById';
import Spinner from 'components/Spinner';
import ROUTES from 'constants/routes';
import ErrorDetailsContainer from 'container/ErrorDetails';
import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime';

import { urlKey } from './utils';

// eslint-disable-next-line sonarjs/cognitive-complexity
function ErrorDetails(): JSX.Element {
	const { t } = useTranslation(['common']);
	const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
		(state) => state.globalTime,
	);
	const { search } = useAppLocation();
	const params = useMemo(() => new URLSearchParams(search), [search]);

	const groupId = params.get(urlKey.groupId);
	const errorId = params.get(urlKey.errorId);
	const timestamp = params.get(urlKey.timestamp);

	const defaultError = t('something_went_wrong');

	const { data: IdData, status: IdStatus } = useQuery(
		[errorId, timestamp, groupId],
		{
			queryFn: () =>
				getById({
					errorID: errorId || '',
					groupID: groupId || '',
					timestamp: timestamp || '',
				}),
			enabled:
				errorId !== null &&
				groupId !== null &&
				timestamp !== null &&
				errorId.length !== 0 &&
				groupId.length !== 0 &&
				timestamp.length !== 0,
		},
	);

	const { data, status } = useQuery([maxTime, minTime, groupId, errorId], {
		queryFn: () =>
			getByErrorType({
				groupID: groupId || '',
				timestamp: timestamp || '',
			}),
		enabled: !!groupId && IdStatus !== 'success',
	});

	// if errorType and serviceName is null redirecting to the ALL_ERROR page not now
	if (groupId === null || timestamp === null) {
		return <Redirect to={ROUTES.ALL_ERROR} />;
	}

	// when the api is in loading state
	if (status === 'loading' || IdStatus === 'loading') {
		return <Spinner tip="Loading.." />;
	}

	// if any error occurred while loading
	if (status === 'error' || IdStatus === 'error') {
		return <Typography>{data?.error || defaultError}</Typography>;
	}

	const idPayload = data?.payload || IdData?.payload;

	// if API is successfully but there is an error
	if (
		(status === 'success' && data?.statusCode >= 400) ||
		(IdStatus === 'success' && IdData.statusCode >= 400) ||
		idPayload === null ||
		idPayload === undefined
	) {
		return <Typography>{data?.error || defaultError}</Typography>;
	}

	return <ErrorDetailsContainer idPayload={idPayload} />;
}

export interface ErrorDetailsParams {
	errorType: string;
	serviceName: string;
}

export default ErrorDetails;