Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rreusser/fail-nicely
A one-liner to at least explain why things didn't work out
https://github.com/rreusser/fail-nicely
Last synced: 12 days ago
JSON representation
A one-liner to at least explain why things didn't work out
- Host: GitHub
- URL: https://github.com/rreusser/fail-nicely
- Owner: rreusser
- Created: 2016-11-25T19:31:57.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-07T17:05:15.000Z (over 6 years ago)
- Last Synced: 2024-12-07T01:33:10.001Z (20 days ago)
- Language: JavaScript
- Homepage: https://rreusser.github.io/fail-nicely/
- Size: 14.6 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fail-nicely
> A one-liner to at least explain why things didn't work out
## tl;dr
Expect something might throw a fatal error? Instead of
```javascript
function (err, data) {
if (err) { show the error somehow? }
...
}
```just write
```javascript
require('fail-nicely')(function (data) {
...
})
```and get a nice-enough error screen. Great for drive-by error handling.
## Example
See demo: [fail-nicely](https://rreusser.github.io/fail-nicely/)
```bash
npm install fail-nicely
``````javascript
var createHandler = require('fail-nicely');var handleFailure = createHandler(onSuccess, {title: 'Sorry!'});
function onSuccess (message) {
console.log(message);
}// Execute the callback with a failure:
handleFailure('This demo requires WebGL 7 support!');// Execute the onSuccess callback:
handleFailure(null, 'Loaded successfully!');
```You can also just pass it an error:
```javascript
try {
var y = 7 + g;
} catch (e) {
handleFailure(e);
}
```The factory pattern is just a little convoluted, but the upshot is that intercepting fatal errors becomes a simple one-liner:
```javascript
var fs = require('fs')
fs.readFile('test.json', createHandler(function (data) {
console.log('loaded data:', data)
}))
```## Usage
#### `require('fail-nicely')(callback[, options])`
Returns a node-style callback (`function (err, data)`) that intercepts and handlers errors and otherwise forwards data through to `callback`.
When `err` is truthy, appends an overlay to the body element with a short explanation of what happened. Usefulf for making quick and ugly demos a little less ugly. If the first argument is an `Error`, it will use the error's message. If it's a string, that will be the explanation. Options are:
- `bg`: overlay background color (default: `'#333'`)
- `fg`: overlay foreground (text) color (default: `'#fff'`)
- `zIndex`: z-index of the overlay (default: `9999`)
- `title`: heading title (default: `'Sorry!'`)
- `fontFamily`: font family (default: `Helvetica, Arial, sans-serif`)
- `position`: positioning of the overlay element (default: `fixed`)
- `invert`: quick shorthand for swapping the foreground/background colors**Returns**: Nothing.
# License
© 2016 Ricky Reusser. MIT License.