errors.ts
This file
Path
src/errors.ts
48 lines · 1.7 KiB · TypeScript
Last changed by
Preserve error codes in productionMichel Weststratebefore adoptiond005fab
History of this fileTake it
Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 1–48why, one step back1declare const __DEV__: boolean23const errors = {4 0: "Illegal state",5 1: "Immer drafts cannot have computed properties",6 2: "This object has been frozen and should not be mutated",7 3(data: any) {8 return (9 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +10 data11 )12 },13 4: "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",14 5: "Immer forbids circular references",15 6: "The first or second argument to `produce` must be a function",16 7: "The third argument to `produce` must be a function or undefined",17 8: "First argument to `createDraft` must be a plain object, an array, or an immerable object",18 9: "First argument to `finishDraft` must be a draft returned by `createDraft`",19 10: "The given draft is already finalized",20 11: "Object.defineProperty() cannot be used on an Immer draft",21 12: "Object.setPrototypeOf() cannot be used on an Immer draft",22 13: "Immer only supports deleting array indices",23 14: "Immer only supports setting array indices and the 'length' property",24 15(path: string) {25 return "Cannot apply patch, path doesn't resolve: " + path26 },27 16: 'Sets cannot have "replace" patches.',28 17(op: string) {29 return "Unsupported patch operation: " + op30 }31} as const3233export function die(error: keyof typeof errors, ...args: any[]): never {34 if (__DEV__) {35 const e = errors[error]36 const msg = !e37 ? "unknown error nr: " + error38 : typeof e === "function"39 ? e.apply(null, args as any)40 : e41 throw new Error(`[Immer] ${msg}`)42 }43 throw new Error(44 `[Immer] minified error nr: ${error}${45 args.length ? " " + args.join(",") : ""46 }`47 )48}