@lark-apaas/client-toolkit 1.2.48 → 1.2.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import "react";
3
+ import { render, screen } from "@testing-library/react";
4
+ import { describe, expect, it, vi } from "vitest";
5
+ import { slardar } from "@lark-apaas/internal-slardar";
6
+ import { SafetyErrorBoundary } from "../safety-error-boundary.js";
7
+ vi.mock('@lark-apaas/internal-slardar', ()=>({
8
+ slardar: {
9
+ sendLog: vi.fn()
10
+ }
11
+ }));
12
+ describe('SafetyErrorBoundary', ()=>{
13
+ it('renders children when no error', ()=>{
14
+ render(/*#__PURE__*/ jsx(SafetyErrorBoundary, {
15
+ children: /*#__PURE__*/ jsx("div", {
16
+ "data-testid": "child",
17
+ children: "child content"
18
+ })
19
+ }));
20
+ expect(screen.getByTestId('child').textContent).toBe('child content');
21
+ });
22
+ it('renders null fallback when child throws', ()=>{
23
+ const Throwing = ()=>{
24
+ throw new Error('Failed to fetch dynamically imported module: safety-XXX.js');
25
+ };
26
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(()=>{});
27
+ const { container } = render(/*#__PURE__*/ jsx(SafetyErrorBoundary, {
28
+ children: /*#__PURE__*/ jsx(Throwing, {})
29
+ }));
30
+ expect(container.innerHTML).toBe('');
31
+ errSpy.mockRestore();
32
+ });
33
+ it('logs error via slardar when child throws', ()=>{
34
+ const Throwing = ()=>{
35
+ throw new Error('chunk load failed');
36
+ };
37
+ const errSpy = vi.spyOn(console, 'error').mockImplementation(()=>{});
38
+ slardar.sendLog.mockClear();
39
+ render(/*#__PURE__*/ jsx(SafetyErrorBoundary, {
40
+ children: /*#__PURE__*/ jsx(Throwing, {})
41
+ }));
42
+ expect(slardar.sendLog).toHaveBeenCalledTimes(1);
43
+ expect(slardar.sendLog).toHaveBeenCalledWith(expect.objectContaining({
44
+ content: expect.stringContaining('chunk load failed'),
45
+ level: 'error',
46
+ extra: expect.objectContaining({
47
+ stack: expect.any(String),
48
+ componentStack: expect.any(String)
49
+ })
50
+ }));
51
+ errSpy.mockRestore();
52
+ });
53
+ });
@@ -7,6 +7,7 @@ import IframeBridge from "./IframeBridge.js";
7
7
  import { Toaster } from "./sonner.js";
8
8
  import { PageHoc } from "./PageHoc.js";
9
9
  import { reportTeaEvent } from "./utils/tea.js";
10
+ import { SafetyErrorBoundary } from "./safety-error-boundary.js";
10
11
  import { useAppInfo } from "../../hooks/index.js";
11
12
  import { TrackKey } from "../../types/tea.js";
12
13
  import { slardar } from "@lark-apaas/internal-slardar";
@@ -196,9 +197,11 @@ const AppContainer_AppContainer = (props)=>{
196
197
  };
197
198
  return /*#__PURE__*/ jsxs(Fragment, {
198
199
  children: [
199
- /*#__PURE__*/ jsx(Suspense, {
200
- fallback: null,
201
- children: /*#__PURE__*/ jsx(Safety, {})
200
+ /*#__PURE__*/ jsx(SafetyErrorBoundary, {
201
+ children: /*#__PURE__*/ jsx(Suspense, {
202
+ fallback: null,
203
+ children: /*#__PURE__*/ jsx(Safety, {})
204
+ })
202
205
  }),
203
206
  /*#__PURE__*/ jsx(QueryProvider, {
204
207
  children: /*#__PURE__*/ jsx(ConfigProvider, {
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ interface Props {
3
+ children: React.ReactNode;
4
+ }
5
+ interface State {
6
+ hasError: boolean;
7
+ }
8
+ /**
9
+ * SafetyErrorBoundary - 包裹 Safety 徽章的 lazy import 失败兜底
10
+ *
11
+ * Why: AppContainer 内 `<Suspense fallback={null}><Safety /></Suspense>` 中 Safety 是 React.lazy
12
+ * 拆出的非核心徽章 chunk。chunk 加载失败(CDN 缓存窗 / 部署漂移 / dev preview hash 漂移 / 网络抖动)
13
+ * 时 rejected Promise 越过 Suspense 上抛,会冒到 React root 让整应用 unmount = 白屏。
14
+ *
15
+ * ErrorBoundary 把失败局限在徽章自身:徽章不显示,应用继续跑。同时上报 slardar 便于事后归因。
16
+ */
17
+ export declare class SafetyErrorBoundary extends React.Component<Props, State> {
18
+ state: State;
19
+ static getDerivedStateFromError(): State;
20
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
21
+ render(): React.ReactNode;
22
+ }
23
+ export {};
@@ -0,0 +1,29 @@
1
+ import react from "react";
2
+ import { slardar } from "@lark-apaas/internal-slardar";
3
+ class SafetyErrorBoundary extends react.Component {
4
+ state = {
5
+ hasError: false
6
+ };
7
+ static getDerivedStateFromError() {
8
+ return {
9
+ hasError: true
10
+ };
11
+ }
12
+ componentDidCatch(error, errorInfo) {
13
+ try {
14
+ slardar.sendLog({
15
+ content: `safety_chunk_load_error: ${error.message}`,
16
+ level: 'error',
17
+ extra: {
18
+ stack: error.stack ?? '',
19
+ componentStack: errorInfo.componentStack ?? ''
20
+ }
21
+ });
22
+ } catch {}
23
+ }
24
+ render() {
25
+ if (this.state.hasError) return null;
26
+ return this.props.children;
27
+ }
28
+ }
29
+ export { SafetyErrorBoundary };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.48",
3
+ "version": "1.2.50",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [