Skip to content

Instantly share code, notes, and snippets.

@rw3iss
Last active July 3, 2025 22:57
Show Gist options
  • Save rw3iss/4e3fff05702bd10e27abf2c9cc96acb8 to your computer and use it in GitHub Desktop.
Save rw3iss/4e3fff05702bd10e27abf2c9cc96acb8 to your computer and use it in GitHub Desktop.
getInnerError
// Finds the inner-most error, returning a string.
export const getInnerError = (e, log?) => {
if (!e) return undefined;
const _log = typeof log !== 'undefined' ? (typeof log == "function" ? log : console.log) : undefined;
if (_log) _log(e);
if (typeof e == "string") return e;
if (Array.isArray(e)) return e.map(_e => getInnerError(_e, log)).join(", ");
// leave these separate so it recursesq through to each to always ensure a message can be found.
return (
getInnerError(e.error, log) ||
getInnerError(e.errors, log) ||
getInnerError(e.data, log) ||
getInnerError(e.response, log) ||
getInnerError(e.message, log) ||
getInnerError(e.result, log) ||
getInnerError(e.reason, log) ||
"Unknown error."
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment