Skip to content

Commit b76729b

Browse files
committed
Do v0
1 parent 35456e2 commit b76729b

9 files changed

+304
-0
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
max_line_length = 100
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
15+
[*.{json,yaml,yml}]
16+
indent_style = space
17+
indent_size = 2

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# IDE's
2+
.vscode
3+
4+
# Project
5+
node_modules
6+
dist
7+
.npmrc
8+
9+
.DS_Store

.prettierrc.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
arrowParens: avoid
2+
useTabs: true
3+
tabWidth: 4
4+
singleQuote: true
5+
semi: false
6+
printWidth: 100
7+
trailingComma: all
8+
overrides:
9+
- files:
10+
- '*.md'
11+
- '*.json'
12+
- '*.yaml'
13+
- '*.yml'
14+
options:
15+
useTabs: false
16+
tabWidth: 2
17+
svelteSortOrder: scripts-markup-styles
18+
svelteBracketNewLine: true

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
# svelte-error-boundary
2+
3+
[![NPM version](https://img.shields.io/npm/v/svelte-error-boundary.svg?style=flat)](https://www.npmjs.com/package/svelte-error-boundary) [![NPM downloads](https://img.shields.io/npm/dm/svelte-error-boundary.svg?style=flat)](https://www.npmjs.com/package/svelte-error-boundary) [![Svelte v3](https://img.shields.io/badge/svelte-v3-blueviolet.svg)](https://svelte.dev)
4+
25
Fix error boundary Svelte 3 problem to prevent full app crash
6+
7+
## :cake: Features
8+
9+
Right now Svelte 3 has no built-in error handling in components out of the box. To solve this issue this package was created inspired by [this thread](https://github.com/sveltejs/svelte/issues/3733).
10+
11+
- Simply applies Javascript `try...catch` statement to child components :lock:
12+
- Easy to use (as traditional component)
13+
14+
## Install
15+
16+
```bash
17+
npm i svelte-error-boundary
18+
```
19+
20+
## Usage
21+
22+
```html
23+
<script>
24+
import ErrorBoundary from 'svelte-error-boundary'
25+
26+
...
27+
</script>
28+
29+
<ErrorBoundary name="custom try catch">
30+
<BrokenComponent />
31+
</ErrorBoundary>
32+
```
33+
34+
## API
35+
36+
### Parameters
37+
38+
| Name | Type | Default | Description |
39+
| ----------- | -------- | ----------- | --------------------------- |
40+
| name | string | `undefined` | Custom name for error scope |
41+
| handleError | function | `undefined` | Function to handle error |
42+
43+
Function `handleError(error, name)` can be used to send error logs to server and so on.
44+
45+
Note that this component does not support SSR (`svelte.compile` with option: `generate: 'ssr'`) and does not support hydration (`svelte.compile` with option: `hydratable: true`).
46+
47+
Please use this `ErrorBoundary` component directly over the `BrokenComponent`, otherwise errors may appear outside. You can manually test the `ErrorBoundary` component by inserting `throw new Error('test')` inside your component.
48+
49+
## License
50+
51+
MIT &copy; [Denis Stasyev](https://github.com/denisstasyev)

package-lock.json

+135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "svelte-error-boundary",
3+
"version": "1.0.1",
4+
"description": "Fix error boundary Svelte 3 problem to prevent full app crash",
5+
"main": "dist/index.js",
6+
"module": "dist/index.mjs",
7+
"files": [
8+
"dist/*"
9+
],
10+
"scripts": {
11+
"build": "rollup -c",
12+
"format": "prettier --write 'src/**/*'",
13+
"prepublishOnly": "npm run format & npm run build"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/denisstasyev/svelte-error-boundary.git"
18+
},
19+
"keywords": [
20+
"svelte",
21+
"error",
22+
"boundary"
23+
],
24+
"author": "Denis Stasyev",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/denisstasyev/svelte-error-boundary/issues"
28+
},
29+
"homepage": "https://github.com/denisstasyev/svelte-error-boundary#readme",
30+
"devDependencies": {
31+
"prettier": "latest",
32+
"prettier-plugin-svelte": "latest",
33+
"rollup": "latest",
34+
"rollup-plugin-node-resolve": "latest",
35+
"rollup-plugin-svelte": "latest",
36+
"svelte": "latest"
37+
}
38+
}

rollup.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import svelte from 'rollup-plugin-svelte'
2+
import resolve from 'rollup-plugin-node-resolve'
3+
4+
import pkg from './package.json'
5+
6+
const name = pkg.name
7+
.replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3')
8+
.replace(/^\w/, m => m.toUpperCase())
9+
.replace(/-\w/g, m => m[1].toUpperCase())
10+
11+
export default {
12+
input: 'src/index.js',
13+
output: [
14+
{ file: pkg.module, format: 'es' },
15+
{ file: pkg.main, format: 'umd', name },
16+
],
17+
plugins: [svelte(), resolve()],
18+
}

src/ErrorBoundary.svelte

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<slot />

src/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ErrorBoundary from './ErrorBoundary.svelte'
2+
3+
export default class errorBoundary extends ErrorBoundary {
4+
constructor(config) {
5+
config.props.$$slots.default = config.props.$$slots.default.map(x => (...args) => {
6+
try {
7+
return x(...args)
8+
} catch (error) {
9+
if (config.props.handleError) {
10+
config.props.handleError(error, config.props.name)
11+
}
12+
console.log(`ERROR${config.props.name ? ` (${config.props.name})` : ''}: `, error)
13+
return null
14+
}
15+
})
16+
17+
super(config)
18+
}
19+
}

0 commit comments

Comments
 (0)