Skip to content

Commit d6368c3

Browse files
committed
fix: Should console error when user onFinish script failed
1 parent 1bf9791 commit d6368c3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.storybook
22
.doc
3+
*.log
34
node_modules
45
coverage/
56
es/

src/useForm.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,12 @@ export class FormStore {
521521
.then(values => {
522522
const { onFinish } = this.callbacks;
523523
if (onFinish) {
524-
onFinish(values);
524+
try {
525+
onFinish(values);
526+
} catch (err) {
527+
// Should print error if user `onFinish` callback failed
528+
console.error(err);
529+
}
525530
}
526531
})
527532
.catch(e => {

tests/validate.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,28 @@ describe('Form.Validate', () => {
302302
await timeout();
303303
expect(onFinish).toHaveBeenCalledWith({ user: 'light' });
304304
});
305+
306+
it('should error in console if user script failed', async () => {
307+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
308+
309+
const wrapper = mount(
310+
<Form
311+
onFinish={() => {
312+
throw new Error('should console this');
313+
}}
314+
initialValues={{ user: 'light' }}
315+
>
316+
<InfoField name="user">
317+
<Input />
318+
</InfoField>
319+
</Form>,
320+
);
321+
322+
wrapper.find('form').simulate('submit');
323+
await timeout();
324+
expect(errorSpy.mock.calls[0][0].message).toEqual('should console this');
325+
326+
errorSpy.mockRestore();
327+
});
305328
});
306329
/* eslint-enable no-template-curly-in-string */

0 commit comments

Comments
 (0)