@onlineapps/conn-infra-error-handler 1.0.0 → 1.0.1
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/API.md +0 -0
- package/README.md +225 -0
- package/onlineapps-conn-infra-error-handler-1.0.0.tgz +0 -0
- package/package.json +5 -4
- package/src/index.js +208 -577
- package/{test → tests}/component/error-handling-flow.test.js +56 -34
- package/{test → tests}/unit/ErrorHandlerConnector.test.js +106 -100
package/API.md
CHANGED
|
File without changes
|
package/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# @onlineapps/conn-infra-error-handler
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
Error handling connector for business services. Wraps `@onlineapps/error-handler-core` and integrates with `@onlineapps/conn-base-monitoring` for unified error handling and logging.
|
|
5
|
+
|
|
6
|
+
**Note:** This connector is for business services using ServiceWrapper. Infrastructure services should use `@onlineapps/error-handler-core` directly.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
```bash
|
|
10
|
+
npm install @onlineapps/conn-infra-error-handler
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
- **Error Classification** - Automatic error type detection (TRANSIENT, BUSINESS, FATAL, etc.)
|
|
15
|
+
- **Retry Logic** - Exponential backoff for transient errors
|
|
16
|
+
- **Circuit Breaker** - Protection against cascading failures
|
|
17
|
+
- **DLQ Routing** - Dead letter queue routing for permanent errors
|
|
18
|
+
- **Compensation** - Rollback operations for failed workflows
|
|
19
|
+
- **Unified Logging** - Structured error logging via monitoring-core
|
|
20
|
+
|
|
21
|
+
## Architecture
|
|
22
|
+
|
|
23
|
+
This connector wraps `@onlineapps/error-handler-core` and integrates with `@onlineapps/conn-base-monitoring`:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
conn-infra-error-handler (wrapper)
|
|
27
|
+
└─> error-handler-core (core logic)
|
|
28
|
+
└─> monitoring-core (logging)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
See [Unified Error Handling Standard](/docs/standards/UNIFIED_ERROR_HANDLING.md) for complete architecture details.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Basic Setup (via ServiceWrapper)
|
|
36
|
+
|
|
37
|
+
The error handler is automatically integrated when using ServiceWrapper:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
const { ServiceWrapper } = require('@onlineapps/service-wrapper');
|
|
41
|
+
|
|
42
|
+
const wrapper = new ServiceWrapper({
|
|
43
|
+
serviceName: 'my-service',
|
|
44
|
+
monitoring: { enabled: true },
|
|
45
|
+
errorHandling: {
|
|
46
|
+
maxRetries: 3,
|
|
47
|
+
retryDelay: 1000
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Error handler is available as wrapper.errorHandler
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Direct Usage
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
const { ErrorHandlerConnector } = require('@onlineapps/conn-infra-error-handler');
|
|
58
|
+
const { init: initMonitoring } = require('@onlineapps/conn-base-monitoring');
|
|
59
|
+
|
|
60
|
+
// Initialize monitoring first
|
|
61
|
+
const monitoring = await initMonitoring({
|
|
62
|
+
serviceName: 'my-service',
|
|
63
|
+
mode: 'light'
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Initialize error handler
|
|
67
|
+
const errorHandler = new ErrorHandlerConnector({
|
|
68
|
+
serviceName: 'my-service',
|
|
69
|
+
serviceVersion: '1.0.0',
|
|
70
|
+
environment: 'production',
|
|
71
|
+
monitoring: monitoring, // Required: conn-base-monitoring instance
|
|
72
|
+
handling: {
|
|
73
|
+
maxRetries: 3,
|
|
74
|
+
retryDelay: 1000,
|
|
75
|
+
retryMultiplier: 2,
|
|
76
|
+
circuitBreakerEnabled: true,
|
|
77
|
+
dlqEnabled: true,
|
|
78
|
+
mqClient: mqClient // Optional, for DLQ routing
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Error Classification
|
|
84
|
+
```javascript
|
|
85
|
+
try {
|
|
86
|
+
await processMessage(message);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const classified = errorHandler.classify(error);
|
|
89
|
+
|
|
90
|
+
if (classified.isTransient) {
|
|
91
|
+
// Retry-able error (network, timeout)
|
|
92
|
+
await errorHandler.handleTransient(error, message);
|
|
93
|
+
} else {
|
|
94
|
+
// Permanent error (validation, business logic)
|
|
95
|
+
await errorHandler.handlePermanent(error, message);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Retry Logic
|
|
101
|
+
```javascript
|
|
102
|
+
const result = await errorHandler.withRetry(async () => {
|
|
103
|
+
return await externalService.call();
|
|
104
|
+
}, {
|
|
105
|
+
maxAttempts: 3,
|
|
106
|
+
backoffMultiplier: 2,
|
|
107
|
+
initialDelay: 100
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Configuration
|
|
112
|
+
|
|
113
|
+
### Options
|
|
114
|
+
```javascript
|
|
115
|
+
{
|
|
116
|
+
serviceName: 'service-name', // Required
|
|
117
|
+
maxRetries: 3, // Default: 3
|
|
118
|
+
retryDelay: 1000, // Default: 1000ms
|
|
119
|
+
enableDLQ: true, // Default: true
|
|
120
|
+
dlqName: 'workflow.error', // Default: workflow.error
|
|
121
|
+
logLevel: 'error', // Default: error
|
|
122
|
+
includeStackTrace: true // Default: true in dev
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Environment Variables
|
|
127
|
+
```bash
|
|
128
|
+
ERROR_MAX_RETRIES=3
|
|
129
|
+
ERROR_RETRY_DELAY=1000
|
|
130
|
+
ERROR_DLQ_ENABLED=true
|
|
131
|
+
ERROR_DLQ_NAME=workflow.error
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Error Types
|
|
135
|
+
|
|
136
|
+
### Transient Errors
|
|
137
|
+
Automatically retried:
|
|
138
|
+
- Network errors (ECONNREFUSED, ETIMEDOUT)
|
|
139
|
+
- Service unavailable (503)
|
|
140
|
+
- Rate limiting (429)
|
|
141
|
+
- Temporary database issues
|
|
142
|
+
|
|
143
|
+
### Permanent Errors
|
|
144
|
+
Sent to DLQ immediately:
|
|
145
|
+
- Validation errors (400)
|
|
146
|
+
- Authentication errors (401)
|
|
147
|
+
- Not found errors (404)
|
|
148
|
+
- Business logic errors
|
|
149
|
+
|
|
150
|
+
## API Reference
|
|
151
|
+
|
|
152
|
+
### ErrorHandler
|
|
153
|
+
|
|
154
|
+
#### initialize()
|
|
155
|
+
Initializes the error handler with configured options.
|
|
156
|
+
|
|
157
|
+
#### classify(error)
|
|
158
|
+
Classifies an error as transient or permanent.
|
|
159
|
+
Returns: `{ isTransient: boolean, category: string, retryable: boolean }`
|
|
160
|
+
|
|
161
|
+
#### handleTransient(error, context)
|
|
162
|
+
Handles transient errors with retry logic.
|
|
163
|
+
|
|
164
|
+
#### handlePermanent(error, context)
|
|
165
|
+
Routes permanent errors to DLQ.
|
|
166
|
+
|
|
167
|
+
#### withRetry(fn, options)
|
|
168
|
+
Wraps a function with retry logic.
|
|
169
|
+
|
|
170
|
+
#### enrichError(error, context)
|
|
171
|
+
Adds context information to error object.
|
|
172
|
+
|
|
173
|
+
## Error Context
|
|
174
|
+
```javascript
|
|
175
|
+
{
|
|
176
|
+
workflowId: 'wf-uuid',
|
|
177
|
+
spanId: 'span-uuid',
|
|
178
|
+
service: 'hello-service',
|
|
179
|
+
operation: 'greet',
|
|
180
|
+
timestamp: 'ISO-8601',
|
|
181
|
+
retryCount: 2,
|
|
182
|
+
maxRetries: 3,
|
|
183
|
+
errorCode: 'NETWORK_ERROR',
|
|
184
|
+
errorMessage: 'Connection refused',
|
|
185
|
+
stackTrace: '...'
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Integration with Service Wrapper
|
|
190
|
+
|
|
191
|
+
The error handler is automatically integrated when using service-wrapper:
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
const { ServiceWrapper } = require('@onlineapps/service-wrapper');
|
|
195
|
+
|
|
196
|
+
const wrapper = new ServiceWrapper({
|
|
197
|
+
errorHandling: {
|
|
198
|
+
enabled: true,
|
|
199
|
+
maxRetries: 3
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Error handling is automatically configured
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Testing
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
npm test # Run all tests
|
|
210
|
+
npm run test:unit # Unit tests only
|
|
211
|
+
npm run test:component # Component tests
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Dependencies
|
|
215
|
+
- `@onlineapps/error-handler-core` - Core error handling functionality
|
|
216
|
+
- `@onlineapps/conn-base-monitoring` - Monitoring integration for business services
|
|
217
|
+
|
|
218
|
+
## Related Documentation
|
|
219
|
+
- [Unified Error Handling Standard](/docs/standards/UNIFIED_ERROR_HANDLING.md) - Complete error handling architecture
|
|
220
|
+
- [Error Handling Architecture](/docs/standards/ERROR_HANDLING_ARCHITECTURE.md) - Core vs connector distinction
|
|
221
|
+
- [Error Handling Standard](/docs/standards/ERROR_HANDLING.md) - Fail loudly principle
|
|
222
|
+
- [Service Wrapper](/shared/connector/service-wrapper/README.md) - Integration guide
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
*Version: 1.0.0 | License: MIT*
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onlineapps/conn-infra-error-handler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Unified error handling with retry strategies, circuit breaker, and compensation patterns",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest",
|
|
8
|
-
"test:unit": "jest
|
|
9
|
-
"test:integration": "jest
|
|
8
|
+
"test:unit": "jest tests/unit",
|
|
9
|
+
"test:integration": "jest tests/integration",
|
|
10
10
|
"docs": "jsdoc2md --files src/**/*.js > API.md"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"author": "OA Drive Team",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"
|
|
22
|
+
"@onlineapps/conn-base-monitoring": "^1.0.0",
|
|
23
|
+
"@onlineapps/error-handler-core": "1.1.0"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"jest": "^29.7.0"
|