Skip to content

Commit 0b08d02

Browse files
committed
feat(controller): add error handling for failed events in collection flow
- Implement handleFailedEvent method to process failed event scenarios - Log errors and throw an InternalServerErrorException on failure - Update finalSubmission method to call handleFailedEvent upon errors
1 parent 6b46848 commit 0b08d02

File tree

1 file changed

+62
-34
lines changed

1 file changed

+62
-34
lines changed

services/workflows-service/src/collection-flow/controllers/collection-flow.controller.ts

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,34 @@ export class CollectionFlowController {
155155
);
156156
}
157157

158+
async handleFailedEvent({ tokenScope }: { tokenScope: ITokenScope }): Promise<void> {
159+
try {
160+
await this.workflowService.event(
161+
{
162+
id: tokenScope.workflowRuntimeDataId,
163+
name: BUILT_IN_EVENT.DEEP_MERGE_CONTEXT,
164+
payload: {
165+
newContext: {
166+
collectionFlow: {
167+
state: {
168+
status: CollectionFlowStatusesEnum.failed,
169+
},
170+
},
171+
},
172+
arrayMergeOption: ARRAY_MERGE_OPTION.REPLACE,
173+
},
174+
},
175+
[tokenScope.projectId],
176+
tokenScope.projectId,
177+
);
178+
} catch (error) {
179+
this.appLogger.error(error);
180+
throw new common.InternalServerErrorException(
181+
'Failed to set collection flow state as failed.',
182+
);
183+
}
184+
}
185+
158186
@common.Post('/final-submission')
159187
async finalSubmission(@TokenScope() tokenScope: ITokenScope, @common.Body() body: FinishFlowDto) {
160188
try {
@@ -201,46 +229,46 @@ export class CollectionFlowController {
201229
[tokenScope.projectId],
202230
);
203231

204-
return this.workflowService.event(
205-
{
206-
id: tokenScope.workflowRuntimeDataId,
207-
name: eventName,
208-
},
209-
[tokenScope.projectId],
210-
tokenScope.projectId,
211-
);
232+
void this.workflowService
233+
.event(
234+
{ id: tokenScope.workflowRuntimeDataId, name: eventName },
235+
[tokenScope.projectId],
236+
tokenScope.projectId,
237+
)
238+
.then(res => {
239+
this.appLogger.log('Background event completed');
240+
this.appLogger.log('Args:', {
241+
id: tokenScope.workflowRuntimeDataId,
242+
name: eventName,
243+
projectIds: [tokenScope.projectId],
244+
currentProjectId: tokenScope.projectId,
245+
});
246+
this.appLogger.log('Result:', res);
247+
})
248+
.catch(async err => {
249+
// TODO: Add to queue
250+
if (err instanceof CollectionFlowMissingException) {
251+
throw err;
252+
}
253+
254+
this.appLogger.error('Background event error:', err);
255+
this.appLogger.error('Args:', {
256+
id: tokenScope.workflowRuntimeDataId,
257+
name: eventName,
258+
projectIds: [tokenScope.projectId],
259+
currentProjectId: tokenScope.projectId,
260+
});
261+
await this.handleFailedEvent({ tokenScope });
262+
});
263+
264+
return {};
212265
} catch (error) {
213266
if (error instanceof CollectionFlowMissingException) {
214267
throw error;
215268
}
216269

217-
try {
218-
await this.workflowService.event(
219-
{
220-
id: tokenScope.workflowRuntimeDataId,
221-
name: BUILT_IN_EVENT.DEEP_MERGE_CONTEXT,
222-
payload: {
223-
newContext: {
224-
collectionFlow: {
225-
state: {
226-
status: CollectionFlowStatusesEnum.failed,
227-
},
228-
},
229-
},
230-
arrayMergeOption: ARRAY_MERGE_OPTION.REPLACE,
231-
},
232-
},
233-
[tokenScope.projectId],
234-
tokenScope.projectId,
235-
);
236-
} catch (error) {
237-
this.appLogger.error(error);
238-
throw new common.InternalServerErrorException(
239-
'Failed to set collection flow state as failed.',
240-
);
241-
}
270+
await this.handleFailedEvent({ tokenScope });
242271

243-
this.appLogger.error(error);
244272
throw new common.InternalServerErrorException('Failed to update collection flow state.');
245273
}
246274
}

0 commit comments

Comments
 (0)