You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SwaggerUI comes with a `safe-render` plugin that handles error handling allows plugging into error handling system and modify it.
240
+
241
+
The plugin accepts a list of component names that should be protected by error boundaries.
242
+
243
+
Its public API looks like this:
244
+
245
+
```js
246
+
{
247
+
fn: {
248
+
componentDidCatch,
249
+
withErrorBoundary:withErrorBoundary(getSystem),
250
+
},
251
+
components: {
252
+
ErrorBoundary,
253
+
Fallback,
254
+
},
255
+
}
256
+
```
257
+
258
+
safe-render plugin is automatically utilized by [base](https://github.com/swagger-api/swagger-ui/blob/78f62c300a6d137e65fd027d850981b010009970/src/core/presets/base.js) and [standalone](https://github.com/swagger-api/swagger-ui/tree/78f62c300a6d137e65fd027d850981b010009970/src/standalone) SwaggerUI presets and
259
+
should always be used as the last plugin, after all the components are already known to the SwaggerUI.
260
+
The plugin defines a default list of components that should be protected by error boundaries:
261
+
262
+
```js
263
+
[
264
+
"App",
265
+
"BaseLayout",
266
+
"VersionPragmaFilter",
267
+
"InfoContainer",
268
+
"ServersContainer",
269
+
"SchemesContainer",
270
+
"AuthorizeBtnContainer",
271
+
"FilterContainer",
272
+
"Operations",
273
+
"OperationContainer",
274
+
"parameters",
275
+
"responses",
276
+
"OperationServers",
277
+
"Models",
278
+
"ModelWrapper",
279
+
"Topbar",
280
+
"StandaloneLayout",
281
+
"onlineValidatorBadge"
282
+
]
283
+
```
284
+
285
+
As demonstrated below, additional components can be protected by utilizing the safe-render plugin
286
+
with configuration options. This gets really handy if you are a SwaggerUI integrator and you maintain a number of
fullOverride:true, // only the component list defined here will apply (not the default list)
301
+
componentList: [
302
+
"MyCustomComponent1",
303
+
],
304
+
}),
305
+
],
306
+
});
307
+
```
308
+
309
+
##### componentDidCatch
310
+
311
+
This static function is invoked after a component has thrown an error.
312
+
It receives two parameters:
313
+
314
+
1.`error` - The error that was thrown.
315
+
2.`info` - An object with a componentStack key containing [information about which component threw the error](https://reactjs.org/docs/error-boundaries.html#component-stack-traces).
316
+
317
+
It has precisely the same signature as error boundaries [componentDidCatch lifecycle method](https://reactjs.org/docs/react-component.html#componentdidcatch),
318
+
except it's a static function and not a class method.
319
+
320
+
Default implement of componentDidCatch uses `console.error` to display the received error:
321
+
322
+
```js
323
+
exportconstcomponentDidCatch=console.error;
324
+
```
325
+
326
+
To utilize your own error handling logic (e.g. [bugsnag](https://www.bugsnag.com/)), create new SwaggerUI plugin that overrides componentDidCatch:
327
+
328
+
{% highlight js linenos %}
329
+
const BugsnagErrorHandlerPlugin = () => {
330
+
// init bugsnag
331
+
332
+
return {
333
+
fn: {
334
+
componentDidCatch = (error, info) => {
335
+
Bugsnag.notify(error);
336
+
Bugsnag.notify(info);
337
+
},
338
+
},
339
+
};
340
+
};
341
+
{% endhighlight %}
342
+
343
+
##### withErrorBoundary
344
+
345
+
This function is HOC (Higher Order Component). It wraps a particular component into the `ErrorBoundary` component.
346
+
It can be overridden via a plugin system to control how components are wrapped by the ErrorBoundary component.
347
+
In 99.9% of situations, you won't need to override this function, but if you do, please read the source code of this function first.
348
+
349
+
##### Fallback
350
+
351
+
The component is displayed when the error boundary catches an error. It can be overridden via a plugin system.
352
+
Its default implementation is trivial:
353
+
354
+
```js
355
+
importReactfrom"react"
356
+
importPropTypesfrom"prop-types"
357
+
358
+
constFallback= ({ name }) => (
359
+
<div className="fallback">
360
+
😱 <i>Could not render { name ==="t"?"this component": name }, see the console.</i>
361
+
</div>
362
+
)
363
+
Fallback.propTypes= {
364
+
name:PropTypes.string.isRequired,
365
+
}
366
+
exportdefaultFallback
367
+
```
368
+
369
+
Feel free to override it to match your look & feel:
370
+
371
+
```js
372
+
constCustomFallbackPlugin= () => ({
373
+
components: {
374
+
Fallback: ({ name } ) =>`This is my custom fallback. ${name} failed to render`,
0 commit comments