Skip to content

Commit 6a11cfa

Browse files
lgrammeldevelopaul
andauthoredJul 8, 2024··
feat (ai/ui): add onError handler to useObject (#2207)
Co-authored-by: Paul Chavez <paulchavezromero20@gmail.com>
1 parent abb2260 commit 6a11cfa

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed
 

‎.changeset/beige-otters-give.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/react': patch
3+
---
4+
5+
feat (ai/ui): add onError handler to useObject

‎content/docs/07-reference/ai-sdk-ui/03-use-object.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export default function Page() {
7474
description:
7575
'Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.',
7676
},
77+
{
78+
name: 'onError',
79+
type: '(error: Error) => void',
80+
optional: true,
81+
description:
82+
'Optional. Callback function to be called when an error is encountered.',
83+
},
7784
]}
7885
/>
7986

‎packages/react/src/use-object.ts

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ Custom fetch implementation. You can use it as a middleware to intercept request
4040
or to provide a custom fetch implementation for e.g. testing.
4141
*/
4242
fetch?: FetchFunction;
43+
44+
/**
45+
* Callback function to be called when an error is encountered.
46+
*/
47+
onError?: (error: Error) => void;
4348
};
4449

4550
export type Experimental_UseObjectHelpers<RESULT, INPUT> = {
@@ -80,6 +85,7 @@ function useObject<RESULT, INPUT = any>({
8085
schema, // required, in the future we will use it for validation
8186
initialValue,
8287
fetch,
88+
onError,
8389
}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<
8490
RESULT,
8591
INPUT
@@ -167,6 +173,10 @@ function useObject<RESULT, INPUT = any>({
167173
return;
168174
}
169175

176+
if (onError && error instanceof Error) {
177+
onError(error);
178+
}
179+
170180
setError(error);
171181
}
172182
};

‎packages/react/src/use-object.ui.test.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ import { z } from 'zod';
99
import { experimental_useObject } from './use-object';
1010

1111
describe('text stream', () => {
12+
let onErrorResult: Error | undefined;
13+
1214
const TestComponent = () => {
1315
const { object, error, submit, isLoading, stop } = experimental_useObject({
1416
api: '/api/use-object',
1517
schema: z.object({ content: z.string() }),
18+
onError(error) {
19+
onErrorResult = error;
20+
},
1621
});
1722

1823
return (
@@ -35,6 +40,7 @@ describe('text stream', () => {
3540

3641
beforeEach(() => {
3742
render(<TestComponent />);
43+
onErrorResult = undefined;
3844
});
3945

4046
afterEach(() => {
@@ -68,6 +74,7 @@ describe('text stream', () => {
6874
it('should not have an error', async () => {
6975
await screen.findByTestId('error');
7076
expect(screen.getByTestId('error')).toBeEmptyDOMElement();
77+
expect(onErrorResult).toBeUndefined();
7178
});
7279
},
7380
);
@@ -155,6 +162,7 @@ describe('text stream', () => {
155162

156163
await screen.findByTestId('error');
157164
expect(screen.getByTestId('error')).toHaveTextContent('Not found');
165+
expect(onErrorResult).toBeInstanceOf(Error);
158166
},
159167
),
160168
);

0 commit comments

Comments
 (0)
Please sign in to comment.