@xyo-network/react-error 3.0.6 → 3.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,5 @@
1
1
  import type { ModuleError } from '@xyo-network/payload-model';
2
- import type { ErrorInfo, ReactNode } from 'react';
3
- import { Component } from 'react';
2
+ import type { FC, ReactNode } from 'react';
4
3
  import type Rollbar from 'rollbar';
5
4
  export interface ThrownErrorBoundaryProps {
6
5
  boundaryName?: string;
@@ -14,11 +13,5 @@ export interface ThrownErrorBoundaryProps {
14
13
  export interface ThrownErrorBoundaryState {
15
14
  xyoError?: ModuleError;
16
15
  }
17
- export declare class ThrownErrorBoundary extends Component<ThrownErrorBoundaryProps, ThrownErrorBoundaryState> {
18
- state: ThrownErrorBoundaryState;
19
- static getDerivedStateFromError(error: Error): ThrownErrorBoundaryState;
20
- static normalizeError(error: Error | ModuleError): ModuleError;
21
- componentDidCatch(error: Error, errorInfo: ErrorInfo): void;
22
- render(): string | number | boolean | import("react/jsx-runtime").JSX.Element | Iterable<ReactNode> | null | undefined;
23
- }
16
+ export declare const ThrownErrorBoundary: FC<ThrownErrorBoundaryProps>;
24
17
  //# sourceMappingURL=ThrownErrorBoundary.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ThrownErrorBoundary.d.ts","sourceRoot":"","sources":["../../../../src/components/ErrorBoundary/ThrownErrorBoundary.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAE7D,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACjD,OAAc,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACxC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAIlC,MAAM,WAAW,wBAAwB;IACvC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,SAAS,CAAA;IACnB,cAAc,CAAC,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,YAAY,CAAC,EAAE,MAAM,KA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ACvB;AA+CD,eAAO,MAAM,mBAAmB,EAAE,EAAE,CAAC,wBAAwB,CAQ5D,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/Provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,KAA8B,MAAM,OAAO,CAAA;AAClD,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAIlC,MAAM,WAAW,0BAA0B;IACzC,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,QAAA,MAAM,qBAAqB,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,0BAA0B,CAAC,CAW7E,CAAA;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAA"}
1
+ {"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/Provider.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAIlC,MAAM,WAAW,0BAA0B;IACzC,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,QAAA,MAAM,qBAAqB,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,0BAA0B,CAAC,CAe7E,CAAA;AAED,OAAO,EAAE,qBAAqB,EAAE,CAAA"}
@@ -1,4 +1,4 @@
1
1
  export * from './Provider.tsx';
2
2
  export * from './State.ts';
3
- export * from './useRollbar.tsx';
3
+ export * from './useErrorReporter.tsx';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,wBAAwB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const useErrorReporter: () => import("./State.ts").ErrorReporterContextState;
2
+ //# sourceMappingURL=useErrorReporter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useErrorReporter.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/useErrorReporter.tsx"],"names":[],"mappings":"AAIA,eAAO,MAAM,gBAAgB,sDAO5B,CAAA"}
@@ -2,32 +2,69 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/components/ErrorBoundary/ThrownErrorBoundary.tsx
5
+ import { useRollbar as useRollbar2 } from "@rollbar/react";
5
6
  import { ModuleErrorSchema } from "@xyo-network/payload-model";
6
- import React3, { Component } from "react";
7
+ import React4, { Component } from "react";
8
+
9
+ // src/contexts/ErrorReporter/Provider.tsx
10
+ import { useRollbar } from "@rollbar/react";
11
+ import React from "react";
12
+
13
+ // src/contexts/ErrorReporter/Context.ts
14
+ import { createContext } from "react";
15
+ var ErrorReporterContext = createContext({});
16
+
17
+ // src/contexts/ErrorReporter/Provider.tsx
18
+ var ErrorReporterProvider = /* @__PURE__ */ __name(({ children, rollbar: rollbarProp }) => {
19
+ let rollbarFromHook;
20
+ try {
21
+ rollbarFromHook = useRollbar();
22
+ } catch {
23
+ }
24
+ const rollbar = rollbarProp ?? rollbarFromHook;
25
+ if (!rollbar) {
26
+ throw new Error("ErrorReporterProvider unable to find a Rollbar instance either passed as prop or from Provider");
27
+ }
28
+ return /* @__PURE__ */ React.createElement(ErrorReporterContext.Provider, {
29
+ value: {
30
+ rollbar
31
+ }
32
+ }, children);
33
+ }, "ErrorReporterProvider");
34
+
35
+ // src/contexts/ErrorReporter/useErrorReporter.tsx
36
+ import { useContext } from "react";
37
+ var useErrorReporter = /* @__PURE__ */ __name(() => {
38
+ const context = useContext(ErrorReporterContext);
39
+ if (context === void 0) {
40
+ console.warn("useErrorReporter must be used within a ErrorReporterContext");
41
+ }
42
+ return context ?? {};
43
+ }, "useErrorReporter");
7
44
 
8
45
  // src/components/ErrorRender/ErrorAlert.tsx
9
46
  import { ExitToApp as ExitIcon } from "@mui/icons-material";
10
47
  import { Alert, AlertTitle, Typography } from "@mui/material";
11
48
  import { ButtonEx } from "@xylabs/react-button";
12
- import React from "react";
49
+ import React2 from "react";
13
50
  var ErrorAlert = /* @__PURE__ */ __name(({ title = "Whoops! Something went wrong", onCancel, error = "An unknown error occurred", errorContext, scope, ...props }) => {
14
51
  const finalScope = scope ?? errorContext;
15
- return /* @__PURE__ */ React.createElement(Alert, {
52
+ return /* @__PURE__ */ React2.createElement(Alert, {
16
53
  severity: "error",
17
54
  ...props
18
- }, /* @__PURE__ */ React.createElement(AlertTitle, null, title), finalScope ? /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Typography, {
55
+ }, /* @__PURE__ */ React2.createElement(AlertTitle, null, title), finalScope ? /* @__PURE__ */ React2.createElement("div", null, /* @__PURE__ */ React2.createElement(Typography, {
19
56
  variant: "caption",
20
57
  mr: 0.5,
21
58
  fontWeight: "bold"
22
- }, "Scope:"), /* @__PURE__ */ React.createElement(Typography, {
59
+ }, "Scope:"), /* @__PURE__ */ React2.createElement(Typography, {
23
60
  variant: "caption"
24
- }, finalScope)) : null, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Typography, {
61
+ }, finalScope)) : null, /* @__PURE__ */ React2.createElement("div", null, /* @__PURE__ */ React2.createElement(Typography, {
25
62
  variant: "caption",
26
63
  mr: 0.5,
27
64
  fontWeight: "bold"
28
- }, "Error:"), /* @__PURE__ */ React.createElement(Typography, {
65
+ }, "Error:"), /* @__PURE__ */ React2.createElement(Typography, {
29
66
  variant: "caption"
30
- }, typeof error === "string" ? error : error?.message)), onCancel ? /* @__PURE__ */ React.createElement(ButtonEx, {
67
+ }, typeof error === "string" ? error : error?.message)), onCancel ? /* @__PURE__ */ React2.createElement(ButtonEx, {
31
68
  variant: "outlined",
32
69
  size: "small",
33
70
  onClick: onCancel,
@@ -36,14 +73,14 @@ var ErrorAlert = /* @__PURE__ */ __name(({ title = "Whoops! Something went wrong
36
73
  right: 8,
37
74
  top: 8
38
75
  }
39
- }, /* @__PURE__ */ React.createElement(ExitIcon, {
76
+ }, /* @__PURE__ */ React2.createElement(ExitIcon, {
40
77
  fontSize: "small"
41
78
  })) : null);
42
79
  }, "ErrorAlert");
43
80
 
44
81
  // src/components/ErrorRender/Render.tsx
45
82
  import { FlexCol } from "@xylabs/react-flexbox";
46
- import React2, { useEffect } from "react";
83
+ import React3, { useEffect } from "react";
47
84
  var ErrorRender = /* @__PURE__ */ __name(({ onCancel, error, noErrorDisplay = false, customError = null, children, errorContext, scope, useLocation, ...props }) => {
48
85
  const location = useLocation?.();
49
86
  useEffect(() => {
@@ -57,24 +94,24 @@ var ErrorRender = /* @__PURE__ */ __name(({ onCancel, error, noErrorDisplay = fa
57
94
  }, [
58
95
  location
59
96
  ]);
60
- return error ? /* @__PURE__ */ React2.createElement(FlexCol, {
97
+ return error ? /* @__PURE__ */ React3.createElement(FlexCol, {
61
98
  alignItems: "stretch",
62
99
  ...props
63
- }, noErrorDisplay ? customError : /* @__PURE__ */ React2.createElement(FlexCol, {
100
+ }, noErrorDisplay ? customError : /* @__PURE__ */ React3.createElement(FlexCol, {
64
101
  alignItems: "center",
65
102
  ...props
66
- }, /* @__PURE__ */ React2.createElement(ErrorAlert, {
103
+ }, /* @__PURE__ */ React3.createElement(ErrorAlert, {
67
104
  error,
68
105
  errorContext,
69
106
  onCancel,
70
107
  scope
71
- }))) : /* @__PURE__ */ React2.createElement(React2.Fragment, null, children) ?? null;
108
+ }))) : /* @__PURE__ */ React3.createElement(React3.Fragment, null, children) ?? null;
72
109
  }, "ErrorRender");
73
110
 
74
111
  // src/components/ErrorBoundary/ThrownErrorBoundary.tsx
75
- var ThrownErrorBoundary = class _ThrownErrorBoundary extends Component {
112
+ var ThrownErrorBoundaryInner = class ThrownErrorBoundaryInner2 extends Component {
76
113
  static {
77
- __name(this, "ThrownErrorBoundary");
114
+ __name(this, "ThrownErrorBoundaryInner");
78
115
  }
79
116
  state = {
80
117
  xyoError: void 0
@@ -82,7 +119,7 @@ var ThrownErrorBoundary = class _ThrownErrorBoundary extends Component {
82
119
  static getDerivedStateFromError(error) {
83
120
  return {
84
121
  hasError: true,
85
- xyoError: _ThrownErrorBoundary.normalizeError(error)
122
+ xyoError: ThrownErrorBoundaryInner2.normalizeError(error)
86
123
  };
87
124
  }
88
125
  static normalizeError(error) {
@@ -108,7 +145,7 @@ var ThrownErrorBoundary = class _ThrownErrorBoundary extends Component {
108
145
  if (errorComponent) {
109
146
  return errorComponent(xyoError);
110
147
  }
111
- return /* @__PURE__ */ React3.createElement(ErrorRender, {
148
+ return /* @__PURE__ */ React4.createElement(ErrorRender, {
112
149
  error: xyoError,
113
150
  errorContext: `${boundaryName} Boundary`,
114
151
  scope,
@@ -118,46 +155,23 @@ var ThrownErrorBoundary = class _ThrownErrorBoundary extends Component {
118
155
  return children;
119
156
  }
120
157
  };
121
-
122
- // src/contexts/ErrorReporter/Provider.tsx
123
- import React4, { useEffect as useEffect2, useState } from "react";
124
-
125
- // src/contexts/ErrorReporter/Context.ts
126
- import { createContext } from "react";
127
- var ErrorReporterContext = createContext({});
128
-
129
- // src/contexts/ErrorReporter/Provider.tsx
130
- var ErrorReporterProvider = /* @__PURE__ */ __name(({ children, rollbar }) => {
131
- const [rollbarInstance, setRollBarInstance] = useState();
132
- useEffect2(() => {
133
- if (rollbarInstance) {
134
- setRollBarInstance(rollbarInstance);
135
- }
136
- }, [
137
- rollbar,
138
- rollbarInstance
139
- ]);
140
- return /* @__PURE__ */ React4.createElement(ErrorReporterContext.Provider, {
141
- value: {
142
- rollbar
143
- }
144
- }, children);
145
- }, "ErrorReporterProvider");
146
-
147
- // src/contexts/ErrorReporter/useRollbar.tsx
148
- import { useContext } from "react";
149
- var useRollbar = /* @__PURE__ */ __name(() => {
150
- const context = useContext(ErrorReporterContext);
151
- if (context === void 0) {
152
- console.warn("useRollbar must be used within a ErrorReporterContext");
158
+ var ThrownErrorBoundary = /* @__PURE__ */ __name(({ rollbar, ...props }) => {
159
+ const { rollbar: rollbarErrorReporter } = useErrorReporter();
160
+ let rollbarFromHook;
161
+ try {
162
+ rollbarFromHook = useRollbar2();
163
+ } catch {
153
164
  }
154
- return context ?? {};
155
- }, "useRollbar");
165
+ return /* @__PURE__ */ React4.createElement(ThrownErrorBoundaryInner, {
166
+ rollbar: rollbar ?? rollbarErrorReporter ?? rollbarFromHook,
167
+ ...props
168
+ });
169
+ }, "ThrownErrorBoundary");
156
170
  export {
157
171
  ErrorAlert,
158
172
  ErrorRender,
159
173
  ErrorReporterProvider,
160
174
  ThrownErrorBoundary,
161
- useRollbar
175
+ useErrorReporter
162
176
  };
163
177
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/components/ErrorBoundary/ThrownErrorBoundary.tsx","../../src/components/ErrorRender/ErrorAlert.tsx","../../src/components/ErrorRender/Render.tsx","../../src/contexts/ErrorReporter/Provider.tsx","../../src/contexts/ErrorReporter/Context.ts","../../src/contexts/ErrorReporter/useRollbar.tsx"],"sourcesContent":["import type { ModuleError } from '@xyo-network/payload-model'\nimport { ModuleErrorSchema } from '@xyo-network/payload-model'\nimport type { ErrorInfo, ReactNode } from 'react'\nimport React, { Component } from 'react'\nimport type Rollbar from 'rollbar'\n\nimport { ErrorRender } from '../ErrorRender/index.ts'\n\nexport interface ThrownErrorBoundaryProps {\n boundaryName?: string\n children: ReactNode\n errorComponent?: (e: ModuleError, boundaryName?: string) => ReactNode\n rethrow?: boolean\n rollbar?: Rollbar\n scope?: string\n title?: string\n}\n\nexport interface ThrownErrorBoundaryState {\n xyoError?: ModuleError\n}\n\nexport class ThrownErrorBoundary extends Component<ThrownErrorBoundaryProps, ThrownErrorBoundaryState> {\n override state: ThrownErrorBoundaryState = { xyoError: undefined }\n\n static getDerivedStateFromError(error: Error) {\n return { hasError: true, xyoError: ThrownErrorBoundary.normalizeError(error) } as ThrownErrorBoundaryState\n }\n\n static normalizeError(error: Error | ModuleError): ModuleError {\n return (\n (error as ModuleError).schema === ModuleErrorSchema\n ? error\n : {\n message: error.message, schema: ModuleErrorSchema, sources: [],\n }) as ModuleError\n }\n\n override componentDidCatch(error: Error, errorInfo: ErrorInfo) {\n const { rethrow, rollbar } = this.props\n const { xyoError } = this.state\n\n rollbar?.error(error)\n\n console.error('Error:', xyoError, errorInfo)\n if (rethrow) {\n throw error\n }\n }\n\n override render() {\n const { xyoError } = this.state\n const {\n children, boundaryName, errorComponent, scope, title,\n } = this.props\n if (xyoError) {\n if (errorComponent) {\n return errorComponent(xyoError)\n }\n return <ErrorRender error={xyoError} errorContext={`${boundaryName} Boundary`} scope={scope} title={title} />\n }\n\n return children\n }\n}\n","import { ExitToApp as ExitIcon } from '@mui/icons-material'\nimport type { AlertProps } from '@mui/material'\nimport {\n Alert, AlertTitle, Typography,\n} from '@mui/material'\nimport { ButtonEx } from '@xylabs/react-button'\nimport type { ModuleError } from '@xyo-network/payload-model'\nimport React from 'react'\n\nexport interface ErrorAlertProps extends AlertProps {\n error?: ModuleError | Error | string\n /** @deprecated use scope instead */\n errorContext?: string\n onCancel?: () => void\n scope?: string\n}\n\nexport const ErrorAlert: React.FC<ErrorAlertProps> = ({\n title = 'Whoops! Something went wrong',\n onCancel,\n error = 'An unknown error occurred',\n errorContext,\n scope,\n ...props\n}) => {\n const finalScope = scope ?? errorContext\n return (\n <Alert severity=\"error\" {...props}>\n <AlertTitle>{title}</AlertTitle>\n {finalScope\n ? (\n <div>\n <Typography variant=\"caption\" mr={0.5} fontWeight=\"bold\">\n Scope:\n </Typography>\n <Typography variant=\"caption\">{finalScope}</Typography>\n </div>\n )\n : null}\n <div>\n <Typography variant=\"caption\" mr={0.5} fontWeight=\"bold\">\n Error:\n </Typography>\n <Typography variant=\"caption\">{typeof error === 'string' ? error : error?.message}</Typography>\n </div>\n {onCancel\n ? (\n <ButtonEx\n variant=\"outlined\"\n size=\"small\"\n onClick={onCancel}\n position=\"absolute\"\n style={{ right: 8, top: 8 }}\n >\n <ExitIcon fontSize=\"small\" />\n </ButtonEx>\n )\n : null}\n </Alert>\n )\n}\n","import { FlexCol } from '@xylabs/react-flexbox'\nimport React, { useEffect } from 'react'\n\nimport { ErrorAlert } from './ErrorAlert.tsx'\nimport type { ErrorRenderProps } from './Props.ts'\n\nexport const ErrorRender: React.FC<ErrorRenderProps> = ({\n onCancel,\n error,\n noErrorDisplay = false,\n customError = null,\n children,\n errorContext,\n scope,\n useLocation,\n ...props\n}) => {\n const location = useLocation?.()\n useEffect(() => {\n if (location) {\n // ensure we end up at the same place we are now after logging in\n location.state = { from: { pathname: window.location.pathname } }\n }\n }, [location])\n\n return error\n ? (\n <FlexCol alignItems=\"stretch\" {...props}>\n {noErrorDisplay\n ? customError\n : (\n <FlexCol alignItems=\"center\" {...props}>\n <ErrorAlert error={error} errorContext={errorContext} onCancel={onCancel} scope={scope} />\n </FlexCol>\n )}\n </FlexCol>\n )\n : (<>{children}</> ?? null)\n}\n","import type { WithChildren } from '@xylabs/react-shared'\nimport React, { useEffect, useState } from 'react'\nimport type Rollbar from 'rollbar'\n\nimport { ErrorReporterContext } from './Context.ts'\n\nexport interface ErrorReporterProviderProps {\n rollbar: Rollbar\n}\n\nconst ErrorReporterProvider: React.FC<WithChildren<ErrorReporterProviderProps>> = ({ children, rollbar }) => {\n const [rollbarInstance, setRollBarInstance] = useState<Rollbar>()\n\n useEffect(() => {\n if (rollbarInstance) {\n setRollBarInstance(rollbarInstance)\n }\n }, [rollbar, rollbarInstance])\n\n // eslint-disable-next-line @eslint-react/no-unstable-context-value\n return <ErrorReporterContext.Provider value={{ rollbar }}>{children}</ErrorReporterContext.Provider>\n}\n\nexport { ErrorReporterProvider }\n","import { createContext } from 'react'\n\nimport type { ErrorReporterContextState } from './State.ts'\n\nexport const ErrorReporterContext = createContext<ErrorReporterContextState>({})\n","import { useContext } from 'react'\n\nimport { ErrorReporterContext } from './Context.ts'\n\nconst useRollbar = () => {\n const context = useContext(ErrorReporterContext)\n if (context === undefined) {\n console.warn('useRollbar must be used within a ErrorReporterContext')\n }\n\n return context ?? {}\n}\n\nexport { useRollbar }\n"],"mappings":";;;;AACA,SAASA,yBAAyB;AAElC,OAAOC,UAASC,iBAAiB;;;ACHjC,SAASC,aAAaC,gBAAgB;AAEtC,SACEC,OAAOC,YAAYC,kBACd;AACP,SAASC,gBAAgB;AAEzB,OAAOC,WAAW;AAUX,IAAMC,aAAwC,wBAAC,EACpDC,QAAQ,gCACRC,UACAC,QAAQ,6BACRC,cACAC,OACA,GAAGC,MAAAA,MACJ;AACC,QAAMC,aAAaF,SAASD;AAC5B,SACE,sBAAA,cAACI,OAAAA;IAAMC,UAAS;IAAS,GAAGH;KAC1B,sBAAA,cAACI,YAAAA,MAAYT,KAAAA,GACZM,aAEK,sBAAA,cAACI,OAAAA,MACC,sBAAA,cAACC,YAAAA;IAAWC,SAAQ;IAAUC,IAAI;IAAKC,YAAW;KAAO,QAAA,GAGzD,sBAAA,cAACH,YAAAA;IAAWC,SAAQ;KAAWN,UAAAA,CAAAA,IAGnC,MACJ,sBAAA,cAACI,OAAAA,MACC,sBAAA,cAACC,YAAAA;IAAWC,SAAQ;IAAUC,IAAI;IAAKC,YAAW;KAAO,QAAA,GAGzD,sBAAA,cAACH,YAAAA;IAAWC,SAAQ;KAAW,OAAOV,UAAU,WAAWA,QAAQA,OAAOa,OAAAA,CAAAA,GAE3Ed,WAEK,sBAAA,cAACe,UAAAA;IACCJ,SAAQ;IACRK,MAAK;IACLC,SAASjB;IACTkB,UAAS;IACTC,OAAO;MAAEC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QZ,MAAM,UAAUH,UAAUW,SAAAA;AAClC,QAAIC,SAAS;AACX,YAAMT;IACR;EACF;EAESa,SAAS;AAChB,UAAM,EAAEhB,SAAQ,IAAK,KAAKD;AAC1B,UAAM,EACJkB,UAAUC,cAAcC,gBAAgBC,OAAOC,MAAK,IAClD,KAAKP;AACT,QAAId,UAAU;AACZ,UAAImB,gBAAgB;AAClB,eAAOA,eAAenB,QAAAA;MACxB;AACA,aAAO,gBAAAsB,OAAA,cAACC,aAAAA;QAAYpB,OAAOH;QAAUwB,cAAc,GAAGN,YAAAA;QAAyBE;QAAcC;;IAC/F;AAEA,WAAOJ;EACT;AACF;;;AG/DA,OAAOQ,UAASC,aAAAA,YAAWC,gBAAgB;;;ACD3C,SAASC,qBAAqB;AAIvB,IAAMC,uBAAuBD,cAAyC,CAAC,CAAA;;;ADM9E,IAAME,wBAA4E,wBAAC,EAAEC,UAAUC,QAAO,MAAE;AACtG,QAAM,CAACC,iBAAiBC,kBAAAA,IAAsBC,SAAAA;AAE9CC,EAAAA,WAAU,MAAA;AACR,QAAIH,iBAAiB;AACnBC,yBAAmBD,eAAAA;IACrB;EACF,GAAG;IAACD;IAASC;GAAgB;AAG7B,SAAO,gBAAAI,OAAA,cAACC,qBAAqBC,UAAQ;IAACC,OAAO;MAAER;IAAQ;KAAID,QAAAA;AAC7D,GAXkF;;;AEVlF,SAASU,kBAAkB;AAI3B,IAAMC,aAAa,6BAAA;AACjB,QAAMC,UAAUC,WAAWC,oBAAAA;AAC3B,MAAIF,YAAYG,QAAW;AACzBC,YAAQC,KAAK,uDAAA;EACf;AAEA,SAAOL,WAAW,CAAC;AACrB,GAPmB;","names":["ModuleErrorSchema","React","Component","ExitToApp","ExitIcon","Alert","AlertTitle","Typography","ButtonEx","React","ErrorAlert","title","onCancel","error","errorContext","scope","props","finalScope","Alert","severity","AlertTitle","div","Typography","variant","mr","fontWeight","message","ButtonEx","size","onClick","position","style","right","top","ExitIcon","fontSize","FlexCol","React","useEffect","ErrorRender","onCancel","error","noErrorDisplay","customError","children","errorContext","scope","useLocation","props","location","useEffect","state","from","pathname","window","React","FlexCol","alignItems","ErrorAlert","ThrownErrorBoundary","Component","state","xyoError","undefined","getDerivedStateFromError","error","hasError","normalizeError","schema","ModuleErrorSchema","message","sources","componentDidCatch","errorInfo","rethrow","rollbar","props","console","render","children","boundaryName","errorComponent","scope","title","React","ErrorRender","errorContext","React","useEffect","useState","createContext","ErrorReporterContext","ErrorReporterProvider","children","rollbar","rollbarInstance","setRollBarInstance","useState","useEffect","React","ErrorReporterContext","Provider","value","useContext","useRollbar","context","useContext","ErrorReporterContext","undefined","console","warn"]}
1
+ {"version":3,"sources":["../../src/components/ErrorBoundary/ThrownErrorBoundary.tsx","../../src/contexts/ErrorReporter/Provider.tsx","../../src/contexts/ErrorReporter/Context.ts","../../src/contexts/ErrorReporter/useErrorReporter.tsx","../../src/components/ErrorRender/ErrorAlert.tsx","../../src/components/ErrorRender/Render.tsx"],"sourcesContent":["import { useRollbar } from '@rollbar/react'\nimport type { ModuleError } from '@xyo-network/payload-model'\nimport { ModuleErrorSchema } from '@xyo-network/payload-model'\nimport type {\n ErrorInfo, FC, ReactNode,\n} from 'react'\nimport React, { Component } from 'react'\nimport type Rollbar from 'rollbar'\n\nimport { useErrorReporter } from '../../contexts/index.ts'\nimport { ErrorRender } from '../ErrorRender/index.ts'\n\nexport interface ThrownErrorBoundaryProps {\n boundaryName?: string\n children: ReactNode\n errorComponent?: (e: ModuleError, boundaryName?: string) => ReactNode\n rethrow?: boolean\n rollbar?: Rollbar\n scope?: string\n title?: string\n}\n\nexport interface ThrownErrorBoundaryState {\n xyoError?: ModuleError\n}\n\nclass ThrownErrorBoundaryInner extends Component<ThrownErrorBoundaryProps, ThrownErrorBoundaryState> {\n override state: ThrownErrorBoundaryState = { xyoError: undefined }\n\n static getDerivedStateFromError(error: Error) {\n return { hasError: true, xyoError: ThrownErrorBoundaryInner.normalizeError(error) } as ThrownErrorBoundaryState\n }\n\n static normalizeError(error: Error | ModuleError): ModuleError {\n return (\n (error as ModuleError).schema === ModuleErrorSchema\n ? error\n : {\n message: error.message, schema: ModuleErrorSchema, sources: [],\n }) as ModuleError\n }\n\n override componentDidCatch(error: Error, errorInfo: ErrorInfo) {\n const { rethrow, rollbar } = this.props\n const { xyoError } = this.state\n\n rollbar?.error(error)\n\n console.error('Error:', xyoError, errorInfo)\n if (rethrow) {\n throw error\n }\n }\n\n override render() {\n const { xyoError } = this.state\n const {\n children, boundaryName, errorComponent, scope, title,\n } = this.props\n if (xyoError) {\n if (errorComponent) {\n return errorComponent(xyoError)\n }\n return <ErrorRender error={xyoError} errorContext={`${boundaryName} Boundary`} scope={scope} title={title} />\n }\n\n return children\n }\n}\n\n// calling the hook outside of the component since only can be called in functional component\nexport const ThrownErrorBoundary: FC<ThrownErrorBoundaryProps> = ({ rollbar, ...props }) => {\n const { rollbar: rollbarErrorReporter } = useErrorReporter()\n let rollbarFromHook: Rollbar | undefined\n // safely call the hook\n try {\n rollbarFromHook = useRollbar()\n } catch {}\n return <ThrownErrorBoundaryInner rollbar={rollbar ?? rollbarErrorReporter ?? rollbarFromHook} {...props} />\n}\n","import { useRollbar } from '@rollbar/react'\nimport type { WithChildren } from '@xylabs/react-shared'\nimport React from 'react'\nimport type Rollbar from 'rollbar'\n\nimport { ErrorReporterContext } from './Context.ts'\n\nexport interface ErrorReporterProviderProps {\n rollbar: Rollbar\n}\n\nconst ErrorReporterProvider: React.FC<WithChildren<ErrorReporterProviderProps>> = ({ children, rollbar: rollbarProp }) => {\n let rollbarFromHook: Rollbar | undefined\n // safely call the hook\n try {\n rollbarFromHook = useRollbar()\n } catch {}\n\n const rollbar = rollbarProp ?? rollbarFromHook\n\n if (!rollbar) {\n throw new Error('ErrorReporterProvider unable to find a Rollbar instance either passed as prop or from Provider')\n }\n\n // eslint-disable-next-line @eslint-react/no-unstable-context-value\n return <ErrorReporterContext.Provider value={{ rollbar }}>{children}</ErrorReporterContext.Provider>\n}\n\nexport { ErrorReporterProvider }\n","import { createContext } from 'react'\n\nimport type { ErrorReporterContextState } from './State.ts'\n\nexport const ErrorReporterContext = createContext<ErrorReporterContextState>({})\n","import { useContext } from 'react'\n\nimport { ErrorReporterContext } from './Context.ts'\n\nexport const useErrorReporter = () => {\n const context = useContext(ErrorReporterContext)\n if (context === undefined) {\n console.warn('useErrorReporter must be used within a ErrorReporterContext')\n }\n\n return context ?? {}\n}\n","import { ExitToApp as ExitIcon } from '@mui/icons-material'\nimport type { AlertProps } from '@mui/material'\nimport {\n Alert, AlertTitle, Typography,\n} from '@mui/material'\nimport { ButtonEx } from '@xylabs/react-button'\nimport type { ModuleError } from '@xyo-network/payload-model'\nimport React from 'react'\n\nexport interface ErrorAlertProps extends AlertProps {\n error?: ModuleError | Error | string\n /** @deprecated use scope instead */\n errorContext?: string\n onCancel?: () => void\n scope?: string\n}\n\nexport const ErrorAlert: React.FC<ErrorAlertProps> = ({\n title = 'Whoops! Something went wrong',\n onCancel,\n error = 'An unknown error occurred',\n errorContext,\n scope,\n ...props\n}) => {\n const finalScope = scope ?? errorContext\n return (\n <Alert severity=\"error\" {...props}>\n <AlertTitle>{title}</AlertTitle>\n {finalScope\n ? (\n <div>\n <Typography variant=\"caption\" mr={0.5} fontWeight=\"bold\">\n Scope:\n </Typography>\n <Typography variant=\"caption\">{finalScope}</Typography>\n </div>\n )\n : null}\n <div>\n <Typography variant=\"caption\" mr={0.5} fontWeight=\"bold\">\n Error:\n </Typography>\n <Typography variant=\"caption\">{typeof error === 'string' ? error : error?.message}</Typography>\n </div>\n {onCancel\n ? (\n <ButtonEx\n variant=\"outlined\"\n size=\"small\"\n onClick={onCancel}\n position=\"absolute\"\n style={{ right: 8, top: 8 }}\n >\n <ExitIcon fontSize=\"small\" />\n </ButtonEx>\n )\n : null}\n </Alert>\n )\n}\n","import { FlexCol } from '@xylabs/react-flexbox'\nimport React, { useEffect } from 'react'\n\nimport { ErrorAlert } from './ErrorAlert.tsx'\nimport type { ErrorRenderProps } from './Props.ts'\n\nexport const ErrorRender: React.FC<ErrorRenderProps> = ({\n onCancel,\n error,\n noErrorDisplay = false,\n customError = null,\n children,\n errorContext,\n scope,\n useLocation,\n ...props\n}) => {\n const location = useLocation?.()\n useEffect(() => {\n if (location) {\n // ensure we end up at the same place we are now after logging in\n location.state = { from: { pathname: window.location.pathname } }\n }\n }, [location])\n\n return error\n ? (\n <FlexCol alignItems=\"stretch\" {...props}>\n {noErrorDisplay\n ? customError\n : (\n <FlexCol alignItems=\"center\" {...props}>\n <ErrorAlert error={error} errorContext={errorContext} onCancel={onCancel} scope={scope} />\n </FlexCol>\n )}\n </FlexCol>\n )\n : (<>{children}</> ?? null)\n}\n"],"mappings":";;;;AAAA,SAASA,cAAAA,mBAAkB;AAE3B,SAASC,yBAAyB;AAIlC,OAAOC,UAASC,iBAAiB;;;ACNjC,SAASC,kBAAkB;AAE3B,OAAOC,WAAW;;;ACFlB,SAASC,qBAAqB;AAIvB,IAAMC,uBAAuBD,cAAyC,CAAC,CAAA;;;ADO9E,IAAME,wBAA4E,wBAAC,EAAEC,UAAUC,SAASC,YAAW,MAAE;AACnH,MAAIC;AAEJ,MAAI;AACFA,sBAAkBC,WAAAA;EACpB,QAAQ;EAAC;AAET,QAAMH,UAAUC,eAAeC;AAE/B,MAAI,CAACF,SAAS;AACZ,UAAM,IAAII,MAAM,gGAAA;EAClB;AAGA,SAAO,sBAAA,cAACC,qBAAqBC,UAAQ;IAACC,OAAO;MAAEP;IAAQ;KAAID,QAAAA;AAC7D,GAfkF;;;AEXlF,SAASS,kBAAkB;AAIpB,IAAMC,mBAAmB,6BAAA;AAC9B,QAAMC,UAAUC,WAAWC,oBAAAA;AAC3B,MAAIF,YAAYG,QAAW;AACzBC,YAAQC,KAAK,6DAAA;EACf;AAEA,SAAOL,WAAW,CAAC;AACrB,GAPgC;;;ACJhC,SAASM,aAAaC,gBAAgB;AAEtC,SACEC,OAAOC,YAAYC,kBACd;AACP,SAASC,gBAAgB;AAEzB,OAA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wBAAC,EAAEZ,SAAS,GAAGC,MAAAA,MAAO;AACrF,QAAM,EAAED,SAASa,qBAAoB,IAAKC,iBAAAA;AAC1C,MAAIC;AAEJ,MAAI;AACFA,sBAAkBC,YAAAA;EACpB,QAAQ;EAAC;AACT,SAAO,gBAAAP,OAAA,cAACzB,0BAAAA;IAAyBgB,SAASA,WAAWa,wBAAwBE;IAAkB,GAAGd;;AACpG,GARiE;","names":["useRollbar","ModuleErrorSchema","React","Component","useRollbar","React","createContext","ErrorReporterContext","ErrorReporterProvider","children","rollbar","rollbarProp","rollbarFromHook","useRollbar","Error","ErrorReporterContext","Provider","value","useContext","useErrorReporter","context","useContext","ErrorReporterContext","undefined","console","warn","ExitToApp","ExitIcon","Alert","AlertTitle","Typography","ButtonEx","React","ErrorAlert","title","onCancel","error","errorContext","scope","props","finalScope","React","Alert","severity","AlertTitle","div","Typography","variant","mr","fontWeight","message","ButtonEx","size","onClick","position","style","right","top","ExitIcon","fontSize","FlexCol","React","useEffect","ErrorRender","onCancel","error","noErrorDisplay","customError","children","errorContext","scope","useLocation","props","location","useEffect","state","from","pathname","window","React","FlexCol","alignItems","ErrorAlert","ThrownErrorBoundaryInner","Component","state","xyoError","undefined","getDerivedStateFromError","error","hasError","normalizeError","schema","ModuleErrorSchema","message","sources","componentDidCatch","errorInfo","rethrow","rollbar","props","console","render","children","boundaryName","errorComponent","scope","title","React","ErrorRender","errorContext","ThrownErrorBoundary","rollbarErrorReporter","useErrorReporter","rollbarFromHook","useRollbar"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/react-error",
3
- "version": "3.0.6",
3
+ "version": "3.0.8",
4
4
  "description": "Common React library for all XYO projects that use React",
5
5
  "keywords": [
6
6
  "xyo",
@@ -43,10 +43,11 @@
43
43
  "lint-pkg": "npmPkgJsonLint ."
44
44
  },
45
45
  "dependencies": {
46
- "@xylabs/react-button": "^4.0.3",
47
- "@xylabs/react-flexbox": "^4.0.3",
48
- "@xylabs/react-shared": "^4.0.3",
49
- "@xyo-network/payload-model": "^3.0.15",
46
+ "@rollbar/react": "^0.12.0-beta",
47
+ "@xylabs/react-button": "^4.1.2",
48
+ "@xylabs/react-flexbox": "^4.1.2",
49
+ "@xylabs/react-shared": "^4.1.2",
50
+ "@xyo-network/payload-model": "^3.1.5",
50
51
  "react-router-dom": "^6.26.1"
51
52
  },
52
53
  "devDependencies": {
@@ -1,4 +1,4 @@
1
- import { Alert } from '@mui/material'
1
+ import { Alert, Button } from '@mui/material'
2
2
  import type { Meta, StoryFn } from '@storybook/react'
3
3
  import React from 'react'
4
4
 
@@ -9,10 +9,26 @@ const StorybookEntry: Meta = {
9
9
  title: 'auth-service/ApiBoundary/ThrownErrorBoundary',
10
10
  }
11
11
 
12
+ const Thrower: React.FC = () => {
13
+ const [shouldThrow, setShouldThrow] = React.useState(false)
14
+ if (shouldThrow) {
15
+ throw new Error('Test Error')
16
+ }
17
+ return (
18
+ <Button onClick={() => {
19
+ setShouldThrow(true)
20
+ }}
21
+ >
22
+ Throw Error
23
+ </Button>
24
+ )
25
+ }
26
+
12
27
  const Template: StoryFn<typeof ThrownErrorBoundary> = ({ errorComponent }) => {
13
28
  return (
14
29
  <ThrownErrorBoundary errorComponent={errorComponent} boundaryName="StoryBook">
15
30
  <Alert severity="info">Use React Dev Tools to trigger and error within the boundary</Alert>
31
+ <Thrower />
16
32
  </ThrownErrorBoundary>
17
33
  )
18
34
  }
@@ -1,9 +1,13 @@
1
+ import { useRollbar } from '@rollbar/react'
1
2
  import type { ModuleError } from '@xyo-network/payload-model'
2
3
  import { ModuleErrorSchema } from '@xyo-network/payload-model'
3
- import type { ErrorInfo, ReactNode } from 'react'
4
+ import type {
5
+ ErrorInfo, FC, ReactNode,
6
+ } from 'react'
4
7
  import React, { Component } from 'react'
5
8
  import type Rollbar from 'rollbar'
6
9
 
10
+ import { useErrorReporter } from '../../contexts/index.ts'
7
11
  import { ErrorRender } from '../ErrorRender/index.ts'
8
12
 
9
13
  export interface ThrownErrorBoundaryProps {
@@ -20,11 +24,11 @@ export interface ThrownErrorBoundaryState {
20
24
  xyoError?: ModuleError
21
25
  }
22
26
 
23
- export class ThrownErrorBoundary extends Component<ThrownErrorBoundaryProps, ThrownErrorBoundaryState> {
27
+ class ThrownErrorBoundaryInner extends Component<ThrownErrorBoundaryProps, ThrownErrorBoundaryState> {
24
28
  override state: ThrownErrorBoundaryState = { xyoError: undefined }
25
29
 
26
30
  static getDerivedStateFromError(error: Error) {
27
- return { hasError: true, xyoError: ThrownErrorBoundary.normalizeError(error) } as ThrownErrorBoundaryState
31
+ return { hasError: true, xyoError: ThrownErrorBoundaryInner.normalizeError(error) } as ThrownErrorBoundaryState
28
32
  }
29
33
 
30
34
  static normalizeError(error: Error | ModuleError): ModuleError {
@@ -63,3 +67,14 @@ export class ThrownErrorBoundary extends Component<ThrownErrorBoundaryProps, Thr
63
67
  return children
64
68
  }
65
69
  }
70
+
71
+ // calling the hook outside of the component since only can be called in functional component
72
+ export const ThrownErrorBoundary: FC<ThrownErrorBoundaryProps> = ({ rollbar, ...props }) => {
73
+ const { rollbar: rollbarErrorReporter } = useErrorReporter()
74
+ let rollbarFromHook: Rollbar | undefined
75
+ // safely call the hook
76
+ try {
77
+ rollbarFromHook = useRollbar()
78
+ } catch {}
79
+ return <ThrownErrorBoundaryInner rollbar={rollbar ?? rollbarErrorReporter ?? rollbarFromHook} {...props} />
80
+ }
@@ -4,7 +4,7 @@ import React from 'react'
4
4
  import Rollbar from 'rollbar'
5
5
 
6
6
  import { ErrorReporterProvider } from './Provider.tsx'
7
- import { useRollbar } from './useRollbar.tsx'
7
+ import { useErrorReporter } from './useErrorReporter.tsx'
8
8
 
9
9
  const StorybookEntry = {
10
10
  argTypes: {},
@@ -14,7 +14,7 @@ const StorybookEntry = {
14
14
  } as Meta<typeof ErrorReporterProvider>
15
15
 
16
16
  const RollbarComponent = () => {
17
- const { rollbar } = useRollbar()
17
+ const { rollbar } = useErrorReporter()
18
18
  const rollbarFound = typeof rollbar?.error === 'function'
19
19
 
20
20
  return (
@@ -1,5 +1,6 @@
1
+ import { useRollbar } from '@rollbar/react'
1
2
  import type { WithChildren } from '@xylabs/react-shared'
2
- import React, { useEffect, useState } from 'react'
3
+ import React from 'react'
3
4
  import type Rollbar from 'rollbar'
4
5
 
5
6
  import { ErrorReporterContext } from './Context.ts'
@@ -8,14 +9,18 @@ export interface ErrorReporterProviderProps {
8
9
  rollbar: Rollbar
9
10
  }
10
11
 
11
- const ErrorReporterProvider: React.FC<WithChildren<ErrorReporterProviderProps>> = ({ children, rollbar }) => {
12
- const [rollbarInstance, setRollBarInstance] = useState<Rollbar>()
12
+ const ErrorReporterProvider: React.FC<WithChildren<ErrorReporterProviderProps>> = ({ children, rollbar: rollbarProp }) => {
13
+ let rollbarFromHook: Rollbar | undefined
14
+ // safely call the hook
15
+ try {
16
+ rollbarFromHook = useRollbar()
17
+ } catch {}
13
18
 
14
- useEffect(() => {
15
- if (rollbarInstance) {
16
- setRollBarInstance(rollbarInstance)
17
- }
18
- }, [rollbar, rollbarInstance])
19
+ const rollbar = rollbarProp ?? rollbarFromHook
20
+
21
+ if (!rollbar) {
22
+ throw new Error('ErrorReporterProvider unable to find a Rollbar instance either passed as prop or from Provider')
23
+ }
19
24
 
20
25
  // eslint-disable-next-line @eslint-react/no-unstable-context-value
21
26
  return <ErrorReporterContext.Provider value={{ rollbar }}>{children}</ErrorReporterContext.Provider>
@@ -1,3 +1,3 @@
1
1
  export * from './Provider.tsx'
2
2
  export * from './State.ts'
3
- export * from './useRollbar.tsx'
3
+ export * from './useErrorReporter.tsx'
@@ -2,13 +2,11 @@ import { useContext } from 'react'
2
2
 
3
3
  import { ErrorReporterContext } from './Context.ts'
4
4
 
5
- const useRollbar = () => {
5
+ export const useErrorReporter = () => {
6
6
  const context = useContext(ErrorReporterContext)
7
7
  if (context === undefined) {
8
- console.warn('useRollbar must be used within a ErrorReporterContext')
8
+ console.warn('useErrorReporter must be used within a ErrorReporterContext')
9
9
  }
10
10
 
11
11
  return context ?? {}
12
12
  }
13
-
14
- export { useRollbar }
@@ -1,3 +0,0 @@
1
- declare const useRollbar: () => import("./State.ts").ErrorReporterContextState;
2
- export { useRollbar };
3
- //# sourceMappingURL=useRollbar.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useRollbar.d.ts","sourceRoot":"","sources":["../../../../src/contexts/ErrorReporter/useRollbar.tsx"],"names":[],"mappings":"AAIA,QAAA,MAAM,UAAU,sDAOf,CAAA;AAED,OAAO,EAAE,UAAU,EAAE,CAAA"}