@simbo/user-facing-error 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE.md +20 -0
- package/README.md +76 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/user-facing-error.d.ts +36 -0
- package/dist/user-facing-error.js +64 -0
- package/dist/user-facing-error.js.map +1 -0
- package/dist/user-facing-error.types.d.ts +19 -0
- package/dist/user-facing-error.types.js +2 -0
- package/dist/user-facing-error.types.js.map +1 -0
- package/package.json +66 -0
package/CHANGELOG.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Simon Lepel <simbo@simbo.de> (https://simbo.de)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# User Facing Error
|
|
2
|
+
|
|
3
|
+
[📦 **`@simbo/user-facing-error`**](https://npmjs.com/package/@simbo/user-facing-error)
|
|
4
|
+
|
|
5
|
+
A custom `Error` class for creating **user-facing error messages**.
|
|
6
|
+
|
|
7
|
+
It supports additional _hints_ to guide users toward resolving issues, while
|
|
8
|
+
maintaining full compatibility with standard JavaScript `Error` behavior.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Extends the native `Error` class
|
|
13
|
+
- Includes an optional hint (`string` or `true` for a generic hint)
|
|
14
|
+
- Supports
|
|
15
|
+
[standard `ErrorOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#options)
|
|
16
|
+
including `cause`
|
|
17
|
+
- Static `from` method for wrapping other errors with a new message
|
|
18
|
+
- Fully typed TypeScript API
|
|
19
|
+
- One dependency
|
|
20
|
+
([`@simbo/stringify-error`](https://npmjs.com/package/@simbo/stringify-error))
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Install `@simbo/user-facing-error` from the npm registry:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm i [-D] @simbo/user-facing-error
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
For a complete API reference, see the
|
|
33
|
+
[documentation](https://simbo.codes/packages/modules/_simbo_user-facing-error/).
|
|
34
|
+
|
|
35
|
+
### Examples
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { UserFacingError } from '@simbo/user-facing-error';
|
|
39
|
+
|
|
40
|
+
// Creating the simplest user-facing error:
|
|
41
|
+
throw new UserFacingError('Operation cancelled');
|
|
42
|
+
|
|
43
|
+
// Creating a basic user-facing error with a hint:
|
|
44
|
+
throw new UserFacingError('Invalid input', 'Use --help for usage information');
|
|
45
|
+
|
|
46
|
+
// Adding a generic hint:
|
|
47
|
+
// (You have to provide a hint when catching this error.)
|
|
48
|
+
throw new UserFacingError('Command failed', true);
|
|
49
|
+
|
|
50
|
+
// Using another error (or anything else) as `cause`:
|
|
51
|
+
const cause = new Error('Underlying error');
|
|
52
|
+
throw new UserFacingError('Top-level error', {
|
|
53
|
+
cause,
|
|
54
|
+
hint: 'Retry with valid credentials',
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Wrapping another error:
|
|
58
|
+
try {
|
|
59
|
+
// some failing operation
|
|
60
|
+
} catch (err) {
|
|
61
|
+
throw UserFacingError.from(
|
|
62
|
+
err,
|
|
63
|
+
'Operation failed',
|
|
64
|
+
'Check your network connection',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Related Packages
|
|
70
|
+
|
|
71
|
+
- [@simbo/graceful-exit](https://npmjs.com/package/@simbo/graceful-exit)
|
|
72
|
+
- [@simbo/stringify-error](https://npmjs.com/package/@simbo/stringify-error)
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
[MIT © Simon Lepel](http://simbo.mit-license.org/2025/)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Hint, SecondParam } from './user-facing-error.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* An error that will be displayed to the user.
|
|
4
|
+
*
|
|
5
|
+
* Beside the main message, it can include an additional hint message to help
|
|
6
|
+
* the user resolve the issue.
|
|
7
|
+
*
|
|
8
|
+
* The hint option can also be `true` to indicate that a generic hint should be
|
|
9
|
+
* shown. (e.g. refer to `--help`).
|
|
10
|
+
*
|
|
11
|
+
* This class extends the standard Error class and also accepts an ErrorOptions
|
|
12
|
+
* object including the `hint` property beside other standard error properties
|
|
13
|
+
* like `cause`.
|
|
14
|
+
*/
|
|
15
|
+
export declare class UserFacingError extends Error {
|
|
16
|
+
#private;
|
|
17
|
+
readonly name = "UserFacingError";
|
|
18
|
+
readonly hint: Hint;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new UserFacingError instance.
|
|
21
|
+
*
|
|
22
|
+
* @param message - The main error message.
|
|
23
|
+
* @param options - Optional hint or ErrorOptions.
|
|
24
|
+
*/
|
|
25
|
+
constructor(message: string, options?: SecondParam);
|
|
26
|
+
/**
|
|
27
|
+
* Creates a UserFacingError from another error, wrapping it with a new message.
|
|
28
|
+
* It can also include a hint string or boolean.
|
|
29
|
+
*
|
|
30
|
+
* @param error - The original error to wrap.
|
|
31
|
+
* @param message - The new message for the UserFacingError.
|
|
32
|
+
* @param options - Optional hint or ErrorOptions.
|
|
33
|
+
* @returns A new UserFacingError instance.
|
|
34
|
+
*/
|
|
35
|
+
static from(error: unknown, message: string, options?: SecondParam): UserFacingError;
|
|
36
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { stringifyError } from '@simbo/stringify-error';
|
|
2
|
+
/**
|
|
3
|
+
* An error that will be displayed to the user.
|
|
4
|
+
*
|
|
5
|
+
* Beside the main message, it can include an additional hint message to help
|
|
6
|
+
* the user resolve the issue.
|
|
7
|
+
*
|
|
8
|
+
* The hint option can also be `true` to indicate that a generic hint should be
|
|
9
|
+
* shown. (e.g. refer to `--help`).
|
|
10
|
+
*
|
|
11
|
+
* This class extends the standard Error class and also accepts an ErrorOptions
|
|
12
|
+
* object including the `hint` property beside other standard error properties
|
|
13
|
+
* like `cause`.
|
|
14
|
+
*/
|
|
15
|
+
export class UserFacingError extends Error {
|
|
16
|
+
name = 'UserFacingError';
|
|
17
|
+
hint;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new UserFacingError instance.
|
|
20
|
+
*
|
|
21
|
+
* @param message - The main error message.
|
|
22
|
+
* @param options - Optional hint or ErrorOptions.
|
|
23
|
+
*/
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message, typeof options === 'object' ? options : undefined);
|
|
26
|
+
this.hint = this.#getHintValue(options);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a UserFacingError from another error, wrapping it with a new message.
|
|
30
|
+
* It can also include a hint string or boolean.
|
|
31
|
+
*
|
|
32
|
+
* @param error - The original error to wrap.
|
|
33
|
+
* @param message - The new message for the UserFacingError.
|
|
34
|
+
* @param options - Optional hint or ErrorOptions.
|
|
35
|
+
* @returns A new UserFacingError instance.
|
|
36
|
+
*/
|
|
37
|
+
static from(error, message, options) {
|
|
38
|
+
const secondParam = {
|
|
39
|
+
...(typeof options === 'object' && options !== null
|
|
40
|
+
? options
|
|
41
|
+
: {
|
|
42
|
+
hint: options,
|
|
43
|
+
}),
|
|
44
|
+
cause: error,
|
|
45
|
+
};
|
|
46
|
+
return new UserFacingError(`${message} (${stringifyError(error)})`, secondParam);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Extracts the hint value from the options.
|
|
50
|
+
*
|
|
51
|
+
* @param options - The options to extract the hint from.
|
|
52
|
+
* @returns The hint value or undefined.
|
|
53
|
+
*/
|
|
54
|
+
#getHintValue(options) {
|
|
55
|
+
return options === true || typeof options === 'string'
|
|
56
|
+
? options
|
|
57
|
+
: typeof options === 'object' &&
|
|
58
|
+
options !== null &&
|
|
59
|
+
(options.hint === true || typeof options.hint === 'string')
|
|
60
|
+
? options.hint
|
|
61
|
+
: undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=user-facing-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-facing-error.js","sourceRoot":"","sources":["../src/user-facing-error.ts"],"names":[],"mappings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
/**
|
|
2
|
+
* The options for the UserFacingError.
|
|
3
|
+
* It can include a hint string to help the user resolve the issue.
|
|
4
|
+
* The hint option can also be `true` to indicate that a generic hint should be shown.
|
|
5
|
+
* It extends the standard ErrorOptions.
|
|
6
|
+
*/
|
|
7
|
+
export interface Options extends ErrorOptions {
|
|
8
|
+
hint?: string | boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The type for second parameter for UserFacingError.
|
|
12
|
+
* It can be a string, a boolean, or an object with additional options.
|
|
13
|
+
*/
|
|
14
|
+
export type SecondParam = string | boolean | Options;
|
|
15
|
+
/**
|
|
16
|
+
* The type for the value of the hint in UserFacingError.
|
|
17
|
+
* It can be a string, a boolean, or undefined.
|
|
18
|
+
*/
|
|
19
|
+
export type Hint = string | true | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-facing-error.types.js","sourceRoot":"","sources":["../src/user-facing-error.types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@simbo/user-facing-error",
|
|
3
|
+
"description": "A custom Error class for creating user-facing error messages with an optional hint to guide users toward resolving issues.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"error",
|
|
7
|
+
"error-handling",
|
|
8
|
+
"user",
|
|
9
|
+
"user-facing",
|
|
10
|
+
"user-friendly",
|
|
11
|
+
"message",
|
|
12
|
+
"hint",
|
|
13
|
+
"exception",
|
|
14
|
+
"wrap",
|
|
15
|
+
"utility",
|
|
16
|
+
"typescript"
|
|
17
|
+
],
|
|
18
|
+
"author": "Simon Lepel <simbo@simbo.de> (https://simbo.de/)",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"homepage": "http://github.com/simbo/packages/tree/main/packages/user-facing-error",
|
|
21
|
+
"repository": "github:simbo/packages",
|
|
22
|
+
"bugs": "https://github.com/simbo/packages/issues",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"CHANGELOG.md",
|
|
32
|
+
"dist",
|
|
33
|
+
"LICENSE.md",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@simbo/stringify-error": "1.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@ianvs/prettier-plugin-sort-imports": "^4.6.2",
|
|
41
|
+
"@types/node": "^20.19.11",
|
|
42
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
43
|
+
"@vitest/ui": "^3.2.4",
|
|
44
|
+
"eslint": "^9.34.0",
|
|
45
|
+
"jiti": "^2.5.1",
|
|
46
|
+
"prettier": "^3.6.2",
|
|
47
|
+
"prettier-plugin-organize-attributes": "^1.0.0",
|
|
48
|
+
"typescript": "^5.9.2",
|
|
49
|
+
"vitest": "^3.2.4",
|
|
50
|
+
"@simbo/tsconfig": "1.0.3",
|
|
51
|
+
"@simbo/prettier-config": "1.0.3",
|
|
52
|
+
"@simbo/eslint-config": "1.0.2"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsc --project tsconfig.build.json",
|
|
56
|
+
"check:eslint": "eslint --config ./eslint.config.ts --max-warnings 0",
|
|
57
|
+
"check:format": "prettier --check .",
|
|
58
|
+
"check:types": "tsc --noEmit",
|
|
59
|
+
"clean:dist": "rm -rf dist",
|
|
60
|
+
"fix:eslint": "pnpm run check:eslint --fix",
|
|
61
|
+
"fix:format": "prettier --write .",
|
|
62
|
+
"test": "vitest run --coverage",
|
|
63
|
+
"test:ui": "pnpm run test:watch --ui",
|
|
64
|
+
"test:watch": "vitest dev --coverage"
|
|
65
|
+
}
|
|
66
|
+
}
|