Skip to content

Commit 3435e37

Browse files
committed
first commit
0 parents  commit 3435e37

File tree

13,744 files changed

+2633775
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

13,744 files changed

+2633775
-0
lines changed

β€Ž.babelrc.jsβ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { NODE_ENV, BABEL_ENV } = process.env
2+
const cjs = NODE_ENV === 'test' || BABEL_ENV === 'commonjs'
3+
const loose = true
4+
5+
module.exports = {
6+
presets: [
7+
[
8+
'@babel/env',
9+
{
10+
loose,
11+
modules: false,
12+
exclude: ['@babel/plugin-transform-regenerator'],
13+
},
14+
],
15+
'@babel/preset-typescript',
16+
],
17+
plugins: [
18+
[
19+
'const-enum',
20+
{
21+
transform: 'constObject',
22+
},
23+
],
24+
'babel-plugin-transform-async-to-promises',
25+
cjs && ['@babel/transform-modules-commonjs', { loose }],
26+
[
27+
'@babel/transform-runtime',
28+
{
29+
useESModules: !cjs,
30+
version: require('./package.json').dependencies[
31+
'@babel/runtime'
32+
].replace(/^[^0-9]*/, ''),
33+
},
34+
],
35+
].filter(Boolean),
36+
}

β€ŽLICENSEβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2019 liaoliao666
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

β€ŽREADME.mdβ€Ž

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
<div align="center">
2+
<h1>vu-error-boundary</h1>
3+
4+
<p>Simple reusable Vue error boundary component</p>
5+
</div>
6+
7+
## This solution
8+
9+
This component provides a simple and reusable wrapper that you can use to wrap
10+
around your components. Any rendering errors in your components hierarchy can
11+
then be gracefully handled.
12+
13+
14+
## Table of Contents
15+
16+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
17+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
18+
19+
- [Installation](#installation)
20+
- [Usage](#usage)
21+
- [API](#api)
22+
- [`ErrorBoundary` props](#errorboundary-props)
23+
- [`useErrorHandler(error?: Error)`](#useerrorhandlererror-error)
24+
- [Issues](#issues)
25+
- [πŸ› Bugs](#-bugs)
26+
- [πŸ’‘ Feature Requests](#-feature-requests)
27+
- [LICENSE](#license)
28+
29+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
30+
31+
## Installation
32+
33+
This module is distributed via [npm][npm] which is bundled with [node][node] and
34+
should be installed as one of your project's `dependencies`:
35+
36+
37+
```bash
38+
$ npm i vu-error-boundary
39+
# or
40+
$ yarn add vu-error-boundary
41+
```
42+
43+
## Usage
44+
45+
The simplest way to use `<ErrorBoundary>` is to wrap it around any component
46+
that may throw an error. This will handle errors thrown by that component and
47+
its descendants too.
48+
49+
```vue
50+
<script>
51+
import {ErrorBoundary} from 'vu-error-boundary'
52+
</script>
53+
54+
<template>
55+
<ErrorBoundary @reset="reset">
56+
<ComponentThatMayError />
57+
<template #fallback="{ resetErrorBoundary, error }">
58+
<div role="alert">
59+
<p>Something went wrong:</p>
60+
<pre>{{ error.message }}</pre>
61+
<button @click="resetErrorBoundary">Try again</button>
62+
</div>
63+
</template>
64+
</ErrorBoundary>
65+
</template>
66+
```
67+
68+
You can vue to errors (e.g. for logging) by providing an `@error` callback:
69+
70+
```vue
71+
<script>
72+
import {ErrorBoundary} from 'vu-error-boundary'
73+
</script>
74+
75+
<template>
76+
<ErrorBoundary @error="(error: Error, info: {componentStack: string}) => {
77+
// Do something with the error
78+
// E.g. log to an error logging client here
79+
}">
80+
<ComponentThatMayError />
81+
</template>
82+
```
83+
84+
## API
85+
86+
### `ErrorBoundary` props
87+
88+
#### `children`
89+
90+
This is what you want rendered when everything's working fine. If there's an
91+
error that Vue can handle within the children of the `ErrorBoundary`, the
92+
`ErrorBoundary` will catch that and allow you to handle it gracefully.
93+
94+
#### `FallbackComponent`
95+
96+
This is a component you want rendered in the event of an error. As props it will
97+
be passed the `error` and `resetErrorBoundary` (which will reset the error
98+
boundary's state when called, useful for a "try again" button when used in
99+
combination with the `onReset` prop).
100+
101+
This is required if no `fallback` or `fallbackRender` prop is provided.
102+
103+
#### `onError`
104+
105+
This will be called when there's been an error that the `ErrorBoundary` has
106+
handled. It will be called with two arguments: `error`, `info`.
107+
108+
#### `onReset`
109+
110+
This will be called immediately before the `ErrorBoundary` resets it's internal
111+
state (which will result in rendering the `children` again). You should use this
112+
to ensure that re-rendering the children will not result in a repeat of the same
113+
error happening again.
114+
115+
`onReset` will be called with whatever `resetErrorBoundary` is called with.
116+
117+
**Important**: `onReset` will _not_ be called when reset happens from a change
118+
in `resetKeys`. Use `onResetKeysChange` for that.
119+
120+
### `useErrorHandler(error?: Error)`
121+
122+
Vue's `onErrorCaptured` feature is limited in that the boundaries can only
123+
handle errors thrown during Vue's lifecycles. To quote
124+
125+
> Error boundaries do not catch errors for:
126+
>
127+
> - Event handlers
128+
> - Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks)
129+
> - Server side rendering
130+
> - Errors thrown in the error boundary itself (rather than its children)
131+
132+
This means you have to handle those errors yourself, but you probably would like
133+
to reuse the error boundaries you worked hard on creating for those kinds of
134+
errors as well. This is what `useErrorHandler` is for.
135+
136+
There are two ways to use `useErrorHandler`:
137+
138+
1. `const handleError = useErrorHandler()`: call `handleError(theError)`
139+
2. `useErrorHandler(error)`: useful if you are managing the error state yourself
140+
or get it from another hook.
141+
142+
Here's an example:
143+
144+
```vue
145+
<script>
146+
export default {
147+
setup() {
148+
const handleError = useErrorHandler()
149+
150+
function handleSubmit(event) {
151+
event.preventDefault()
152+
const name = event.target.elements.name.value
153+
fetchGreeting(name).then(
154+
newGreeting => setGreeting(newGreeting),
155+
handleError,
156+
)
157+
}
158+
159+
return {
160+
handleSubmit
161+
}
162+
},
163+
}
164+
</script>
165+
166+
<template>
167+
<div v-if="greeting">{{greeting}}</div>
168+
<form v-else @submit="handleSubmit">
169+
<label>Name</label>
170+
<input id="name" />
171+
<button type="submit">get a greeting</button>
172+
</form>
173+
</template>
174+
```
175+
176+
> Note, in case it's not clear what's happening here, you could also write
177+
> `handleSubmit` like this:
178+
179+
```javascript
180+
function handleSubmit(event) {
181+
event.preventDefault()
182+
const name = event.target.elements.name.value
183+
fetchGreeting(name).then(
184+
newGreeting => setGreeting(newGreeting),
185+
error => handleError(error),
186+
)
187+
}
188+
```
189+
190+
Alternatively, let's say you're using a hook that gives you the error:
191+
192+
```vue
193+
<script>
194+
export default {
195+
setup() {
196+
const name = ref('')
197+
const {greeting, error} = useGreeting(name)
198+
useErrorHandler(error)
199+
200+
function handleSubmit(event) {
201+
event.preventDefault()
202+
name.value = event.target.elements.name.value
203+
}
204+
205+
return {
206+
handleSubmit
207+
}
208+
},
209+
}
210+
</script>
211+
212+
<template>
213+
<div v-if="greeting">{{greeting}}</div>
214+
<form v-else @submit="handleSubmit">
215+
<label>Name</label>
216+
<input id="name" />
217+
<button type="submit">get a greeting</button>
218+
</form>
219+
</template>
220+
```
221+
222+
In this case, if the `error` is ever set to a truthy value, then it will be
223+
propagated to the nearest error boundary.
224+
225+
In either case, you could handle those errors like this:
226+
227+
```vue
228+
<template>
229+
<ErrorBoundary FallbackComponent={ErrorFallback}>
230+
<Greeting />
231+
</ErrorBoundary>
232+
</template>
233+
```
234+
235+
And now that'll handle your runtime errors as well as the async errors in the
236+
`fetchGreeting` or `useGreeting` code.
237+
238+
## Issues
239+
240+
_Looking to contribute? Look for the [Good First Issue][good-first-issue]
241+
label._
242+
243+
### πŸ› Bugs
244+
245+
Please file an issue for bugs, missing documentation, or unexpected behavior.
246+
247+
[**See Bugs**][bugs]
248+
249+
### πŸ’‘ Feature Requests
250+
251+
Please file an issue to suggest new features. Vote on feature requests by adding
252+
a πŸ‘. This helps maintainers prioritize what to work on.
253+
254+
[**See Feature Requests**][requests]
255+
256+
## LICENSE
257+
258+
MIT

β€Ždist/vu-error-boundary.development.jsβ€Ž

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždist/vu-error-boundary.development.js.mapβ€Ž

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždist/vu-error-boundary.production.min.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Ždist/vu-error-boundary.production.min.js.mapβ€Ž

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)