Skip to content

Commit a0edc17

Browse files
authored
Merge pull request #144 from import-ai/fix/task_callback
fix(task): Fix task callback with empty but not null exception
2 parents c982c7e + 28bc9fc commit a0edc17

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/utils/is-empty.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isEmpty } from './is-empty';
2+
3+
describe('isEmpty', () => {
4+
test.each([
5+
// Empty values - should return true
6+
['', true, 'empty string'],
7+
[[], true, 'empty array'],
8+
[{}, true, 'empty object'],
9+
[null, true, 'empty object'],
10+
[undefined, true, 'empty object'],
11+
12+
// Non-empty values - should return false
13+
['hello', false, 'non-empty string'],
14+
[' ', false, 'string with space'],
15+
['0', false, 'string with zero'],
16+
[[1], false, 'array with one element'],
17+
[[1, 2, 3], false, 'array with multiple elements'],
18+
[[null], false, 'array with null element'],
19+
[[undefined], false, 'array with undefined element'],
20+
[{ a: 1 }, false, 'object with property'],
21+
[{ a: null }, false, 'object with null value'],
22+
[{ a: undefined }, false, 'object with undefined value'],
23+
])('should return %s for %s (%s)', (input, expected, description) => {
24+
expect(isEmpty(input)).toBe(expected);
25+
});
26+
});

src/utils/is-empty.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Checks if a string, array, or record is empty.
3+
*
4+
* @param value - The value to check (string, array, or record)
5+
* @returns true if the value is empty, false otherwise
6+
*
7+
* @example
8+
* isEmpty('') // true
9+
* isEmpty([]) // true
10+
* isEmpty({}) // true
11+
* isEmpty('hello') // false
12+
* isEmpty([1, 2, 3]) // false
13+
* isEmpty({ a: 1 }) // false
14+
*/
15+
export function isEmpty(
16+
value: string | any[] | Record<string, any> | undefined | null,
17+
): boolean {
18+
if (value === undefined || value === null) {
19+
return true; // null, undefined are considered empty
20+
}
21+
if (typeof value === 'string' || Array.isArray(value)) {
22+
return value.length === 0;
23+
}
24+
25+
if (typeof value === 'object' && value.constructor === Object) {
26+
return Object.keys(value).length === 0;
27+
}
28+
29+
throw new TypeError(
30+
'Expected a string, array, or record, but received: ' + typeof value,
31+
);
32+
}

src/wizard/processors/collect.processor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ResourcesService } from 'omniboxd/resources/resources.service';
22
import { Task } from 'omniboxd/tasks/tasks.entity';
33
import { BadRequestException } from '@nestjs/common';
44
import { Processor } from 'omniboxd/wizard/processors/processor.abstract';
5+
import { isEmpty } from 'omniboxd/utils/is-empty';
56

67
export class CollectProcessor extends Processor {
78
constructor(protected readonly resourcesService: ResourcesService) {
@@ -13,7 +14,7 @@ export class CollectProcessor extends Processor {
1314
if (!resourceId) {
1415
throw new BadRequestException('Invalid task payload');
1516
}
16-
if (task.exception) {
17+
if (task.exception && !isEmpty(task.exception)) {
1718
await this.resourcesService.update(task.userId, resourceId, {
1819
namespaceId: task.namespaceId,
1920
content: task.exception.error,

src/wizard/processors/reader.processor.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ describe('ReaderProcessor', () => {
387387
expect(attachmentsService.uploadAttachment).toHaveBeenCalled();
388388
expect(task.output!.images).toBeUndefined();
389389
});
390+
391+
it('empty dict exception', async () => {
392+
const task = createMockTask({
393+
exception: {},
394+
output: {
395+
markdown: '# Test Document',
396+
},
397+
});
398+
399+
resourcesService.get.mockResolvedValue(mockResource as Resource);
400+
resourcesService.update.mockResolvedValue(undefined);
401+
402+
const result = await processor.process(task);
403+
expect(result).toEqual({ resourceId: 'test-resource-id' });
404+
});
390405
});
391406
});
392407
});

0 commit comments

Comments
 (0)