@bombillazo/error-x 0.2.2 → 0.3.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/README.md +11 -12
- package/dist/index.cjs +195 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +120 -134
- package/dist/index.d.ts +120 -134
- package/dist/index.js +196 -84
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -596,141 +596,127 @@ declare class ErrorX extends Error {
|
|
|
596
596
|
* ```
|
|
597
597
|
*/
|
|
598
598
|
static fromJSON(serialized: SerializableError): ErrorX;
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* Preset configurations for common errors organized by category.
|
|
603
|
-
*
|
|
604
|
-
* ## Features
|
|
605
|
-
* - **Pre-configured error templates** for common HTTP status codes (400-511)
|
|
606
|
-
* - **Type-safe** with TypeScript support
|
|
607
|
-
* - **Fully customizable** via destructuring and override pattern
|
|
608
|
-
* - **User-friendly messages** included for all presets
|
|
609
|
-
* - **Categorized by type** - all HTTP presets include `type: 'http'`
|
|
610
|
-
*
|
|
611
|
-
* ## Available Categories
|
|
612
|
-
* - **HTTP**: Common HTTP status codes (4xx client errors, 5xx server errors)
|
|
613
|
-
*
|
|
614
|
-
* ## Usage Patterns
|
|
615
|
-
*
|
|
616
|
-
* ### 1. Direct Usage
|
|
617
|
-
* Use a preset as-is without any modifications:
|
|
618
|
-
* ```typescript
|
|
619
|
-
* import { ErrorX, PRESETS } from '@bombillazo/error-x'
|
|
620
|
-
*
|
|
621
|
-
* throw new ErrorX(PRESETS.HTTP.NOT_FOUND)
|
|
622
|
-
* // Result: 404 error with default message and UI message
|
|
623
|
-
* ```
|
|
624
|
-
*
|
|
625
|
-
* ### 2. Override Specific Fields
|
|
626
|
-
* Customize the error while keeping other preset values:
|
|
627
|
-
* ```typescript
|
|
628
|
-
* throw new ErrorX({
|
|
629
|
-
* ...PRESETS.HTTP.NOT_FOUND,
|
|
630
|
-
* message: 'User not found',
|
|
631
|
-
* metadata: { userId: 123 }
|
|
632
|
-
* })
|
|
633
|
-
* // Result: 404 error with custom message but keeps httpStatus, code, name, uiMessage, type
|
|
634
|
-
* ```
|
|
635
|
-
*
|
|
636
|
-
* ### 3. Add Metadata and Actions
|
|
637
|
-
* Enhance presets with additional context and behaviors:
|
|
638
|
-
* ```typescript
|
|
639
|
-
* throw new ErrorX({
|
|
640
|
-
* ...PRESETS.HTTP.UNAUTHORIZED,
|
|
641
|
-
* metadata: { attemptedAction: 'viewProfile', userId: 456 },
|
|
642
|
-
* actions: [
|
|
643
|
-
* { action: 'logout', payload: { clearStorage: true } },
|
|
644
|
-
* { action: 'redirect', payload: { redirectURL: '/login' } }
|
|
645
|
-
* ]
|
|
646
|
-
* })
|
|
647
|
-
* ```
|
|
648
|
-
*
|
|
649
|
-
* ### 4. Add Error Cause
|
|
650
|
-
* Chain errors by adding a cause:
|
|
651
|
-
* ```typescript
|
|
652
|
-
* try {
|
|
653
|
-
* // some operation
|
|
654
|
-
* } catch (originalError) {
|
|
655
|
-
* throw new ErrorX({
|
|
656
|
-
* ...PRESETS.HTTP.INTERNAL_SERVER_ERROR,
|
|
657
|
-
* cause: originalError,
|
|
658
|
-
* metadata: { operation: 'database-query' }
|
|
659
|
-
* })
|
|
660
|
-
* }
|
|
661
|
-
* ```
|
|
662
|
-
*
|
|
663
|
-
* ## Common HTTP Presets
|
|
664
|
-
*
|
|
665
|
-
* ### 4xx Client Errors
|
|
666
|
-
* - `BAD_REQUEST` (400) - Invalid request data
|
|
667
|
-
* - `UNAUTHORIZED` (401) - Authentication required
|
|
668
|
-
* - `FORBIDDEN` (403) - Insufficient permissions
|
|
669
|
-
* - `NOT_FOUND` (404) - Resource not found
|
|
670
|
-
* - `METHOD_NOT_ALLOWED` (405) - HTTP method not allowed
|
|
671
|
-
* - `CONFLICT` (409) - Resource conflict
|
|
672
|
-
* - `UNPROCESSABLE_ENTITY` (422) - Validation failed
|
|
673
|
-
* - `TOO_MANY_REQUESTS` (429) - Rate limit exceeded
|
|
674
|
-
*
|
|
675
|
-
* ### 5xx Server Errors
|
|
676
|
-
* - `INTERNAL_SERVER_ERROR` (500) - Unexpected server error
|
|
677
|
-
* - `NOT_IMPLEMENTED` (501) - Feature not implemented
|
|
678
|
-
* - `BAD_GATEWAY` (502) - Upstream server error
|
|
679
|
-
* - `SERVICE_UNAVAILABLE` (503) - Service temporarily down
|
|
680
|
-
* - `GATEWAY_TIMEOUT` (504) - Upstream timeout
|
|
681
|
-
*
|
|
682
|
-
* @example
|
|
683
|
-
* ```typescript
|
|
684
|
-
* import { ErrorX, PRESETS } from '@bombillazo/error-x'
|
|
685
|
-
*
|
|
686
|
-
* // API endpoint example
|
|
687
|
-
* app.get('/users/:id', async (req, res) => {
|
|
688
|
-
* const user = await db.users.findById(req.params.id)
|
|
689
|
-
*
|
|
690
|
-
* if (!user) {
|
|
691
|
-
* throw new ErrorX({
|
|
692
|
-
* ...PRESETS.HTTP.NOT_FOUND,
|
|
693
|
-
* message: 'User not found',
|
|
694
|
-
* metadata: { userId: req.params.id }
|
|
695
|
-
* })
|
|
696
|
-
* }
|
|
697
|
-
*
|
|
698
|
-
* res.json(user)
|
|
699
|
-
* })
|
|
700
|
-
*
|
|
701
|
-
* // Authentication middleware example
|
|
702
|
-
* const requireAuth = (req, res, next) => {
|
|
703
|
-
* if (!req.user) {
|
|
704
|
-
* throw new ErrorX({
|
|
705
|
-
* ...PRESETS.HTTP.UNAUTHORIZED,
|
|
706
|
-
* actions: [
|
|
707
|
-
* { action: 'redirect', payload: { redirectURL: '/login' } }
|
|
708
|
-
* ]
|
|
709
|
-
* })
|
|
710
|
-
* }
|
|
711
|
-
* next()
|
|
712
|
-
* }
|
|
713
|
-
*
|
|
714
|
-
* // Rate limiting example
|
|
715
|
-
* if (isRateLimited(req.ip)) {
|
|
716
|
-
* throw new ErrorX({
|
|
717
|
-
* ...PRESETS.HTTP.TOO_MANY_REQUESTS,
|
|
718
|
-
* metadata: {
|
|
719
|
-
* ip: req.ip,
|
|
720
|
-
* retryAfter: 60
|
|
721
|
-
* }
|
|
722
|
-
* })
|
|
723
|
-
* }
|
|
724
|
-
* ```
|
|
725
|
-
*
|
|
726
|
-
* @public
|
|
727
|
-
*/
|
|
728
|
-
declare const PRESETS: {
|
|
729
599
|
/**
|
|
730
600
|
* HTTP error presets for common HTTP status codes.
|
|
731
|
-
*
|
|
601
|
+
*
|
|
602
|
+
* ## Features
|
|
603
|
+
* - **Pre-configured error templates** for common HTTP status codes (400-511)
|
|
604
|
+
* - **Type-safe** with TypeScript support
|
|
605
|
+
* - **Fully customizable** via destructuring and override pattern
|
|
606
|
+
* - **User-friendly messages** included for all presets
|
|
607
|
+
* - **Categorized by type** - all HTTP presets include `type: 'http'`
|
|
608
|
+
*
|
|
609
|
+
* ## Usage Patterns
|
|
610
|
+
*
|
|
611
|
+
* ### 1. Direct Usage
|
|
612
|
+
* Use a preset as-is without any modifications:
|
|
613
|
+
* ```typescript
|
|
614
|
+
* throw new ErrorX(ErrorX.HTTP.NOT_FOUND)
|
|
615
|
+
* // Result: 404 error with default message and UI message
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* ### 2. Override Specific Fields
|
|
619
|
+
* Customize the error while keeping other preset values:
|
|
620
|
+
* ```typescript
|
|
621
|
+
* throw new ErrorX({
|
|
622
|
+
* ...ErrorX.HTTP.NOT_FOUND,
|
|
623
|
+
* message: 'User not found',
|
|
624
|
+
* metadata: { userId: 123 }
|
|
625
|
+
* })
|
|
626
|
+
* // Result: 404 error with custom message but keeps httpStatus, code, name, uiMessage, type
|
|
627
|
+
* ```
|
|
628
|
+
*
|
|
629
|
+
* ### 3. Add Metadata and Actions
|
|
630
|
+
* Enhance presets with additional context and behaviors:
|
|
631
|
+
* ```typescript
|
|
632
|
+
* throw new ErrorX({
|
|
633
|
+
* ...ErrorX.HTTP.UNAUTHORIZED,
|
|
634
|
+
* metadata: { attemptedAction: 'viewProfile', userId: 456 },
|
|
635
|
+
* actions: [
|
|
636
|
+
* { action: 'logout', payload: { clearStorage: true } },
|
|
637
|
+
* { action: 'redirect', payload: { redirectURL: '/login' } }
|
|
638
|
+
* ]
|
|
639
|
+
* })
|
|
640
|
+
* ```
|
|
641
|
+
*
|
|
642
|
+
* ### 4. Add Error Cause
|
|
643
|
+
* Chain errors by adding a cause:
|
|
644
|
+
* ```typescript
|
|
645
|
+
* try {
|
|
646
|
+
* // some operation
|
|
647
|
+
* } catch (originalError) {
|
|
648
|
+
* throw new ErrorX({
|
|
649
|
+
* ...ErrorX.HTTP.INTERNAL_SERVER_ERROR,
|
|
650
|
+
* cause: originalError,
|
|
651
|
+
* metadata: { operation: 'database-query' }
|
|
652
|
+
* })
|
|
653
|
+
* }
|
|
654
|
+
* ```
|
|
655
|
+
*
|
|
656
|
+
* ## Common HTTP Presets
|
|
657
|
+
*
|
|
658
|
+
* ### 4xx Client Errors
|
|
659
|
+
* - `BAD_REQUEST` (400) - Invalid request data
|
|
660
|
+
* - `UNAUTHORIZED` (401) - Authentication required
|
|
661
|
+
* - `FORBIDDEN` (403) - Insufficient permissions
|
|
662
|
+
* - `NOT_FOUND` (404) - Resource not found
|
|
663
|
+
* - `METHOD_NOT_ALLOWED` (405) - HTTP method not allowed
|
|
664
|
+
* - `CONFLICT` (409) - Resource conflict
|
|
665
|
+
* - `UNPROCESSABLE_ENTITY` (422) - Validation failed
|
|
666
|
+
* - `TOO_MANY_REQUESTS` (429) - Rate limit exceeded
|
|
667
|
+
*
|
|
668
|
+
* ### 5xx Server Errors
|
|
669
|
+
* - `INTERNAL_SERVER_ERROR` (500) - Unexpected server error
|
|
670
|
+
* - `NOT_IMPLEMENTED` (501) - Feature not implemented
|
|
671
|
+
* - `BAD_GATEWAY` (502) - Upstream server error
|
|
672
|
+
* - `SERVICE_UNAVAILABLE` (503) - Service temporarily down
|
|
673
|
+
* - `GATEWAY_TIMEOUT` (504) - Upstream timeout
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```typescript
|
|
677
|
+
* // API endpoint example
|
|
678
|
+
* app.get('/users/:id', async (req, res) => {
|
|
679
|
+
* const user = await db.users.findById(req.params.id)
|
|
680
|
+
*
|
|
681
|
+
* if (!user) {
|
|
682
|
+
* throw new ErrorX({
|
|
683
|
+
* ...ErrorX.HTTP.NOT_FOUND,
|
|
684
|
+
* message: 'User not found',
|
|
685
|
+
* metadata: { userId: req.params.id }
|
|
686
|
+
* })
|
|
687
|
+
* }
|
|
688
|
+
*
|
|
689
|
+
* res.json(user)
|
|
690
|
+
* })
|
|
691
|
+
*
|
|
692
|
+
* // Authentication middleware example
|
|
693
|
+
* const requireAuth = (req, res, next) => {
|
|
694
|
+
* if (!req.user) {
|
|
695
|
+
* throw new ErrorX({
|
|
696
|
+
* ...ErrorX.HTTP.UNAUTHORIZED,
|
|
697
|
+
* actions: [
|
|
698
|
+
* { action: 'redirect', payload: { redirectURL: '/login' } }
|
|
699
|
+
* ]
|
|
700
|
+
* })
|
|
701
|
+
* }
|
|
702
|
+
* next()
|
|
703
|
+
* }
|
|
704
|
+
*
|
|
705
|
+
* // Rate limiting example
|
|
706
|
+
* if (isRateLimited(req.ip)) {
|
|
707
|
+
* throw new ErrorX({
|
|
708
|
+
* ...ErrorX.HTTP.TOO_MANY_REQUESTS,
|
|
709
|
+
* metadata: {
|
|
710
|
+
* ip: req.ip,
|
|
711
|
+
* retryAfter: 60
|
|
712
|
+
* }
|
|
713
|
+
* })
|
|
714
|
+
* }
|
|
715
|
+
* ```
|
|
716
|
+
*
|
|
717
|
+
* @public
|
|
732
718
|
*/
|
|
733
|
-
readonly HTTP: {
|
|
719
|
+
static readonly HTTP: {
|
|
734
720
|
readonly BAD_REQUEST: {
|
|
735
721
|
httpStatus: number;
|
|
736
722
|
code: string;
|
|
@@ -1044,6 +1030,6 @@ declare const PRESETS: {
|
|
|
1044
1030
|
type: string;
|
|
1045
1031
|
};
|
|
1046
1032
|
};
|
|
1047
|
-
}
|
|
1033
|
+
}
|
|
1048
1034
|
|
|
1049
|
-
export { type CustomAction, type ErrorAction, type ErrorMetadata, ErrorX, type ErrorXOptions, type HandlingTarget, HandlingTargets, type LogoutAction, type NotifyAction,
|
|
1035
|
+
export { type CustomAction, type ErrorAction, type ErrorMetadata, ErrorX, type ErrorXOptions, type HandlingTarget, HandlingTargets, type LogoutAction, type NotifyAction, type RedirectAction, type SerializableError };
|