A custom Error
class for creating user-facing error messages.
It supports additional hints to guide users toward resolving issues, while
maintaining full compatibility with standard JavaScript Error
behavior.
Error
classstring
or true
for a generic hint)ErrorOptions
including cause
from
method for wrapping other errors with a new message@simbo/stringify-error
)Install @simbo/user-facing-error
from the npm registry:
npm i [-D] @simbo/user-facing-error
For a complete API reference, see the documentation.
import { UserFacingError } from '@simbo/user-facing-error';
// Creating the simplest user-facing error:
throw new UserFacingError('Operation cancelled');
// Creating a basic user-facing error with a hint:
throw new UserFacingError('Invalid input', 'Use --help for usage information');
// Adding a generic hint:
// (You have to provide a hint when catching this error.)
throw new UserFacingError('Command failed', true);
// Using another error (or anything else) as `cause`:
const cause = new Error('Underlying error');
throw new UserFacingError('Top-level error', {
cause,
hint: 'Retry with valid credentials',
});
// Wrapping another error:
try {
// some failing operation
} catch (err) {
throw UserFacingError.from(
err,
'Operation failed',
'Check your network connection',
);
}