@wix/auto-patterns 1.33.0 → 1.35.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/docs/GETTING_STARTED.md +98 -0
- package/docs/OVERVIEW.md +54 -0
- package/docs/documentation.yaml +11 -0
- package/{dist/docs → mcp-docs}/action_cell.md +50 -2
- package/{dist/docs → mcp-docs}/auto-patterns-guide.md +610 -39
- package/{dist/docs → mcp-docs}/bulk_actions.md +78 -22
- package/{dist/docs → mcp-docs}/collection_page_actions.md +56 -2
- package/{dist/docs → mcp-docs}/custom_overrides.md +1 -0
- package/{dist/docs → mcp-docs}/entity_page_actions.md +78 -1
- package/{dist/docs → mcp-docs}/entity_page_view_actions.md +2 -0
- package/mcp-docs/error_handling.md +470 -0
- package/package.json +14 -15
- package/dist/docs/error_handling.md +0 -135
- /package/{dist/docs → mcp-docs}/app_config_structure.md +0 -0
- /package/{dist/docs → mcp-docs}/app_context.md +0 -0
- /package/{dist/docs → mcp-docs}/collection_page.md +0 -0
- /package/{dist/docs → mcp-docs}/entity_page.md +0 -0
- /package/{dist/docs → mcp-docs}/index.md +0 -0
- /package/{dist/docs → mcp-docs}/installation.md +0 -0
- /package/{dist/docs → mcp-docs}/introduction.md +0 -0
- /package/{dist/docs → mcp-docs}/pages_configuration.md +0 -0
- /package/{dist/docs → mcp-docs}/resolved_action.md +0 -0
- /package/{dist/docs → mcp-docs}/schema_config.md +0 -0
- /package/{dist/docs → mcp-docs}/sdk_utilities.md +0 -0
- /package/{dist/docs → mcp-docs}/wix_fqdn_custom_data_source.md +0 -0
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
# Error Handling for HTTP Requests
|
|
2
|
+
|
|
3
|
+
**⚠️ CRITICAL FOR AI AGENTS**: When creating or updating ANY custom actions (ActionCell, BulkActions, CollectionPage actions, EntityPage actions, or Custom Data Sources), you **MUST ALWAYS** read this error handling documentation first and apply the correct error handling patterns.
|
|
4
|
+
|
|
5
|
+
**⚠️ MANDATORY WORKFLOW FOR AI AGENTS:**
|
|
6
|
+
1. **BEFORE** implementing any custom action, read this entire error handling documentation
|
|
7
|
+
2. **IDENTIFY** what type of HTTP requests (if any) your action will make
|
|
8
|
+
3. **APPLY** the correct error handling pattern based on the request type
|
|
9
|
+
4. **VERIFY** your implementation follows the validation checklist below
|
|
10
|
+
5. **NEVER** skip this process - it's critical for proper error handling
|
|
11
|
+
|
|
12
|
+
**⚠️ CRITICAL**: When implementing custom data sources that make HTTP requests, you **MUST** wrap specific HTTP calls with proper error handling. This ensures consistent error management, prevents unhandled promise rejections, and provides better user experience.
|
|
13
|
+
|
|
14
|
+
## What Requires Error Handling
|
|
15
|
+
|
|
16
|
+
**ONLY the following HTTP requests** in your custom data source actions must be wrapped with `errorHandler.withErrorHandler`:
|
|
17
|
+
|
|
18
|
+
- **httpClient from @wix/essentials** (e.g., `httpClient.request(getDummyEntity(...))`)
|
|
19
|
+
- **Wix APIs** (e.g., `wix/data`, `wix/stores`, `items.get()`, `items.insert()`, `collections.getDataCollection()`)
|
|
20
|
+
- **httpClient.fetchWithAuth()** calls
|
|
21
|
+
|
|
22
|
+
**DO NOT use errorHandler.withErrorHandler with:**
|
|
23
|
+
|
|
24
|
+
- **External API calls** using `fetch()`, `axios.get()`, or other HTTP libraries
|
|
25
|
+
- **Custom HTTP clients** (e.g., any non-Wix HTTP request library)
|
|
26
|
+
- **Third-party API calls** using non-Wix HTTP methods
|
|
27
|
+
|
|
28
|
+
**EXCEPTION**: SDK actions that you get from the AutoPatterns SDK (e.g., `sdk.getOptimisticActions()`, `sdk.getSchema()`) do NOT need error handling as they are already handled internally.
|
|
29
|
+
|
|
30
|
+
## ⚠️ CRITICAL: When to Apply Error Handling
|
|
31
|
+
|
|
32
|
+
**ALWAYS apply error handling when:**
|
|
33
|
+
|
|
34
|
+
1. **Creating Custom Data Sources** - All HTTP requests in data source actions must be wrapped
|
|
35
|
+
2. **Implementing FQDN-based Custom Data Sources** - All Wix API calls must be wrapped
|
|
36
|
+
3. **Making any Wix HTTP requests** in custom actions (ActionCell, BulkActions, etc.)
|
|
37
|
+
|
|
38
|
+
**NEVER apply error handling when:**
|
|
39
|
+
|
|
40
|
+
1. **Using external APIs** (fetch, axios, third-party libraries)
|
|
41
|
+
2. **Using AutoPatterns SDK methods** (sdk.getOptimisticActions, sdk.getSchema, etc.)
|
|
42
|
+
3. **Making non-HTTP operations** (local computations, form validations, etc.)
|
|
43
|
+
|
|
44
|
+
## Why Error Handling is Required
|
|
45
|
+
|
|
46
|
+
Custom data sources often make HTTP requests to Wix APIs and services. Without proper error handling:
|
|
47
|
+
|
|
48
|
+
- **Unhandled Promise Rejections**: HTTP failures can crash your application
|
|
49
|
+
- **Poor User Experience**: Users don't get meaningful error messages
|
|
50
|
+
- **Inconsistent Error Management**: Different parts of your app handle errors differently
|
|
51
|
+
- **Debugging Difficulties**: Hard to trace and diagnose API failures
|
|
52
|
+
|
|
53
|
+
## Using @wix/essentials errorHandler
|
|
54
|
+
|
|
55
|
+
The recommended approach is to use the `errorHandler` from `@wix/essentials` package, which provides consistent error handling patterns across Wix applications.
|
|
56
|
+
|
|
57
|
+
### Installation
|
|
58
|
+
|
|
59
|
+
First, ensure `@wix/essentials` is installed in your project:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install @wix/essentials
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Basic Error Handling Pattern
|
|
66
|
+
|
|
67
|
+
**⚠️ MANDATORY**: Wrap Wix HTTP requests in your custom data source actions with `errorHandler.withErrorHandler`:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { errorHandler } from '@wix/essentials';
|
|
71
|
+
|
|
72
|
+
// In your custom data source actions
|
|
73
|
+
actions: {
|
|
74
|
+
get: async (entityId: string) => {
|
|
75
|
+
return errorHandler.withErrorHandler(
|
|
76
|
+
async () => {
|
|
77
|
+
const response = await httpClient.request(
|
|
78
|
+
getDummyEntity({ dummyEntityId: entityId })
|
|
79
|
+
);
|
|
80
|
+
return response.data.dummyEntity;
|
|
81
|
+
},
|
|
82
|
+
{}
|
|
83
|
+
);
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### ⚠️ CRITICAL: Error Handling in Custom Actions
|
|
89
|
+
|
|
90
|
+
**When implementing ANY custom action (ActionCell, BulkActions, CollectionPage actions, EntityPage actions), you MUST:**
|
|
91
|
+
|
|
92
|
+
1. **Check if your action makes Wix HTTP requests**
|
|
93
|
+
2. **If YES**: Wrap the HTTP request with `errorHandler.withErrorHandler`
|
|
94
|
+
3. **If NO**: Do not use errorHandler (e.g., for external APIs, SDK methods, local operations)
|
|
95
|
+
|
|
96
|
+
**Example: Custom ActionCell with Wix API call**
|
|
97
|
+
```typescript
|
|
98
|
+
import { errorHandler } from '@wix/essentials';
|
|
99
|
+
|
|
100
|
+
export const myCustomAction: CustomActionCellActionResolver = (params) => {
|
|
101
|
+
const { actionParams, sdk } = params;
|
|
102
|
+
const { item } = actionParams;
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
label: 'Update Status',
|
|
106
|
+
icon: <UpdateIcon />,
|
|
107
|
+
onClick: () => {
|
|
108
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
109
|
+
errorHandler.withErrorHandler(
|
|
110
|
+
async () => {
|
|
111
|
+
const response = await httpClient.request(
|
|
112
|
+
updateEntityStatus({ entityId: item.id, status: 'active' })
|
|
113
|
+
);
|
|
114
|
+
return response.data;
|
|
115
|
+
},
|
|
116
|
+
{}
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Example: Custom ActionCell with External API call**
|
|
124
|
+
```typescript
|
|
125
|
+
export const myCustomAction: CustomActionCellActionResolver = (params) => {
|
|
126
|
+
const { actionParams, sdk } = params;
|
|
127
|
+
const { item } = actionParams;
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
label: 'Send to External Service',
|
|
131
|
+
icon: <SendIcon />,
|
|
132
|
+
onClick: () => {
|
|
133
|
+
// ✅ CORRECT: External API call - NO errorHandler needed
|
|
134
|
+
fetch('https://api.external-service.com/send', {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
body: JSON.stringify({ data: item }),
|
|
137
|
+
headers: { 'Content-Type': 'application/json' }
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Examples of HTTP Requests That Need Error Handling
|
|
145
|
+
|
|
146
|
+
**httpClient from @wix/essentials:**
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
return errorHandler.withErrorHandler(async () => {
|
|
150
|
+
const response = await httpClient.request(
|
|
151
|
+
getDummyEntity({ dummyEntityId: entityId })
|
|
152
|
+
);
|
|
153
|
+
return response.data.dummyEntity;
|
|
154
|
+
}, {});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Wix APIs (wix/data, wix/stores):**
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
return errorHandler.withErrorHandler(async () => {
|
|
161
|
+
return await items.get(collectionId, entityId);
|
|
162
|
+
}, {});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**httpClient.fetchWithAuth() calls:**
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
return errorHandler.withErrorHandler(async () => {
|
|
169
|
+
const response = await httpClient.fetchWithAuth("/api/data");
|
|
170
|
+
return await response.json();
|
|
171
|
+
}, {});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Examples of HTTP Requests That Do NOT Need Error Handling
|
|
175
|
+
|
|
176
|
+
**External API calls (using fetch, axios, etc.):**
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
// ✅ CORRECT - No error handling needed for external APIs
|
|
180
|
+
const response = await fetch("https://api.example.com/data");
|
|
181
|
+
return await response.json();
|
|
182
|
+
|
|
183
|
+
// ✅ CORRECT - No error handling needed for axios
|
|
184
|
+
const response = await axios.get("https://api.example.com/data");
|
|
185
|
+
return response.data;
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**Third-party HTTP libraries:**
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// ✅ CORRECT - No error handling needed for third-party HTTP clients
|
|
192
|
+
const response = await someThirdPartyHttpClient.get("/data");
|
|
193
|
+
return response.data;
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Integration with AutoPatterns Error Handling
|
|
197
|
+
|
|
198
|
+
The error handling in your custom data source integrates seamlessly with AutoPatterns' built-in error handling:
|
|
199
|
+
|
|
200
|
+
1. **User-Friendly Messages**: Errors are displayed to users in a consistent format
|
|
201
|
+
2. **Retry Mechanisms**: Users can retry failed operations
|
|
202
|
+
3. **Loading States**: Proper loading states during HTTP requests
|
|
203
|
+
4. **Error Boundaries**: Errors are caught and handled gracefully
|
|
204
|
+
|
|
205
|
+
## ⚠️ AI AGENT VALIDATION CHECKLIST
|
|
206
|
+
|
|
207
|
+
**BEFORE implementing ANY custom action or data source, AI agents MUST verify:**
|
|
208
|
+
|
|
209
|
+
### ✅ Error Handling Requirements
|
|
210
|
+
- **httpClient from @wix/essentials** is wrapped with `errorHandler.withErrorHandler`
|
|
211
|
+
- **Wix APIs (wix/data, wix/stores, etc)** are wrapped with `errorHandler.withErrorHandler`
|
|
212
|
+
- **httpClient.fetchWithAuth()** calls are wrapped with `errorHandler.withErrorHandler`
|
|
213
|
+
- **External API calls are NOT wrapped** with errorHandler (e.g., fetch(), axios, third-party HTTP clients)
|
|
214
|
+
- **SDK actions are used directly** without error handling (e.g., `sdk.getOptimisticActions()`, `sdk.getSchema()`)
|
|
215
|
+
- **@wix/essentials** is installed as a dependency
|
|
216
|
+
|
|
217
|
+
### ✅ Implementation Checklist
|
|
218
|
+
- **Read this error handling documentation** before implementing any custom action
|
|
219
|
+
- **Identify the type of HTTP request** being made (Wix vs External)
|
|
220
|
+
- **Apply the correct error handling pattern** based on the request type
|
|
221
|
+
- **Test error scenarios** to ensure proper error handling
|
|
222
|
+
- **Verify error messages** are user-friendly and actionable
|
|
223
|
+
|
|
224
|
+
### ✅ Common Mistakes to Avoid
|
|
225
|
+
- ❌ **NEVER** wrap external API calls (fetch, axios) with errorHandler
|
|
226
|
+
- ❌ **NEVER** wrap SDK methods (sdk.getOptimisticActions) with errorHandler
|
|
227
|
+
- ❌ **NEVER** forget to wrap Wix HTTP requests with errorHandler
|
|
228
|
+
- ❌ **NEVER** implement custom actions without reading this documentation first
|
|
229
|
+
|
|
230
|
+
## Quick Decision Tree for AI Agents
|
|
231
|
+
|
|
232
|
+
**When implementing custom actions, ask:**
|
|
233
|
+
|
|
234
|
+
1. **Does this action make HTTP requests?**
|
|
235
|
+
- **NO** → No error handling needed
|
|
236
|
+
- **YES** → Go to step 2
|
|
237
|
+
|
|
238
|
+
2. **What type of HTTP request?**
|
|
239
|
+
- **Wix APIs** (httpClient, wix/data, wix/stores) → **WRAP with errorHandler.withErrorHandler**
|
|
240
|
+
- **External APIs** (fetch, axios, third-party) → **NO errorHandler needed**
|
|
241
|
+
- **SDK methods** (sdk.getOptimisticActions, sdk.getSchema) → **NO errorHandler needed**
|
|
242
|
+
|
|
243
|
+
3. **Verify implementation:**
|
|
244
|
+
- **Wix requests** are wrapped with `errorHandler.withErrorHandler`
|
|
245
|
+
- **External requests** are NOT wrapped
|
|
246
|
+
- **SDK methods** are used directly
|
|
247
|
+
|
|
248
|
+
## Complete Examples for All Custom Action Types
|
|
249
|
+
|
|
250
|
+
### ActionCell Custom Actions
|
|
251
|
+
|
|
252
|
+
**Example 1: ActionCell with Wix API call (REQUIRES errorHandler)**
|
|
253
|
+
```typescript
|
|
254
|
+
import { errorHandler } from '@wix/essentials';
|
|
255
|
+
import { CustomActionCellActionResolver } from '@wix/auto-patterns';
|
|
256
|
+
|
|
257
|
+
export const updateEntityStatus: CustomActionCellActionResolver = (params) => {
|
|
258
|
+
const { actionParams, sdk } = params;
|
|
259
|
+
const { item } = actionParams;
|
|
260
|
+
|
|
261
|
+
return {
|
|
262
|
+
label: 'Update Status',
|
|
263
|
+
icon: <UpdateIcon />,
|
|
264
|
+
onClick: () => {
|
|
265
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
266
|
+
errorHandler.withErrorHandler(
|
|
267
|
+
async () => {
|
|
268
|
+
const response = await httpClient.request(
|
|
269
|
+
updateEntityStatus({ entityId: item.id, status: 'active' })
|
|
270
|
+
);
|
|
271
|
+
return response.data;
|
|
272
|
+
},
|
|
273
|
+
{}
|
|
274
|
+
);
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
**Example 2: ActionCell with External API call (NO errorHandler needed)**
|
|
281
|
+
```typescript
|
|
282
|
+
import { CustomActionCellActionResolver } from '@wix/auto-patterns';
|
|
283
|
+
|
|
284
|
+
export const sendToExternalService: CustomActionCellActionResolver = (params) => {
|
|
285
|
+
const { actionParams, sdk } = params;
|
|
286
|
+
const { item } = actionParams;
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
label: 'Send to External Service',
|
|
290
|
+
icon: <SendIcon />,
|
|
291
|
+
onClick: () => {
|
|
292
|
+
// ✅ CORRECT: External API call - NO errorHandler needed
|
|
293
|
+
fetch('https://api.external-service.com/send', {
|
|
294
|
+
method: 'POST',
|
|
295
|
+
body: JSON.stringify({ data: item }),
|
|
296
|
+
headers: { 'Content-Type': 'application/json' }
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### BulkActions Custom Actions
|
|
304
|
+
|
|
305
|
+
**Example 3: BulkAction with Wix API call (REQUIRES errorHandler)**
|
|
306
|
+
```typescript
|
|
307
|
+
import { errorHandler } from '@wix/essentials';
|
|
308
|
+
import { CustomBulkActionsActionResolver } from '@wix/auto-patterns';
|
|
309
|
+
|
|
310
|
+
export const bulkUpdateStatus: CustomBulkActionsActionResolver = (params) => {
|
|
311
|
+
const { actionParams, sdk } = params;
|
|
312
|
+
const { selectedValues } = actionParams;
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
label: 'Bulk Update Status',
|
|
316
|
+
icon: <BulkUpdateIcon />,
|
|
317
|
+
onClick: () => {
|
|
318
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
319
|
+
errorHandler.withErrorHandler(
|
|
320
|
+
async () => {
|
|
321
|
+
const response = await httpClient.request(
|
|
322
|
+
bulkUpdateEntityStatus({
|
|
323
|
+
entityIds: selectedValues.map(item => item.id),
|
|
324
|
+
status: 'active'
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
return response.data;
|
|
328
|
+
},
|
|
329
|
+
{}
|
|
330
|
+
);
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### CollectionPage Custom Actions
|
|
337
|
+
|
|
338
|
+
**Example 4: CollectionPage action with Wix API call (REQUIRES errorHandler)**
|
|
339
|
+
```typescript
|
|
340
|
+
import { errorHandler } from '@wix/essentials';
|
|
341
|
+
import { CustomActionCollectionPageActionResolver } from '@wix/auto-patterns';
|
|
342
|
+
|
|
343
|
+
export const exportCollection: CustomActionCollectionPageActionResolver = (params) => {
|
|
344
|
+
const { actionParams, sdk } = params;
|
|
345
|
+
const { collectionId } = actionParams;
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
label: 'Export Collection',
|
|
349
|
+
icon: <ExportIcon />,
|
|
350
|
+
onClick: () => {
|
|
351
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
352
|
+
errorHandler.withErrorHandler(
|
|
353
|
+
async () => {
|
|
354
|
+
const response = await httpClient.request(
|
|
355
|
+
exportCollectionData({ collectionId })
|
|
356
|
+
);
|
|
357
|
+
return response.data;
|
|
358
|
+
},
|
|
359
|
+
{}
|
|
360
|
+
);
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### EntityPage Custom Actions
|
|
367
|
+
|
|
368
|
+
**Example 5: EntityPage action with Wix API call (REQUIRES errorHandler)**
|
|
369
|
+
```typescript
|
|
370
|
+
import { errorHandler } from '@wix/essentials';
|
|
371
|
+
import { CustomEntityPageMoreActionsActionResolver } from '@wix/auto-patterns';
|
|
372
|
+
|
|
373
|
+
export const sendEmail: CustomEntityPageMoreActionsActionResolver = (params) => {
|
|
374
|
+
const { actionParams, sdk } = params;
|
|
375
|
+
const { entity } = actionParams;
|
|
376
|
+
|
|
377
|
+
return {
|
|
378
|
+
label: 'Send Email',
|
|
379
|
+
icon: <EmailIcon />,
|
|
380
|
+
onClick: () => {
|
|
381
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
382
|
+
errorHandler.withErrorHandler(
|
|
383
|
+
async () => {
|
|
384
|
+
const response = await httpClient.request(
|
|
385
|
+
sendEmailToEntity({ entityId: entity.id, email: entity.email })
|
|
386
|
+
);
|
|
387
|
+
return response.data;
|
|
388
|
+
},
|
|
389
|
+
{}
|
|
390
|
+
);
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Custom Data Sources
|
|
397
|
+
|
|
398
|
+
**Example 6: Custom Data Source with Wix API calls (REQUIRES errorHandler)**
|
|
399
|
+
```typescript
|
|
400
|
+
import { errorHandler } from '@wix/essentials';
|
|
401
|
+
|
|
402
|
+
export const myCustomDataSource = async (collectionId: string, context: any) => {
|
|
403
|
+
return {
|
|
404
|
+
id: 'myCustomCollection',
|
|
405
|
+
fields: {
|
|
406
|
+
// ... field definitions
|
|
407
|
+
},
|
|
408
|
+
idField: 'id',
|
|
409
|
+
actions: {
|
|
410
|
+
get: async (entityId: string) => {
|
|
411
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
412
|
+
return errorHandler.withErrorHandler(
|
|
413
|
+
async () => {
|
|
414
|
+
const response = await httpClient.request(
|
|
415
|
+
getDummyEntity({ dummyEntityId: entityId })
|
|
416
|
+
);
|
|
417
|
+
return response.data.dummyEntity;
|
|
418
|
+
},
|
|
419
|
+
{}
|
|
420
|
+
);
|
|
421
|
+
},
|
|
422
|
+
create: async (newEntity: any) => {
|
|
423
|
+
// ✅ CORRECT: Wix API call wrapped with errorHandler
|
|
424
|
+
return errorHandler.withErrorHandler(
|
|
425
|
+
async () => {
|
|
426
|
+
const response = await httpClient.request(
|
|
427
|
+
createDummyEntity({ dummyEntity: newEntity })
|
|
428
|
+
);
|
|
429
|
+
return response.data.dummyEntity;
|
|
430
|
+
},
|
|
431
|
+
{}
|
|
432
|
+
);
|
|
433
|
+
},
|
|
434
|
+
// ... other actions
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
};
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### Actions Using SDK Methods (NO errorHandler needed)
|
|
441
|
+
|
|
442
|
+
**Example 7: Action using SDK methods (NO errorHandler needed)**
|
|
443
|
+
```typescript
|
|
444
|
+
import { CustomActionCellActionResolver } from '@wix/auto-patterns';
|
|
445
|
+
|
|
446
|
+
export const quickUpdate: CustomActionCellActionResolver = (params) => {
|
|
447
|
+
const { actionParams, sdk } = params;
|
|
448
|
+
const { item } = actionParams;
|
|
449
|
+
|
|
450
|
+
return {
|
|
451
|
+
label: 'Quick Update',
|
|
452
|
+
icon: <QuickUpdateIcon />,
|
|
453
|
+
onClick: () => {
|
|
454
|
+
// ✅ CORRECT: SDK methods - NO errorHandler needed
|
|
455
|
+
const optimisticActions = sdk.getOptimisticActions(sdk.collectionId);
|
|
456
|
+
const schema = sdk.getSchema(sdk.collectionId);
|
|
457
|
+
|
|
458
|
+
const updatedItem = { ...item, lastUpdated: new Date() };
|
|
459
|
+
optimisticActions.updateOne(updatedItem, {
|
|
460
|
+
submit: async (items) => schema.actions.update(items[0]),
|
|
461
|
+
successToast: 'Item updated successfully',
|
|
462
|
+
errorToast: (err, {retry}) => ({
|
|
463
|
+
text: 'Update failed',
|
|
464
|
+
action: { text: 'Retry', onClick: retry }
|
|
465
|
+
})
|
|
466
|
+
});
|
|
467
|
+
},
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/auto-patterns",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.35.0",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Matvey Oklander",
|
|
@@ -18,13 +18,14 @@
|
|
|
18
18
|
"!dist/**/__tests__",
|
|
19
19
|
"types",
|
|
20
20
|
"react",
|
|
21
|
-
"form"
|
|
21
|
+
"form",
|
|
22
|
+
"docs",
|
|
23
|
+
"mcp-docs"
|
|
22
24
|
],
|
|
23
25
|
"scripts": {
|
|
26
|
+
"build": "npm run generate-guide && yoshi-library build",
|
|
27
|
+
"start": "npm run generate-guide && yoshi-library start",
|
|
24
28
|
"generate-guide": "node scripts/generate-guide.js",
|
|
25
|
-
"build": "yoshi-library build && npm run generate-guide",
|
|
26
|
-
"prestart": "npm run generate-guide",
|
|
27
|
-
"start": "yoshi-library start",
|
|
28
29
|
"test": "yoshi-library test",
|
|
29
30
|
"test:watch": "yoshi-library test --watch",
|
|
30
31
|
"lint": "yoshi-library lint",
|
|
@@ -35,9 +36,8 @@
|
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
38
|
"@babel/runtime": "^7.28.4",
|
|
38
|
-
"@wix/data": "^1.0.
|
|
39
|
+
"@wix/data": "^1.0.290",
|
|
39
40
|
"@wix/wix-ui-icons-common": "^3.87.3",
|
|
40
|
-
"ejs": "^3.1.10",
|
|
41
41
|
"lodash": "^4.17.21"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
@@ -46,22 +46,21 @@
|
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@testing-library/react": "^11.2.7",
|
|
49
|
-
"@types/ejs": "^3.1.5",
|
|
50
49
|
"@types/jest": "^27.5.2",
|
|
51
50
|
"@types/lodash": "^4.17.20",
|
|
52
51
|
"@types/node": "^16.18.126",
|
|
53
52
|
"@types/node-fetch": "^2.6.13",
|
|
54
53
|
"@types/react": "^16.14.66",
|
|
55
|
-
"@wix/crm": "^1.0.
|
|
54
|
+
"@wix/crm": "^1.0.1022",
|
|
56
55
|
"@wix/design-system": "^1.214.3",
|
|
57
56
|
"@wix/eslint-config-yoshi": "^6.158.0",
|
|
58
57
|
"@wix/fe-essentials-standalone": "^1.1380.0",
|
|
59
58
|
"@wix/jest-yoshi-preset": "^6.158.0",
|
|
60
|
-
"@wix/patterns": "^1.
|
|
61
|
-
"@wix/sdk": "^1.
|
|
62
|
-
"@wix/sdk-testkit": ">=0.1.
|
|
63
|
-
"@wix/wix-data-items-common": "^1.0.
|
|
64
|
-
"@wix/wix-data-items-sdk": "^1.0.
|
|
59
|
+
"@wix/patterns": "^1.279.0",
|
|
60
|
+
"@wix/sdk": "^1.16.0",
|
|
61
|
+
"@wix/sdk-testkit": ">=0.1.8",
|
|
62
|
+
"@wix/wix-data-items-common": "^1.0.235",
|
|
63
|
+
"@wix/wix-data-items-sdk": "^1.0.428",
|
|
65
64
|
"@wix/yoshi-flow-library": "^6.158.0",
|
|
66
65
|
"@wix/yoshi-style-dependencies": "^6.158.0",
|
|
67
66
|
"chance": "^1.1.13",
|
|
@@ -126,5 +125,5 @@
|
|
|
126
125
|
"wallaby": {
|
|
127
126
|
"autoDetect": true
|
|
128
127
|
},
|
|
129
|
-
"falconPackageHash": "
|
|
128
|
+
"falconPackageHash": "da47af708036d7d07cc5947543173eddfa769691a73169ff7e5a65b2"
|
|
130
129
|
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
# Error Handling for HTTP Requests
|
|
2
|
-
|
|
3
|
-
**⚠️ CRITICAL**: When implementing custom data sources that make HTTP requests, you **MUST** wrap specific HTTP calls with proper error handling. This ensures consistent error management, prevents unhandled promise rejections, and provides better user experience.
|
|
4
|
-
|
|
5
|
-
## What Requires Error Handling
|
|
6
|
-
|
|
7
|
-
**ONLY the following HTTP requests** in your custom data source actions must be wrapped with `errorHandler`:
|
|
8
|
-
|
|
9
|
-
- **httpClient from @wix/essentials** (e.g., `httpClient.request(getDummyEntity(...))`)
|
|
10
|
-
- **Wix APIs** (e.g., `wix/data`, `wix/stores`, `items.get()`, `items.insert()`, `collections.getDataCollection()`)
|
|
11
|
-
- **httpClient.fetchWithAuth()** calls
|
|
12
|
-
|
|
13
|
-
**DO NOT use errorHandler with:**
|
|
14
|
-
|
|
15
|
-
- **External API calls** using `fetch()`, `axios.get()`, or other HTTP libraries
|
|
16
|
-
- **Custom HTTP clients** (e.g., any non-Wix HTTP request library)
|
|
17
|
-
- **Third-party API calls** using non-Wix HTTP methods
|
|
18
|
-
|
|
19
|
-
**EXCEPTION**: SDK actions that you get from the AutoPatterns SDK (e.g., `sdk.getOptimisticActions()`, `sdk.getSchema()`) do NOT need error handling as they are already handled internally.
|
|
20
|
-
|
|
21
|
-
## Why Error Handling is Required
|
|
22
|
-
|
|
23
|
-
Custom data sources often make HTTP requests to Wix APIs and services. Without proper error handling:
|
|
24
|
-
|
|
25
|
-
- **Unhandled Promise Rejections**: HTTP failures can crash your application
|
|
26
|
-
- **Poor User Experience**: Users don't get meaningful error messages
|
|
27
|
-
- **Inconsistent Error Management**: Different parts of your app handle errors differently
|
|
28
|
-
- **Debugging Difficulties**: Hard to trace and diagnose API failures
|
|
29
|
-
|
|
30
|
-
## Using @wix/essentials errorHandler
|
|
31
|
-
|
|
32
|
-
The recommended approach is to use the `errorHandler` from `@wix/essentials` package, which provides consistent error handling patterns across Wix applications.
|
|
33
|
-
|
|
34
|
-
### Installation
|
|
35
|
-
|
|
36
|
-
First, ensure `@wix/essentials` is installed in your project:
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
npm install @wix/essentials
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### Basic Error Handling Pattern
|
|
43
|
-
|
|
44
|
-
Wrap Wix HTTP requests in your custom data source actions with `errorHandler.withErrorHandler`:
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
import { errorHandler } from '@wix/essentials';
|
|
48
|
-
|
|
49
|
-
// In your custom data source actions
|
|
50
|
-
actions: {
|
|
51
|
-
get: async (entityId: string) => {
|
|
52
|
-
return errorHandler.withErrorHandler(
|
|
53
|
-
async () => {
|
|
54
|
-
const response = await httpClient.request(
|
|
55
|
-
getDummyEntity({ dummyEntityId: entityId })
|
|
56
|
-
);
|
|
57
|
-
return response.data.dummyEntity;
|
|
58
|
-
},
|
|
59
|
-
{}
|
|
60
|
-
);
|
|
61
|
-
},
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### Examples of HTTP Requests That Need Error Handling
|
|
66
|
-
|
|
67
|
-
**httpClient from @wix/essentials:**
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
return errorHandler.withErrorHandler(async () => {
|
|
71
|
-
const response = await httpClient.request(
|
|
72
|
-
getDummyEntity({ dummyEntityId: entityId })
|
|
73
|
-
);
|
|
74
|
-
return response.data.dummyEntity;
|
|
75
|
-
}, {});
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
**Wix APIs (wix/data, wix/stores):**
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
return errorHandler.withErrorHandler(async () => {
|
|
82
|
-
return await items.get(collectionId, entityId);
|
|
83
|
-
}, {});
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
**httpClient.fetchWithAuth() calls:**
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
return errorHandler.withErrorHandler(async () => {
|
|
90
|
-
const response = await httpClient.fetchWithAuth("/api/data");
|
|
91
|
-
return await response.json();
|
|
92
|
-
}, {});
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### Examples of HTTP Requests That Do NOT Need Error Handling
|
|
96
|
-
|
|
97
|
-
**External API calls (using fetch, axios, etc.):**
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
// ✅ CORRECT - No error handling needed for external APIs
|
|
101
|
-
const response = await fetch("https://api.example.com/data");
|
|
102
|
-
return await response.json();
|
|
103
|
-
|
|
104
|
-
// ✅ CORRECT - No error handling needed for axios
|
|
105
|
-
const response = await axios.get("https://api.example.com/data");
|
|
106
|
-
return response.data;
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
**Third-party HTTP libraries:**
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
// ✅ CORRECT - No error handling needed for third-party HTTP clients
|
|
113
|
-
const response = await someThirdPartyHttpClient.get("/data");
|
|
114
|
-
return response.data;
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## Integration with AutoPatterns Error Handling
|
|
118
|
-
|
|
119
|
-
The error handling in your custom data source integrates seamlessly with AutoPatterns' built-in error handling:
|
|
120
|
-
|
|
121
|
-
1. **User-Friendly Messages**: Errors are displayed to users in a consistent format
|
|
122
|
-
2. **Retry Mechanisms**: Users can retry failed operations
|
|
123
|
-
3. **Loading States**: Proper loading states during HTTP requests
|
|
124
|
-
4. **Error Boundaries**: Errors are caught and handled gracefully
|
|
125
|
-
|
|
126
|
-
## Validation Checklist
|
|
127
|
-
|
|
128
|
-
Before deploying your custom data source, ensure:
|
|
129
|
-
|
|
130
|
-
✅ **httpClient from @wix/essentials** is wrapped with `errorHandler.withErrorHandler`
|
|
131
|
-
✅ **Wix APIs (wix/data, wix/stores, etc)** are wrapped with `errorHandler.withErrorHandler`
|
|
132
|
-
✅ **httpClient.fetchWithAuth()** calls are wrapped with `errorHandler.withErrorHandler`
|
|
133
|
-
✅ **External API calls are NOT wrapped** with errorHandler (e.g., fetch(), axios, third-party HTTP clients)
|
|
134
|
-
✅ **SDK actions are used directly** without error handling (e.g., `sdk.getOptimisticActions()`, `sdk.getSchema()`)
|
|
135
|
-
✅ **@wix/essentials** is installed as a dependency
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|