@oxyhq/services
Version:
Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀
145 lines (141 loc) • 4.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ErrorBoundary = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
* Error Boundary Component
*
* Catches JavaScript errors anywhere in the child component tree,
* logs those errors, and displays a fallback UI instead of crashing.
*
* Usage:
* <ErrorBoundary>
* <YourComponent />
* </ErrorBoundary>
*/
class ErrorBoundary extends _react.Component {
constructor(props) {
super(props);
this.state = {
hasError: false,
error: null,
errorInfo: null
};
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI
return {
hasError: true,
error
};
}
componentDidCatch(error, errorInfo) {
// Log error to console in development
if (__DEV__) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
// Call optional error handler
if (this.props.onError) {
this.props.onError(error, errorInfo);
}
// Update state with error info
this.setState({
error,
errorInfo
});
}
handleReset = () => {
this.setState({
hasError: false,
error: null,
errorInfo: null
});
};
render() {
if (this.state.hasError) {
// Use custom fallback if provided
if (this.props.fallback) {
return this.props.fallback;
}
// Default fallback UI
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
style: styles.container,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
style: styles.errorContainer,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: styles.errorTitle,
children: "Something went wrong"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: styles.errorMessage,
children: this.state.error?.message || 'An unexpected error occurred'
}), __DEV__ && this.state.errorInfo && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: styles.errorDetails,
children: this.state.errorInfo.componentStack
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
style: styles.resetButton,
onPress: this.handleReset,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: styles.resetButtonText,
children: "Try Again"
})
})]
})
});
}
return this.props.children;
}
}
exports.ErrorBoundary = ErrorBoundary;
const styles = _reactNative.StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: '#fff'
},
errorContainer: {
maxWidth: 400,
width: '100%',
padding: 20,
borderRadius: 8,
backgroundColor: '#f5f5f5',
borderWidth: 1,
borderColor: '#e0e0e0'
},
errorTitle: {
fontSize: 20,
fontWeight: 'bold',
marginBottom: 10,
color: '#d32f2f'
},
errorMessage: {
fontSize: 14,
marginBottom: 15,
color: '#666'
},
errorDetails: {
fontSize: 12,
marginBottom: 15,
color: '#999',
fontFamily: 'monospace'
},
resetButton: {
backgroundColor: '#007AFF',
padding: 12,
borderRadius: 6,
alignItems: 'center'
},
resetButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600'
}
});
var _default = exports.default = ErrorBoundary;
//# sourceMappingURL=ErrorBoundary.js.map