@servicetitan/error-boundary 24.1.2 → 24.2.0

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,19 @@
1
+ import { Component, ComponentType, PropsWithChildren } from 'react';
2
+ import { Log } from '@servicetitan/log-service';
3
+ export interface FallbackProps {
4
+ onRefresh?: () => void;
5
+ }
6
+ export type ErrorBoundaryProps = PropsWithChildren<{
7
+ moduleName: string;
8
+ fallback?: ComponentType<FallbackProps>;
9
+ getErrorData?: (error: Error) => Parameters<Log['error']>[0]['data'];
10
+ }>;
11
+ export declare class ErrorBoundary extends Component<ErrorBoundaryProps> {
12
+ state: {
13
+ hasError: boolean;
14
+ };
15
+ private logService?;
16
+ componentDidCatch(error: Error): void;
17
+ render(): string | number | boolean | Iterable<import("react").ReactNode> | import("react/jsx-runtime").JSX.Element | null | undefined;
18
+ }
19
+ //# sourceMappingURL=error-boundary.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-boundary.d.ts","sourceRoot":"","sources":["../src/error-boundary.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAM,iBAAiB,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAGhD,MAAM,WAAW,aAAa;IAC1B,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B;AAED,MAAM,MAAM,kBAAkB,GAAG,iBAAiB,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC;IACxC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;CACxE,CAAC,CAAC;AAEH,qBAAa,aAAc,SAAQ,SAAS,CAAC,kBAAkB,CAAC;IAC5D,KAAK;;MAEH;IAEF,QAEgB,UAAU,CAAC,CAAM;IAEjC,iBAAiB,CAAC,KAAK,EAAE,KAAK;IAU9B,MAAM;CAcT"}
@@ -0,0 +1,52 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ import { Component } from 'react';
12
+ import { Log } from '@servicetitan/log-service';
13
+ import { injectDependency, optional } from '@servicetitan/react-ioc';
14
+ export class ErrorBoundary extends Component {
15
+ constructor() {
16
+ super(...arguments);
17
+ Object.defineProperty(this, "state", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: {
22
+ hasError: false,
23
+ }
24
+ });
25
+ }
26
+ componentDidCatch(error) {
27
+ if (this.logService) {
28
+ const { moduleName: Module, getErrorData } = this.props;
29
+ const data = Object.assign({ Module }, getErrorData === null || getErrorData === void 0 ? void 0 : getErrorData(error));
30
+ this.logService.error({ error, message: error.message, data });
31
+ }
32
+ this.setState({ hasError: true }); // eslint-disable-line react/no-set-state
33
+ }
34
+ render() {
35
+ const { fallback: Fallback = DefaultFallback } = this.props;
36
+ const { hasError } = this.state;
37
+ if (hasError) {
38
+ const handleRefresh = () => {
39
+ this.setState({ hasError: false }); // eslint-disable-line react/no-set-state
40
+ };
41
+ return _jsx(Fallback, { onRefresh: handleRefresh });
42
+ }
43
+ return this.props.children;
44
+ }
45
+ }
46
+ __decorate([
47
+ optional(),
48
+ injectDependency(Log),
49
+ __metadata("design:type", Log)
50
+ ], ErrorBoundary.prototype, "logService", void 0);
51
+ const DefaultFallback = () => _jsx("h2", { children: "Something went wrong" });
52
+ //# sourceMappingURL=error-boundary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-boundary.js","sourceRoot":"","sources":["../src/error-boundary.tsx"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAwC,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAYrE,MAAM,OAAO,aAAc,SAAQ,SAA6B;IAAhE;;QACI;;;;mBAAQ;gBACJ,QAAQ,EAAE,KAAK;aAClB;WAAC;IA8BN,CAAC;IAxBG,iBAAiB,CAAC,KAAY;QAC1B,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;YACxD,MAAM,IAAI,mBAAK,MAAM,IAAK,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAG,KAAK,CAAC,CAAE,CAAC;YAClD,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QACnE,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,yCAAyC;IAChF,CAAC;IAED,MAAM;QACF,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,eAAe,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAE5D,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAEhC,IAAI,QAAQ,EAAE,CAAC;YACX,MAAM,aAAa,GAAG,GAAG,EAAE;gBACvB,IAAI,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,yCAAyC;YACjF,CAAC,CAAC;YACF,OAAO,KAAC,QAAQ,IAAC,SAAS,EAAE,aAAa,GAAI,CAAC;QAClD,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC/B,CAAC;CACJ;AA1BmB;IAFf,QAAQ,EAAE;IACV,gBAAgB,CAAC,GAAG,CAAC;8BACO,GAAG;iDAAC;AA4BrC,MAAM,eAAe,GAAO,GAAG,EAAE,CAAC,gDAA6B,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,18 +1,2 @@
1
- import { Component, ComponentType, PropsWithChildren } from 'react';
2
- export interface FallbackProps {
3
- onRefresh?: () => void;
4
- }
5
- type ErrorBoundaryProps = PropsWithChildren<{
6
- moduleName: string;
7
- fallback?: ComponentType<FallbackProps>;
8
- }>;
9
- export declare class ErrorBoundary extends Component<ErrorBoundaryProps> {
10
- state: {
11
- hasError: boolean;
12
- };
13
- private logService?;
14
- componentDidCatch(error: Error): void;
15
- render(): string | number | boolean | Iterable<import("react").ReactNode> | import("react/jsx-runtime").JSX.Element | null | undefined;
16
- }
17
- export {};
1
+ export * from './error-boundary';
18
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAM,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAMxE,MAAM,WAAW,aAAa;IAC1B,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;CAC1B;AAED,KAAK,kBAAkB,GAAG,iBAAiB,CAAC;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC;CAC3C,CAAC,CAAC;AAEH,qBAAa,aAAc,SAAQ,SAAS,CAAC,kBAAkB,CAAC;IAC5D,KAAK;;MAEH;IAEF,QAEgB,UAAU,CAAC,CAAM;IAEjC,iBAAiB,CAAC,KAAK,EAAE,KAAK;IAU9B,MAAM;CAcT"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -1,54 +1,2 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- import { jsx as _jsx } from "react/jsx-runtime";
11
- /* eslint-disable react/no-set-state */
12
- import { Component } from 'react';
13
- import { injectDependency, optional } from '@servicetitan/react-ioc';
14
- import { Log } from '@servicetitan/log-service';
15
- export class ErrorBoundary extends Component {
16
- constructor() {
17
- super(...arguments);
18
- Object.defineProperty(this, "state", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: {
23
- hasError: false,
24
- }
25
- });
26
- }
27
- componentDidCatch(error) {
28
- var _a;
29
- (_a = this.logService) === null || _a === void 0 ? void 0 : _a.error({
30
- error,
31
- message: error.message,
32
- data: { Module: this.props.moduleName },
33
- });
34
- this.setState({ hasError: true });
35
- }
36
- render() {
37
- const { fallback: Fallback = DefaultFallback } = this.props;
38
- const { hasError } = this.state;
39
- if (hasError) {
40
- const handleRefresh = () => {
41
- this.setState({ hasError: false });
42
- };
43
- return _jsx(Fallback, { onRefresh: handleRefresh });
44
- }
45
- return this.props.children;
46
- }
47
- }
48
- __decorate([
49
- optional(),
50
- injectDependency(Log),
51
- __metadata("design:type", Log)
52
- ], ErrorBoundary.prototype, "logService", void 0);
53
- const DefaultFallback = () => _jsx("h2", { children: "Something went wrong" });
1
+ export * from './error-boundary';
54
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;AAAA,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAwC,MAAM,OAAO,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAErE,OAAO,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAWhD,MAAM,OAAO,aAAc,SAAQ,SAA6B;IAAhE;;QACI;;;;mBAAQ;gBACJ,QAAQ,EAAE,KAAK;aAClB;WAAC;IA8BN,CAAC;IAxBG,iBAAiB,CAAC,KAAY;;QAC1B,MAAA,IAAI,CAAC,UAAU,0CAAE,KAAK,CAAC;YACnB,KAAK;YACL,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;SAC1C,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IAED,MAAM;QACF,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,eAAe,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAE5D,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAEhC,IAAI,QAAQ,EAAE,CAAC;YACX,MAAM,aAAa,GAAG,GAAG,EAAE;gBACvB,IAAI,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;YACvC,CAAC,CAAC;YACF,OAAO,KAAC,QAAQ,IAAC,SAAS,EAAE,aAAa,GAAI,CAAC;QAClD,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC/B,CAAC;CACJ;AA1BmB;IAFf,QAAQ,EAAE;IACV,gBAAgB,CAAC,GAAG,CAAC;8BACO,GAAG;iDAAC;AA4BrC,MAAM,eAAe,GAAO,GAAG,EAAE,CAAC,gDAA6B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/error-boundary",
3
- "version": "24.1.2",
3
+ "version": "24.2.0",
4
4
  "description": "",
5
5
  "homepage": "https://docs.st.dev/docs/frontend/error-boundary",
6
6
  "repository": {
@@ -16,14 +16,17 @@
16
16
  "src"
17
17
  ],
18
18
  "devDependencies": {
19
- "@servicetitan/log-service": "24.1.2",
20
- "@servicetitan/react-ioc": "24.1.2",
19
+ "@servicetitan/log-service": "24.2.0",
20
+ "@servicetitan/react-ioc": "24.2.0",
21
+ "@testing-library/dom": "^10.3.1",
22
+ "@testing-library/jest-dom": "^6.4.5",
23
+ "@testing-library/react": "^16.0.0",
21
24
  "@types/react": "~18.3.3",
22
25
  "react": "~18.3.1"
23
26
  },
24
27
  "peerDependencies": {
25
- "@servicetitan/log-service": "24.1.2",
26
- "@servicetitan/react-ioc": "24.1.2",
28
+ "@servicetitan/log-service": "24.2.0",
29
+ "@servicetitan/react-ioc": "24.2.0",
27
30
  "react": ">=17.0.2"
28
31
  },
29
32
  "publishConfig": {
@@ -32,5 +35,5 @@
32
35
  "cli": {
33
36
  "webpack": false
34
37
  },
35
- "gitHead": "eaeb5a71ea2c40c5311ff6cd0896a504ae0a3502"
38
+ "gitHead": "13848340f521a17e1e251fde0d031022910ca7ab"
36
39
  }
@@ -0,0 +1,122 @@
1
+ import { Log } from '@servicetitan/log-service';
2
+ import { Provider } from '@servicetitan/react-ioc';
3
+ import '@testing-library/jest-dom';
4
+ import { render, screen, waitFor } from '@testing-library/react';
5
+ import { FC, ReactNode } from 'react';
6
+
7
+ import { ErrorBoundary, ErrorBoundaryProps, FallbackProps } from '../error-boundary';
8
+
9
+ describe(`[error-boundary] ${ErrorBoundary.name}`, () => {
10
+ const childContent = 'foo';
11
+
12
+ let props: ErrorBoundaryProps;
13
+ let children: ReactNode;
14
+ let log: Log;
15
+
16
+ const ChildComponent: FC = () => <div>{childContent}</div>;
17
+
18
+ beforeEach(() => {
19
+ children = <ChildComponent />;
20
+ props = { moduleName: 'Foo' };
21
+ log = { error: jest.fn() } as any;
22
+ });
23
+
24
+ const subject = () =>
25
+ render(
26
+ <Provider singletons={[{ provide: Log, useValue: log }]}>
27
+ <ErrorBoundary {...props}>{children}</ErrorBoundary>
28
+ </Provider>
29
+ );
30
+
31
+ test('renders children', () => {
32
+ subject();
33
+
34
+ expect(screen.getByText(childContent)).toBeInTheDocument();
35
+ });
36
+
37
+ describe('when an error occurs', () => {
38
+ let throwError: boolean;
39
+
40
+ const error = new Error('Oops');
41
+
42
+ const ErrorComponent: FC = () => {
43
+ if (throwError) {
44
+ throw error;
45
+ }
46
+ return null;
47
+ };
48
+
49
+ beforeEach(() => {
50
+ jest.spyOn(console, 'error').mockImplementation(jest.fn()); // suppress console errors
51
+ children = <ErrorComponent />;
52
+ throwError = true;
53
+ });
54
+
55
+ test('renders default fallback', () => {
56
+ subject();
57
+
58
+ expect(screen.getByText('Something went wrong')).toBeInTheDocument();
59
+ });
60
+
61
+ test('logs the error', () => {
62
+ subject();
63
+
64
+ expect(log.error).toHaveBeenCalledWith({
65
+ error,
66
+ data: { Module: props.moduleName },
67
+ message: error.message,
68
+ });
69
+ });
70
+
71
+ describe('with a custom fallback', () => {
72
+ let fallbackProps: FallbackProps;
73
+
74
+ const fallbackMessage = 'Custom fallback message';
75
+
76
+ const Fallback: FC<FallbackProps> = props => {
77
+ fallbackProps = props;
78
+ return <div>{fallbackMessage}</div>;
79
+ };
80
+
81
+ beforeEach(() => (props.fallback = Fallback));
82
+
83
+ test('renders custom fallback', () => {
84
+ subject();
85
+
86
+ expect(screen.getByText(fallbackMessage)).toBeInTheDocument();
87
+ });
88
+
89
+ test('onRefresh removes the error', async () => {
90
+ subject();
91
+ await screen.findByText(fallbackMessage);
92
+
93
+ throwError = false;
94
+ fallbackProps.onRefresh!();
95
+
96
+ await waitFor(() => {
97
+ expect(screen.queryByText(fallbackMessage)).not.toBeInTheDocument();
98
+ });
99
+ });
100
+ });
101
+
102
+ describe('with custom error data', () => {
103
+ const when = Date();
104
+
105
+ const customDataCallback: ErrorBoundaryProps['getErrorData'] = (error: Error) => {
106
+ return { when, errorName: error.name };
107
+ };
108
+
109
+ beforeEach(() => (props.getErrorData = customDataCallback));
110
+
111
+ test('adds custom data to the log', () => {
112
+ subject();
113
+
114
+ expect(log.error).toHaveBeenLastCalledWith(
115
+ expect.objectContaining({
116
+ data: { Module: props.moduleName, ...customDataCallback(error) },
117
+ })
118
+ );
119
+ });
120
+ });
121
+ });
122
+ });
@@ -1,17 +1,15 @@
1
- /* eslint-disable react/no-set-state */
2
1
  import { Component, ComponentType, FC, PropsWithChildren } from 'react';
3
-
4
- import { injectDependency, optional } from '@servicetitan/react-ioc';
5
-
6
2
  import { Log } from '@servicetitan/log-service';
3
+ import { injectDependency, optional } from '@servicetitan/react-ioc';
7
4
 
8
5
  export interface FallbackProps {
9
6
  onRefresh?: () => void;
10
7
  }
11
8
 
12
- type ErrorBoundaryProps = PropsWithChildren<{
9
+ export type ErrorBoundaryProps = PropsWithChildren<{
13
10
  moduleName: string;
14
11
  fallback?: ComponentType<FallbackProps>;
12
+ getErrorData?: (error: Error) => Parameters<Log['error']>[0]['data'];
15
13
  }>;
16
14
 
17
15
  export class ErrorBoundary extends Component<ErrorBoundaryProps> {
@@ -24,13 +22,13 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps> {
24
22
  private declare logService?: Log;
25
23
 
26
24
  componentDidCatch(error: Error) {
27
- this.logService?.error({
28
- error,
29
- message: error.message,
30
- data: { Module: this.props.moduleName },
31
- });
25
+ if (this.logService) {
26
+ const { moduleName: Module, getErrorData } = this.props;
27
+ const data = { Module, ...getErrorData?.(error) };
28
+ this.logService.error({ error, message: error.message, data });
29
+ }
32
30
 
33
- this.setState({ hasError: true });
31
+ this.setState({ hasError: true }); // eslint-disable-line react/no-set-state
34
32
  }
35
33
 
36
34
  render() {
@@ -40,7 +38,7 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps> {
40
38
 
41
39
  if (hasError) {
42
40
  const handleRefresh = () => {
43
- this.setState({ hasError: false });
41
+ this.setState({ hasError: false }); // eslint-disable-line react/no-set-state
44
42
  };
45
43
  return <Fallback onRefresh={handleRefresh} />;
46
44
  }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './error-boundary';