| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Managing Errors |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
@packageDocumentation |
| 29 |
@module dome/errors |
| 30 |
*/ |
| 31 |
|
| 32 |
import React from 'react'; |
| 33 |
import { Debug } from 'dome'; |
| 34 |
import { Label } from 'dome/controls/labels'; |
| 35 |
import { Button } from 'dome/controls/buttons'; |
| 36 |
|
| 37 |
const D = new Debug('Dome'); |
| 38 |
|
| 39 |
// -------------------------------------------------------------------------- |
| 40 |
// --- Error Boundaries |
| 41 |
// -------------------------------------------------------------------------- |
| 42 |
|
| 43 |
/** |
| 44 |
Alternative renderer in case of error. |
| 45 |
@param reload - callback for re-rendering the faulty component |
| 46 |
*/ |
| 47 |
export interface ErrorRenderer { |
| 48 |
(error: unknown, info: unknown, reload: () => void): JSX.Element; |
| 49 |
} |
| 50 |
|
| 51 |
export interface CatchProps { |
| 52 |
/** Name of the error boundary. */ |
| 53 |
label?: string; |
| 54 |
/** Alternative renderer callback in case of errors. */ |
| 55 |
onError?: JSX.Element | ErrorRenderer; |
| 56 |
} |
| 57 |
|
| 58 |
interface CatchState { |
| 59 |
error?: unknown; |
| 60 |
info?: unknown; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
React Error Boundaries. |
| 65 |
*/ |
| 66 |
export class Catch extends React.Component<CatchProps, CatchState, unknown> { |
| 67 |
|
| 68 |
constructor(props: CatchProps) { |
| 69 |
super(props); |
| 70 |
this.state = {}; |
| 71 |
this.logerr = this.logerr.bind(this); |
| 72 |
this.reload = this.reload.bind(this); |
| 73 |
} |
| 74 |
|
| 75 |
componentDidCatch(error: unknown, info: unknown): void { |
| 76 |
this.setState({ error, info }); |
| 77 |
} |
| 78 |
|
| 79 |
logerr(): void { |
| 80 |
const { error, info } = this.state; |
| 81 |
D.error('catched error:', error, info); |
| 82 |
} |
| 83 |
|
| 84 |
reload(): void { |
| 85 |
this.setState({ error: undefined, info: undefined }); |
| 86 |
} |
| 87 |
|
| 88 |
render(): JSX.Element { |
| 89 |
const { error, info } = this.state; |
| 90 |
if (error) { |
| 91 |
const { onError, label = 'Error' } = this.props; |
| 92 |
if (typeof (onError) === 'function') |
| 93 |
return onError(error, info, this.reload); |
| 94 |
return ( |
| 95 |
<div> |
| 96 |
<Button |
| 97 |
icon="WARNING" |
| 98 |
kind="warning" |
| 99 |
title={typeof(error) === 'string' ? error : undefined} |
| 100 |
onClick={this.logerr} |
| 101 |
/> |
| 102 |
<Button icon="RELOAD" onClick={this.reload} /> |
| 103 |
<Label>{label}</Label> |
| 104 |
</div> |
| 105 |
); |
| 106 |
} |
| 107 |
return (<>{this.props.children}</>); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// -------------------------------------------------------------------------- |