logging, error boundry

This commit is contained in:
Dobromir Popov
2024-06-25 17:54:30 +03:00
parent 8279514e8a
commit 1936a9cb78
6 changed files with 83 additions and 48 deletions

View File

@ -1,10 +1,14 @@
import React from 'react';
const logger = require('../src/logger');
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
this.logger = null;
if (typeof window === 'undefined') {
this.logger = require('../src/logger');
}
}
static getDerivedStateFromError(error) {
@ -13,14 +17,16 @@ class ErrorBoundary extends React.Component {
}
componentDidCatch(error, info) {
// You can also log the error to an error reporting service
// Log the error to an error reporting service
console.error(error, info);
logger.error(error, info);
if (this.logger) {
this.logger.error(`${error}: ${info.componentStack}`);
}
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
// Render any custom fallback UI
return <h1>Нещо се обърка при изтриването. Моля, опитай отново и се свържете с нас ако проблема продължи. </h1>;
}
@ -28,4 +34,4 @@ class ErrorBoundary extends React.Component {
}
}
export default ErrorBoundary;
export default ErrorBoundary;