Skip to content

Commit

Permalink
Add more meaningful error message when source isn't JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Nov 26, 2019
1 parent 865afca commit f26956f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/ra-core/src/controller/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f26956f

Please sign in to comment.