hypertune
Version:
[Hypertune](https://www.hypertune.com/) is the most flexible platform for feature flags, A/B testing, analytics and app configuration. Built with full end-to-end type-safety, Git-style version control and local, synchronous, in-memory flag evaluation. Opt
22 lines (19 loc) • 624 B
text/typescript
export default function asError(value: unknown): Error {
if (value instanceof Error) {
return value;
}
if (typeof value === "string") {
return new Error(value);
}
try {
const stringified = JSON.stringify(value);
if (typeof stringified !== "string") {
// Can happen in some edge cases, e.g. JSON.stringify(() => {})
return new Error("Error that stringified to non-string value.");
}
return new Error(stringified);
} catch (error) {
// Indicates value was non-stringifyable, e.g. recursive object, BigInt
return new Error("Error that couldn't be stringified.");
}
}