From f26956f64d5c7bef48f19b643ac12bfd279456b2 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Tue, 26 Nov 2019 21:38:22 +0100 Subject: [PATCH] Add more meaningful error message when source isn't JSON --- .../src/controller/useCreateController.ts | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/ra-core/src/controller/useCreateController.ts b/packages/ra-core/src/controller/useCreateController.ts index 0fb896f5006..d95178573d0 100644 --- a/packages/ra-core/src/controller/useCreateController.ts +++ b/packages/ra-core/src/controller/useCreateController.ts @@ -147,12 +147,24 @@ const useCreateController = (props: CreateProps): CreateControllerProps => { export default useCreateController; -export const getRecord = ({ state, search }, record: any = {}) => - state && state.record - ? state.record - : search - ? JSON.parse(parse(search).source) - : record; +export const getRecord = ({ state, search }, record: any = {}) => { + if (state && state.record) { + return state.record; + } + if (search) { + try { + const searchParams = parse(search); + if (searchParams.source) { + return JSON.parse(searchParams.source); + } + } catch (e) { + console.error( + `Failed to parse location search parameter '${search}'. To pre-fill some fields in the Create form, pass a stringified source parameter (e.g. '?source={"title":"foo"}')` + ); + } + } + return record; +}; const getDefaultRedirectRoute = (hasShow, hasEdit) => { if (hasEdit) {