Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "caught", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Avoids UnhandledPromiseRejectionWarning and PromiseRejectionHandledWarning", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "bash test.sh" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
220
README.md
@@ -1,3 +0,3 @@ | ||
node tryjson | ||
============ | ||
node caught | ||
=========== | ||
@@ -12,194 +12,19 @@ [![npm install][install-img]][npm-url] | ||
[npm-url]: https://www.npmjs.com/package/tryjson | ||
[github-url]: https://github.com/rsp/node-tryjson | ||
[readme-url]: https://github.com/rsp/node-tryjson#readme | ||
[issues-url]: https://github.com/rsp/node-tryjson/issues | ||
[license-url]: https://github.com/rsp/node-tryjson/blob/master/LICENSE.md | ||
[travis-url]: https://travis-ci.org/rsp/node-tryjson | ||
[travis-img]: https://travis-ci.org/rsp/node-tryjson.svg?branch=master | ||
[snyk-url]: https://snyk.io/test/github/rsp/node-tryjson | ||
[snyk-img]: https://snyk.io/test/github/rsp/node-tryjson/badge.svg | ||
[david-url]: https://david-dm.org/rsp/node-tryjson | ||
[david-img]: https://david-dm.org/rsp/node-tryjson/status.svg | ||
[install-img]: https://nodei.co/npm/tryjson.png?compact=true | ||
[downloads-img]: https://img.shields.io/npm/dt/tryjson.svg | ||
[license-img]: https://img.shields.io/npm/l/tryjson.svg | ||
[stats-url]: http://npm-stat.com/charts.html?package=tryjson | ||
[github-follow-url]: https://github.com/rsp | ||
[github-follow-img]: https://img.shields.io/github/followers/rsp.svg?style=social&label=Follow | ||
[twitter-follow-url]: https://twitter.com/intent/follow?screen_name=pocztarski | ||
[twitter-follow-img]: https://img.shields.io/twitter/follow/pocztarski.svg?style=social&label=Follow | ||
[stackoverflow-url]: https://stackoverflow.com/users/613198/rsp | ||
[stackexchange-url]: https://stackexchange.com/users/303952/rsp | ||
[stackexchange-img]: https://stackexchange.com/users/flair/303952.png | ||
See this answer on Stack Overflow: | ||
TL;DR | ||
----- | ||
### JSON.parse / JSON.stringify | ||
```js | ||
// this can crash your program | ||
// you need try/catch: | ||
object = JSON.parse(string); | ||
* [Should I refrain from handling Promise rejection asynchronously?](https://stackoverflow.com/questions/40920179/should-i-refrain-from-handling-promise-rejection-asynchronously/40921505#40921505) | ||
// this can crash your program | ||
// you need try/catch: | ||
string = tryjson.stringify(object); | ||
``` | ||
### tryjson.parse / tryjson.stringify | ||
```js | ||
// no need for try/catch: | ||
string = tryjson.stringify(object); | ||
// no need for try/catch: | ||
object = tryjson.parse(string); | ||
``` | ||
Why | ||
--- | ||
Not everyone knows that you should **always** run `JSON.parse` inside of a `try/catch` block or otherwise you risk your application crashing on bad input. Most of the examples of using `JSON.parse` posted online never does that. People usually assume that you will get `undefined` on bad or empty input but you don't. | ||
**Remember: Always `try { JSON.parse() }` or use `tryjson.parse()`** | ||
This module works like many people assume that the built-in `JSON` works and can simplify some common code. | ||
People usually write: | ||
```js | ||
object = JSON.parse(string); | ||
``` | ||
when they mean: | ||
```js | ||
try { | ||
object = JSON.parse(string); | ||
} catch (e) { | ||
object = undefined; | ||
} | ||
``` | ||
and now they can write it as: | ||
```js | ||
object = tryjson.parse(string); | ||
``` | ||
or even as: | ||
```js | ||
object = JSON.parse(string); | ||
``` | ||
if you want to locally override `JSON` with: | ||
```js | ||
var JSON = require('tryjson'); | ||
``` | ||
You can even get a different value than the default `undefined` for invalid JSON: | ||
```js | ||
object = JSON.parse(string, {error: 'Invalid JSON'}); | ||
``` | ||
How it works | ||
------------ | ||
This module works like `JSON.parse` (and in fact it uses `JSON.parse`) but instead of throwing exceptions it returns `undefined` on failure (or some other fallback value if provided). This is not always a desired behaviour but sometimes it is. | ||
There is also a `stringify` method that works like `JSON.stringify` but instead of throwing exceptions on circular structures it returns `"null"` (or a JSON representation of some other fallback value if provided) - which, again, may not be what you always want but sometime it is and you can use this module to simplify your code in those cases. | ||
Methods | ||
------- | ||
### `parse(string)` | ||
Returns the result of parsing `string` as JSON or `undefined` if it cannot be parsed. | ||
### `parse(string, fallback)` | ||
Returns the result of parsing `string` as JSON or the value of `fallback` if it cannot be parsed. | ||
### `stringify(value)` | ||
Returns the JSON representation of `value` or the JSON representation of `null` if `value` cannot be represented as JSON (e.g. contains circular references). | ||
### `stringify(value, fallback)` | ||
Returns the JSON representation of `value` or the JSON representation of `fallback` if `value` cannot be represented as JSON (or the JSON representation of `null` if `fallback` cannot be represented as JSON as well). | ||
Rationale | ||
--------- | ||
Why `tryjson.parse` returns `undefined` for invalid JSON by default? Because a valid JSON can never be parsed to `undefined` so you can test it reliably for that value with `value === undefined` to know if it was invalid. You can specify a custom fallback value as a second argument. | ||
Why `tryjson.stringify` returns `"null"` for objects that cannot be serialized by default? Because `"null"` is a valid JSON string so it can always be parsed without errors and is still easy to test for `null` value. Note that this time, getting "null" does not necessarily mean that the object couldn't be serialized because it might have been originally equal to `null` as well. You can specify a custom fallback value as a second argument - it will be stringified to JSON if possible, or the string `"null"` will be returned. It always returns a valid JSON string. | ||
Installation | ||
------------ | ||
Install to use in your Node project, updating the dependencies in package.json: | ||
```sh | ||
npm install tryjson --save | ||
``` | ||
Examples | ||
-------- | ||
Basic usage: | ||
### Parsing | ||
```js | ||
var tryjson = require('tryjson'); | ||
tryjson.parse('{"a":1,"b":2}'); | ||
// returns object: { a: 1, b: 2 } | ||
tryjson.parse('{"a":1,"b":2'); | ||
// returns value: undefined | ||
``` | ||
### Stringification | ||
```js | ||
var tryjson = require('tryjson'); | ||
var x = {a: 1}; | ||
tryjson.stringify(x); | ||
// returns string: '{"a":1}' | ||
x.b = x; | ||
tryjson.stringify(x); | ||
// returns string: 'null' | ||
``` | ||
### Testing returned values | ||
```js | ||
var object = tryjson.parse(string); | ||
if (object === undefined) { | ||
// the string was invalid JSON | ||
} | ||
if (object == null) { | ||
// the string was either invalid JSON | ||
// or "null" | ||
} | ||
if (!object) { | ||
// the string was either invalid JSON,// | ||
// "null", "false" or "0" | ||
} | ||
``` | ||
### Custom fallback values: | ||
```js | ||
tryjson.parse('{"a":1,"b":2}', {err: 'bad json'}); | ||
// returns object: { a: 1, b: 2 } | ||
tryjson.parse('{"a":1,"b":2', {err: 'bad json'}); | ||
// returns object: { err: 'bad json' } | ||
var x = {a: 1}; | ||
tryjson.stringify(x, {err: 'bad object'}); | ||
// returns string: '{"a":1}' | ||
x.b = x; | ||
tryjson.stringify(x, {err: 'bad object'}); | ||
// returns string: '{"err":"bad object"}' | ||
// invalid object and invalid fallback: | ||
tryjson.stringify(x, x); | ||
// returns string: 'null' | ||
``` | ||
Issues | ||
------ | ||
For any bug reports or feature requests please | ||
[post an issue on GitHub](https://github.com/rsp/node-tryjson/issues). | ||
[post an issue on GitHub][issues-url]. | ||
Author | ||
------ | ||
Rafał Pocztarski - [https://github.com/rsp](https://github.com/rsp) | ||
[**Rafał Pocztarski**](https://pocztarski.com/) | ||
<br/> | ||
[![Follow on GitHub][github-follow-img]][github-follow-url] | ||
[![Follow on Twitter][twitter-follow-img]][twitter-follow-url] | ||
<br/> | ||
[![Follow on Stack Exchange][stackexchange-img]][stackoverflow-url] | ||
@@ -209,1 +34,24 @@ License | ||
MIT License (Expat). See [LICENSE.md](LICENSE.md) for details. | ||
[npm-url]: https://www.npmjs.com/package/caught | ||
[github-url]: https://github.com/rsp/node-caught | ||
[readme-url]: https://github.com/rsp/node-caught#readme | ||
[issues-url]: https://github.com/rsp/node-caught/issues | ||
[license-url]: https://github.com/rsp/node-caught/blob/master/LICENSE.md | ||
[travis-url]: https://travis-ci.org/rsp/node-caught | ||
[travis-img]: https://travis-ci.org/rsp/node-caught.svg?branch=master | ||
[snyk-url]: https://snyk.io/test/github/rsp/node-caught | ||
[snyk-img]: https://snyk.io/test/github/rsp/node-caught/badge.svg | ||
[david-url]: https://david-dm.org/rsp/node-caught | ||
[david-img]: https://david-dm.org/rsp/node-caught/status.svg | ||
[install-img]: https://nodei.co/npm/caught.png?compact=true | ||
[downloads-img]: https://img.shields.io/npm/dt/caught.svg | ||
[license-img]: https://img.shields.io/npm/l/caught.svg | ||
[stats-url]: http://npm-stat.com/charts.html?package=caught | ||
[github-follow-url]: https://github.com/rsp | ||
[github-follow-img]: https://img.shields.io/github/followers/rsp.svg?style=social&label=Follow | ||
[twitter-follow-url]: https://twitter.com/intent/follow?screen_name=pocztarski | ||
[twitter-follow-img]: https://img.shields.io/twitter/follow/pocztarski.svg?style=social&label=Follow | ||
[stackoverflow-url]: https://stackoverflow.com/users/613198/rsp | ||
[stackexchange-url]: https://stackexchange.com/users/303952/rsp | ||
[stackexchange-img]: https://stackexchange.com/users/flair/303952.png |
10
test.js
@@ -0,10 +1,8 @@ | ||
'use strict'; | ||
var caught = require('.'); | ||
var prm = caught(Promise.reject(new Error('fail'))); | ||
var p = caught(Promise.reject(0)); | ||
setTimeout(() => { | ||
prm.catch((err) => { | ||
console.log(err.message); | ||
}) | ||
}, 0); | ||
setTimeout(() => p.catch(e => console.error('caught')), 0); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
8
1
4532
6
56