(error: unknown)
| 145 | } |
| 146 | |
| 147 | export function errorData(error: unknown) { |
| 148 | if (error instanceof Error) { |
| 149 | return { |
| 150 | type: error.name, |
| 151 | message: errorMessage(error), |
| 152 | stack: error.stack, |
| 153 | cause: error.cause === undefined ? undefined : errorFormat(error.cause), |
| 154 | formatted: errorFormat(error), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (!isRecord(error)) { |
| 159 | return { |
| 160 | type: typeof error, |
| 161 | message: errorMessage(error), |
| 162 | formatted: errorFormat(error), |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | const data = Object.getOwnPropertyNames(error).reduce<Record<string, unknown>>((acc, key) => { |
| 167 | const value = error[key] |
| 168 | if (value === undefined) return acc |
| 169 | if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { |
| 170 | acc[key] = value |
| 171 | return acc |
| 172 | } |
| 173 | // oxlint-disable-next-line no-base-to-string -- intentional coercion of arbitrary error properties |
| 174 | acc[key] = value instanceof Error ? value.message : String(value) |
| 175 | return acc |
| 176 | }, {}) |
| 177 | |
| 178 | if (typeof data.message !== "string") data.message = errorMessage(error) |
| 179 | if (typeof data.type !== "string") data.type = error.constructor?.name |
| 180 | data.formatted = errorFormat(error) |
| 181 | return data |
| 182 | } |
no test coverage detected