File size: 1,123 Bytes
9853396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { toast } from 'sonner';
import i18n from '@/lib/i18n';

export function handleServerError(error: unknown) {
  let errMsg = i18n.t('common.errors.somethingWentWrong');

  if (error && typeof error === 'object' && 'status' in error && Number(error.status) === 204) {
    errMsg = i18n.t('common.errors.contentNotFound');
  }

  // Handle fetch API errors (Response objects) or objects with data property
  if (error instanceof Response) {
    // For Response objects, we can try to parse the body to get the error message
    error
      .clone()
      .json()
      .then((data) => {
        if (data?.title) {
          toast.error(data.title);
        } else {
          toast.error(errMsg);
        }
      })
      .catch(() => {
        toast.error(errMsg);
      });
    return;
  } else if (error && typeof error === 'object' && 'data' in error && error.data) {
    // For objects with a data property (similar to AxiosError structure)
    const data = error.data as { title?: string };
    if (data.title) {
      errMsg = data.title;
    }
  }

  toast.error(errMsg);
}