{"id":"042d92914912f967","repo":"sindresorhus/is","slug":"expected-value-which-is-error-received-value-of","errorCode":null,"errorMessage":"Expected value which is `Error`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `Error`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1598,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('empty string', value));\n\t}\n}\n\nexport function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {\n\tif (!isEmptyStringOrWhitespace(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));\n\t}\n}\n\nexport function assertEnumCase<T = unknown>(value: unknown, targetEnum: T, message?: string): asserts value is T[keyof T] {\n\tif (!isEnumCase(value, targetEnum)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('EnumCase', value));\n\t}\n}\n\nexport function assertError(value: unknown, message?: string): asserts value is Error {\n\tif (!isError(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Error', value));\n\t}\n}\n\nexport function assertEvenInteger(value: number, message?: string): asserts value is number {\n\tif (!isEvenInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('even integer', value));\n\t}\n}\n\nexport function assertFalsy(value: unknown, message?: string): asserts value is Falsy {\n\tif (!isFalsy(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('falsy', value));\n\t}\n}\n\nexport function assertFiniteNumber(value: unknown, message?: string): asserts value is number {\n\tif (!isFiniteNumber(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('finite number', value));","sourceCodeStart":1580,"sourceCodeEnd":1616,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1580-L1616","documentation":"Thrown by `assertError` (source/index.ts:1596) when the value is not an `Error` instance (including subclasses like `TypeError`). It's commonly used in catch blocks, since TypeScript types caught values as `unknown`; if code throws a non-Error (string, plain object), this assertion rejects it with a TypeError naming the actual type.","triggerScenarios":"Calling `assert.error(value)` on a caught value that was thrown as a string or plain object, on an error-like object deserialized from JSON, or on an Error crossing realm boundaries (vm, iframe, worker) if the check relies on instanceof semantics.","commonSituations":"`catch (error) { assert.error(error); }` failing because third-party code does `throw 'message'` or rejects a promise with a plain object; error objects serialized over IPC/HTTP arriving as plain objects; axios/fetch libraries rejecting with custom non-Error shapes.","solutions":["Find and fix code that throws non-Error values — always `throw new Error(...)`","When consuming third-party rejections, normalize first: `error instanceof Error ? error : new Error(String(error))`","Rehydrate serialized errors (from JSON/IPC) into real Error instances before asserting","Use `is.error(value)` to branch gracefully instead of throwing on unknown rejection shapes"],"exampleFix":"// before\ntry { doWork(); } catch (error) { assert.error(error); } // throws if a string was thrown\n// after\ntry { doWork(); } catch (error) {\n  const err = error instanceof Error ? error : new Error(String(error));\n  assert.error(err);\n}","handlingStrategy":"type-guard","validationCode":"if (value instanceof Error) {\n  assert.error(value);\n}","typeGuard":"function isError(value: unknown): value is Error {\n  return value instanceof Error || Object.prototype.toString.call(value) === '[object Error]';\n}","tryCatchPattern":"try {\n  assert.error(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // caught value was not an Error — someone threw a string/object; wrap it: new Error(String(value))\n  } else throw error;\n}","preventionTips":["In catch blocks the caught value is unknown — never assume it's an Error; normalize with value instanceof Error ? value : new Error(String(value))","instanceof Error fails across realms (iframes, worker boundaries) — use a toString-based check if that applies","Always throw Error instances (or subclasses) in your own code, never strings"],"tags":["type-assertion","runtime-validation","error-handling","typescript"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}