nit: improved error messages
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alec Larson · committed by Alec Larson · parent 02fed2c · Browse files · 1 file·+17 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−10 removed
- other:+17 added−10 removed
Provenance
- Commit
a951f40- Tree
2cabf84- Author
Alec Larson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
02fed2c
1 file changed+17 added−10 removed
modified:src/immer.js+17 added-10 removed
45 unmodified lines46 }47 48 // prettier-ignore49 {50- if (typeof recipe !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")51- if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")50+ if (typeof recipe !== "function") {51+ throw new Error("The first or second argument to `produce` must be a function")52+ }53+ if (patchListener !== undefined && typeof patchListener !== "function") {54+ throw new Error("The third argument to `produce` must be a function or undefined")55+ }56 }57 58 let result59 30 unmodified lines90 return result !== NOTHING ? result : undefined91 }92 }93 createDraft(base) {90- if (!isDraftable(base)) throw new Error("First argument to createDraft should be a plain object, an array, or an immerable object.") // prettier-ignore94+ if (!isDraftable(base)) {95+ throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore96+ }97 const scope = ImmerScope.enter()98 const proxy = this.createProxy(base)99+ proxy[DRAFT_STATE].isManual = true100 scope.leave()94- proxy[DRAFT_STATE].customDraft = true101 return proxy102 }103 finishDraft(draft, patchListener) {98- if (!isDraft(draft)) throw new Error("First argument to finishDraft should be an object from createDraft.") // prettier-ignore99- const state = draft[DRAFT_STATE]100- if (!state.customDraft) throw new Error("The draft provided was not created using `createDraft`") // prettier-ignore101- if (state.finalized) throw new Error("The draft provided was has already been finished") // prettier-ignore102- // TODO: check if created with createDraft103- // TODO: check if not finsihed twice104+ const state = draft && draft[DRAFT_STATE]105+ if (!state || !state.isManual) {106+ throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore107+ }108+ if (state.finalized) {109+ throw new Error("The given draft is already finalized") // prettier-ignore110+ }111 const {scope} = state112 scope.usePatches(patchListener)113 return this.processResult(undefined, scope)114 }174 unmodified lines