├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | - 16 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type AnyPresentableError = { 2 | readonly isPresentable: true; 3 | } & Error; 4 | 5 | export class PresentableError extends Error implements AnyPresentableError { 6 | readonly name: 'PresentableError'; 7 | readonly isPresentable: true; 8 | constructor(message: string | PresentableError, options?: {cause?: unknown}); 9 | } 10 | 11 | /** 12 | Note: If the given value is already a presentable error-like value, it's just passed through. 13 | */ 14 | export function isPresentableError(value: unknown): value is AnyPresentableError; 15 | 16 | export function makePresentableError(error: Error): error is AnyPresentableError; 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const {toString} = Object.prototype; 2 | const isError = value => toString.call(value) === '[object Error]'; 3 | 4 | export class PresentableError extends Error { 5 | constructor(message, {cause} = {}) { 6 | super(); 7 | 8 | if (message instanceof PresentableError) { 9 | return message; // eslint-disable-line no-constructor-return 10 | } 11 | 12 | if (typeof message !== 'string') { 13 | throw new TypeError('Message required.'); 14 | } 15 | 16 | this.name = 'PresentableError'; 17 | this.message = message; 18 | this.cause = cause; 19 | } 20 | 21 | get isPresentable() { 22 | return true; 23 | } 24 | } 25 | 26 | export function isPresentableError(value) { 27 | return value && isError(value) && value.isPresentable === true; 28 | } 29 | 30 | export function makePresentableError(error) { 31 | if (!isError(error)) { 32 | throw new TypeError('The value is not an error.'); 33 | } 34 | 35 | if (isPresentableError(error)) { 36 | return; 37 | } 38 | 39 | if (!Object.isExtensible(error)) { 40 | throw new Error('The error is non-extensible and cannot be edited.'); 41 | } 42 | 43 | Object.defineProperty(error, 'isPresentable', {value: true}); 44 | } 45 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | /// import {expectType} from 'tsd'; 2 | // import presentableError from './index.js'; 3 | 4 | // TODO: Make tests. 5 | 6 | // expectType(); 7 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "presentable-error", 3 | "version": "0.0.1", 4 | "description": "Make presentable errors", 5 | "license": "MIT", 6 | "repository": "sindresorhus/presentable-error", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "engines": { 19 | "node": ">=16" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts" 27 | ], 28 | "keywords": [ 29 | "error", 30 | "presentable" 31 | ], 32 | "devDependencies": { 33 | "ava": "^5.3.1", 34 | "tsd": "^0.28.1", 35 | "xo": "^0.56.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # presentable-error 2 | 3 | > Make presentable errors 4 | 5 | **Work in progress. Request for feedback.** 6 | 7 | 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. 8 | 9 | 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. 10 | 11 | ## Install 12 | 13 | ```sh 14 | npm install presentable-error 15 | ``` 16 | 17 | ## Usage 18 | 19 | See [`index.d.ts`](index.d.ts) for now. 20 | 21 | #### Example in CLI 22 | 23 | ```js 24 | import meow from 'meow'; 25 | 26 | try { 27 | throwableFunction(); 28 | } catch (error) { 29 | if (error.isPresentable) { 30 | console.error(error.message); 31 | process.exit(1); 32 | } 33 | 34 | throw error; 35 | } 36 | ``` 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {PresentableError, isPresentableError, makePresentableError} from './index.js'; 3 | 4 | test('PresentableError', t => { 5 | const error = new PresentableError('x'); 6 | t.true(error.isPresentable); 7 | }); 8 | 9 | test('isPresentableError', t => { 10 | const error = new PresentableError('x'); 11 | t.true(isPresentableError(error)); 12 | t.false(isPresentableError(new Error('x'))); 13 | 14 | const error2 = new Error('x'); 15 | error2.isPresentable = true; 16 | t.true(isPresentableError(error2)); 17 | }); 18 | 19 | test('makePresentableError', t => { 20 | const error = new Error('x'); 21 | makePresentableError(error); 22 | t.true(isPresentableError(error)); 23 | t.true(error.isPresentable); 24 | 25 | t.notThrows(() => { 26 | makePresentableError(Object.seal(new PresentableError('x'))); 27 | }); 28 | }); 29 | --------------------------------------------------------------------------------