UNPKG

2.18 kBPlain TextView Raw
1export const errors =
2 process.env.NODE_ENV !== "production"
3 ? [
4 // All error codes, starting by 0:
5 function(plugin: string) {
6 return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`
7 },
8 function(thing: string) {
9 return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`
10 },
11 "This object has been frozen and should not be mutated",
12 function(data: any) {
13 return (
14 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
15 data
16 )
17 },
18 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
19 "Immer forbids circular references",
20 "The first or second argument to `produce` must be a function",
21 "The third argument to `produce` must be a function or undefined",
22 "First argument to `createDraft` must be a plain object, an array, or an immerable object",
23 "First argument to `finishDraft` must be a draft returned by `createDraft`",
24 function(thing: string) {
25 return `'current' expects a draft, got: ${thing}`
26 },
27 "Object.defineProperty() cannot be used on an Immer draft",
28 "Object.setPrototypeOf() cannot be used on an Immer draft",
29 "Immer only supports deleting array indices",
30 "Immer only supports setting array indices and the 'length' property",
31 function(thing: string) {
32 return `'original' expects a draft, got: ${thing}`
33 }
34 // Note: if more errors are added, the errorOffset in Patches.ts should be increased
35 // See Patches.ts for additional errors
36 ]
37 : []
38
39export function die(error: number, ...args: any[]): never {
40 if (process.env.NODE_ENV !== "production") {
41 const e = errors[error]
42 const msg = typeof e === "function" ? e.apply(null, args as any) : e
43 throw new Error(`[Immer] ${msg}`)
44 }
45 throw new Error(
46 `[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`
47 )
48}