mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
Refactoring to use Error instead of string in the useError hook state variable
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
import React, { createContext, useContext, useState } from "react";
|
||||
|
||||
interface ErrorContextProps {
|
||||
error: string;
|
||||
setError: React.Dispatch<React.SetStateAction<string>>;
|
||||
error: Error | null;
|
||||
setError: React.Dispatch<React.SetStateAction<Error | null>>;
|
||||
}
|
||||
|
||||
const ErrorContext = createContext<ErrorContextProps | undefined>(undefined);
|
||||
@@ -21,7 +21,7 @@ interface ErrorProviderProps {
|
||||
}
|
||||
|
||||
export const ErrorProvider: React.FC<ErrorProviderProps> = ({ children }) => {
|
||||
const [error, setError] = useState<string>("");
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
||||
return (
|
||||
<ErrorContext.Provider value={{ error, setError }}>
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
"use client";
|
||||
import { useError } from "./errorContext";
|
||||
import { useEffect, useState } from "react";
|
||||
import * as Sentry from "@sentry/react";
|
||||
|
||||
const ErrorMessage: React.FC = () => {
|
||||
const { error, setError } = useError();
|
||||
const [isVisible, setIsVisible] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (error) setIsVisible(true);
|
||||
if (error) {
|
||||
setIsVisible(true);
|
||||
Sentry.captureException(error);
|
||||
console.error("Error", error.message, error);
|
||||
}
|
||||
}, [error]);
|
||||
|
||||
if (!isVisible || !error) return null;
|
||||
@@ -16,12 +21,12 @@ const ErrorMessage: React.FC = () => {
|
||||
<div
|
||||
onClick={() => {
|
||||
setIsVisible(false);
|
||||
setError("");
|
||||
setError(null);
|
||||
}}
|
||||
className="max-w-xs z-50 fixed top-16 right-10 bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded transition-opacity duration-300 ease-out opacity-100 hover:opacity-75 cursor-pointer transform hover:scale-105"
|
||||
role="alert"
|
||||
>
|
||||
<span className="block sm:inline">{error}</span>
|
||||
<span className="block sm:inline">{error?.message}</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import * as Sentry from "@sentry/react";
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
|
||||
const handleError = (
|
||||
setError: Dispatch<SetStateAction<String>>,
|
||||
errorString: string,
|
||||
errorObj?: any,
|
||||
) => {
|
||||
setError(errorString);
|
||||
|
||||
if (errorObj) {
|
||||
Sentry.captureException(errorObj);
|
||||
} else {
|
||||
Sentry.captureMessage(errorString);
|
||||
}
|
||||
};
|
||||
|
||||
export default handleError;
|
||||
Reference in New Issue
Block a user