From 65d4311e66f73e848f86b49775302898e7cdef4b Mon Sep 17 00:00:00 2001 From: Sandhya Govindaraju Date: Fri, 18 Oct 2024 16:05:28 -0500 Subject: [PATCH] FIX: no need to handle async/sync separately --- src/index.js | 19 ++++++------------- src/index.test.js | 2 ++ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 98f897e..e900425 100644 --- a/src/index.js +++ b/src/index.js @@ -39,13 +39,6 @@ let defaultGetSnapshot = form => F.flattenObject(toJS(gatherFormValues(form))) let defaultGetNestedSnapshot = form => F.unflattenObject(form.getSnapshot()) -const handleSubmitErr = (state, err) => { - if (err instanceof ValidationError) { - state.errors = err.cause - } - throw err -} - export default ({ submit: configSubmit, value = {}, @@ -185,16 +178,16 @@ export default ({ return F.getOrReturn('message', form.submit.state.error) }, }) - let submit = Command(() => { + let submit = Command(async () => { if (_.isEmpty(form.validate())) { form.submit.state.error = null try { - // Handle both sync and sync configSubmit - return Promise.resolve(configSubmit(form.getSnapshot(), form)).catch( - err => handleSubmitErr(state, err) - ) + return await configSubmit(form.getSnapshot(), form) } catch (err) { - handleSubmitErr(state, err) + if (err instanceof ValidationError) { + state.errors = { '': err.message, ...err.cause } + } + throw err } } throw 'Validation Error' diff --git a/src/index.test.js b/src/index.test.js index 8404ea4..37c0fbd 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -421,6 +421,7 @@ describe('submit()', () => { 'location.addresses.0.tenants.0': ['invalid format'], }) expect(form.errors).toEqual({ + '': 'My submit failed', 'location.addresses.0.tenants.0': ['invalid format'], }) expect(form.submitError).toBe('My submit failed') @@ -440,6 +441,7 @@ describe('submit()', () => { 'location.addresses.0.tenants.0': ['invalid format'], }) expect(form.errors).toEqual({ + '': 'My submit failed', 'location.addresses.0.tenants.0': ['invalid format'], }) expect(form.submitError).toBe('My submit failed')