apiErrorHandler.ts 741 B

1234567891011121314151617181920212223242526272829303132
  1. import axios from "axios";
  2. enum HttpError {
  3. Unauthorized = 401,
  4. ServerError = 500,
  5. }
  6. type ErrorHandlerResponse = {
  7. status: HttpError;
  8. message: string;
  9. };
  10. export const errorHandler = (error: unknown): ErrorHandlerResponse => {
  11. if (axios.isAxiosError(error) && error.response) {
  12. const { status, data } = error.response;
  13. console.log(status, data);
  14. if (
  15. status === HttpError.ServerError &&
  16. data?.message.includes("Incorect")
  17. ) {
  18. return {
  19. status: HttpError.Unauthorized,
  20. message: "The email or password was incorrect",
  21. };
  22. }
  23. return { status, message: data };
  24. }
  25. return {
  26. status: HttpError.ServerError,
  27. message: "There was an error, please try again",
  28. };
  29. };