mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
26 lines
936 B
JavaScript
26 lines
936 B
JavaScript
/**
|
|
* Extract a human-readable error message from any thrown value.
|
|
* Handles Error objects, strings, and plain objects with message/error fields.
|
|
* @param {*} error — caught exception value
|
|
* @returns {string} best-effort error description
|
|
*/
|
|
export function describeError(error) {
|
|
if (!error) return 'Unknown initialization error';
|
|
if (error instanceof Error) {
|
|
if (error.message && error.message.trim()) return error.message.trim();
|
|
return error.name || 'Initialization error';
|
|
}
|
|
if (typeof error === 'string' && error.trim()) return error.trim();
|
|
if (typeof error === 'object') {
|
|
const maybeMessage = String(error.message || error.error || '').trim();
|
|
if (maybeMessage) return maybeMessage;
|
|
try {
|
|
const serialized = JSON.stringify(error);
|
|
if (serialized && serialized !== '{}') return serialized;
|
|
} catch {
|
|
// ignore serialization error
|
|
}
|
|
}
|
|
return String(error);
|
|
}
|