More robust error handling & Display errors to user

This commit is contained in:
Koper
2023-08-31 19:11:22 +07:00
parent df078f7bd6
commit 11bd568a6b
7 changed files with 139 additions and 41 deletions

View File

@@ -0,0 +1,31 @@
"use client";
import React, { createContext, useContext, useState } from "react";
interface ErrorContextProps {
error: string;
setError: React.Dispatch<React.SetStateAction<string>>;
}
const ErrorContext = createContext<ErrorContextProps | undefined>(undefined);
export const useError = () => {
const context = useContext(ErrorContext);
if (!context) {
throw new Error("useError must be used within an ErrorProvider");
}
return context;
};
interface ErrorProviderProps {
children: React.ReactNode;
}
export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
const [error, setError] = useState<string>("");
return (
<ErrorContext.Provider value={{ error, setError }}>
{children}
</ErrorContext.Provider>
);
};