handle-server-error.ts 484 B

123456789101112131415161718192021222324
  1. import { AxiosError } from 'axios'
  2. import { toast } from 'sonner'
  3. export function handleServerError(error: unknown) {
  4. // eslint-disable-next-line no-console
  5. console.log(error)
  6. let errMsg = 'Something went wrong!'
  7. if (
  8. error &&
  9. typeof error === 'object' &&
  10. 'status' in error &&
  11. Number(error.status) === 204
  12. ) {
  13. errMsg = 'Content not found.'
  14. }
  15. if (error instanceof AxiosError) {
  16. errMsg = error.response?.data.title
  17. }
  18. toast.error(errMsg)
  19. }