import { captureException } from '@sentry/browser';
import axios from 'axios';

/**
 * Sidebar microloans nav: 404 → `null`; other errors → Sentry + `null` so the
 * nav item can stay hidden without failing the query.
 */
export async function fetchSafeSidebarNav<T>(
  fetcher: () => Promise<T>,
  sentryContext: string,
): Promise<T | null> {
  try {
    return await fetcher();
  } catch (error) {
    if (axios.isAxiosError(error) && error.response?.status === 404) {
      return null;
    }
    captureException(error, {
      tags: { area: 'microloans' },
      extra: { context: sentryContext },
    });
    return null;
  }
}
