Skip to content

Commit

Permalink
feat (ai/ui): add onError handler to useObject (#2207)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Chavez <[email protected]>
  • Loading branch information
lgrammel and developaul committed Jul 8, 2024
1 parent abb2260 commit 6a11cfa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-otters-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/react': patch
---

feat (ai/ui): add onError handler to useObject
7 changes: 7 additions & 0 deletions content/docs/07-reference/ai-sdk-ui/03-use-object.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export default function Page() {
description:
'Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.',
},
{
name: 'onError',
type: '(error: Error) => void',
optional: true,
description:
'Optional. Callback function to be called when an error is encountered.',
},
]}
/>

Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/use-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Custom fetch implementation. You can use it as a middleware to intercept request
or to provide a custom fetch implementation for e.g. testing.
*/
fetch?: FetchFunction;

/**
* Callback function to be called when an error is encountered.
*/
onError?: (error: Error) => void;
};

export type Experimental_UseObjectHelpers<RESULT, INPUT> = {
Expand Down Expand Up @@ -80,6 +85,7 @@ function useObject<RESULT, INPUT = any>({
schema, // required, in the future we will use it for validation
initialValue,
fetch,
onError,
}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<
RESULT,
INPUT
Expand Down Expand Up @@ -167,6 +173,10 @@ function useObject<RESULT, INPUT = any>({
return;
}

if (onError && error instanceof Error) {
onError(error);
}

setError(error);
}
};
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/use-object.ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { z } from 'zod';
import { experimental_useObject } from './use-object';

describe('text stream', () => {
let onErrorResult: Error | undefined;

const TestComponent = () => {
const { object, error, submit, isLoading, stop } = experimental_useObject({
api: '/api/use-object',
schema: z.object({ content: z.string() }),
onError(error) {
onErrorResult = error;
},
});

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

beforeEach(() => {
render(<TestComponent />);
onErrorResult = undefined;
});

afterEach(() => {
Expand Down Expand Up @@ -68,6 +74,7 @@ describe('text stream', () => {
it('should not have an error', async () => {
await screen.findByTestId('error');
expect(screen.getByTestId('error')).toBeEmptyDOMElement();
expect(onErrorResult).toBeUndefined();
});
},
);
Expand Down Expand Up @@ -155,6 +162,7 @@ describe('text stream', () => {

await screen.findByTestId('error');
expect(screen.getByTestId('error')).toHaveTextContent('Not found');
expect(onErrorResult).toBeInstanceOf(Error);
},
),
);
Expand Down

0 comments on commit 6a11cfa

Please sign in to comment.