initial commit
This commit is contained in:
16
node_modules/presentable-error/index.d.ts
generated
vendored
Normal file
16
node_modules/presentable-error/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
export type AnyPresentableError = {
|
||||
readonly isPresentable: true;
|
||||
} & Error;
|
||||
|
||||
export class PresentableError extends Error implements AnyPresentableError {
|
||||
readonly name: 'PresentableError';
|
||||
readonly isPresentable: true;
|
||||
constructor(message: string | PresentableError, options?: {cause?: unknown});
|
||||
}
|
||||
|
||||
/**
|
||||
Note: If the given value is already a presentable error-like value, it's just passed through.
|
||||
*/
|
||||
export function isPresentableError(value: unknown): value is AnyPresentableError;
|
||||
|
||||
export function makePresentableError(error: Error): error is AnyPresentableError;
|
||||
44
node_modules/presentable-error/index.js
generated
vendored
Normal file
44
node_modules/presentable-error/index.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
const {toString} = Object.prototype;
|
||||
const isError = value => toString.call(value) === '[object Error]';
|
||||
|
||||
export class PresentableError extends Error {
|
||||
constructor(message, {cause} = {}) {
|
||||
super();
|
||||
|
||||
if (message instanceof PresentableError) {
|
||||
return message; // eslint-disable-line no-constructor-return
|
||||
}
|
||||
|
||||
if (typeof message !== 'string') {
|
||||
throw new TypeError('Message required.');
|
||||
}
|
||||
|
||||
this.name = 'PresentableError';
|
||||
this.message = message;
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
get isPresentable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export function isPresentableError(value) {
|
||||
return value && isError(value) && value.isPresentable === true;
|
||||
}
|
||||
|
||||
export function makePresentableError(error) {
|
||||
if (!isError(error)) {
|
||||
throw new TypeError('The value is not an error.');
|
||||
}
|
||||
|
||||
if (isPresentableError(error)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Object.isExtensible(error)) {
|
||||
throw new Error('The error is non-extensible and cannot be edited.');
|
||||
}
|
||||
|
||||
Object.defineProperty(error, 'isPresentable', {value: true});
|
||||
}
|
||||
9
node_modules/presentable-error/license
generated
vendored
Normal file
9
node_modules/presentable-error/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
37
node_modules/presentable-error/package.json
generated
vendored
Normal file
37
node_modules/presentable-error/package.json
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "presentable-error",
|
||||
"version": "0.0.1",
|
||||
"description": "Make presentable errors",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/presentable-error",
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"types": "./index.d.ts",
|
||||
"default": "./index.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"error",
|
||||
"presentable"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "^5.3.1",
|
||||
"tsd": "^0.28.1",
|
||||
"xo": "^0.56.0"
|
||||
}
|
||||
}
|
||||
36
node_modules/presentable-error/readme.md
generated
vendored
Normal file
36
node_modules/presentable-error/readme.md
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
# presentable-error
|
||||
|
||||
> Make presentable errors
|
||||
|
||||
**Work in progress. Request for feedback.**
|
||||
|
||||
The idea is to create a convention for errors that are meant to be presented to the user without a stack trace. Same as any object with a `.then` property can be awaited (duck-typing), I would like to create a convention where every error with a `.isPresentable` property should be presented to the user in a nicer way. This is especially useful for command-line tools. For example, if a command-line tool uses packages that follow the presentable error convention, the command-line tool could simply check for `error.isPresentable` and then log it nicely instead of throwing such errors.
|
||||
|
||||
This package comes with types for creating presentable errors and checking for them, but if you follow the convention, you don't even need to use this package directly. This can be useful if you want to use your own error subclasses. Then you can simply add the `.isPresentable` property. Ensure it's non-writable and non-configurable.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install presentable-error
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
See [`index.d.ts`](index.d.ts) for now.
|
||||
|
||||
#### Example in CLI
|
||||
|
||||
```js
|
||||
import meow from 'meow';
|
||||
|
||||
try {
|
||||
throwableFunction();
|
||||
} catch (error) {
|
||||
if (error.isPresentable) {
|
||||
console.error(error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user