MCPcopy Index your code
hub / github.com/nodejs/node / formatError

Function formatError

lib/internal/util/inspect.js:1933–2043  ·  view source on GitHub ↗
(err, constructor, tag, ctx, keys)

Source from the content-addressed store, hash-verified

1931}
1932
1933function formatError(err, constructor, tag, ctx, keys) {
1934 let message, name, stack;
1935 try {
1936 stack = getStackString(ctx, err);
1937 } catch {
1938 return ObjectPrototypeToString(err);
1939 }
1940
1941 let messageIsGetterThatThrows = false;
1942 try {
1943 message = err.message;
1944 } catch {
1945 messageIsGetterThatThrows = true;
1946 }
1947 let nameIsGetterThatThrows = false;
1948 try {
1949 name = err.name;
1950 } catch {
1951 nameIsGetterThatThrows = true;
1952 }
1953
1954 if (!ctx.showHidden && keys.length !== 0) {
1955 const index = ArrayPrototypeIndexOf(keys, 'stack');
1956 if (index !== -1) {
1957 ArrayPrototypeSplice(keys, index, 1);
1958 }
1959
1960 if (!messageIsGetterThatThrows) {
1961 const index = ArrayPrototypeIndexOf(keys, 'message');
1962 // Only hide the property if it's a string and if it's part of the original stack
1963 if (index !== -1 && (typeof message !== 'string' || StringPrototypeIncludes(stack, message))) {
1964 ArrayPrototypeSplice(keys, index, 1);
1965 }
1966 }
1967
1968 if (!nameIsGetterThatThrows) {
1969 const index = ArrayPrototypeIndexOf(keys, 'name');
1970 // Only hide the property if it's a string and if it's part of the original stack
1971 if (index !== -1 && (typeof name !== 'string' || StringPrototypeIncludes(stack, name))) {
1972 ArrayPrototypeSplice(keys, index, 1);
1973 }
1974 }
1975 }
1976 name ??= 'Error';
1977
1978 if (ObjectPrototypeHasOwnProperty(err, 'cause') &&
1979 (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) {
1980 ArrayPrototypePush(keys, 'cause');
1981 }
1982
1983 // Print errors aggregated into AggregateError
1984 try {
1985 const errors = err.errors;
1986 if (ArrayIsArray(errors) && ObjectPrototypeHasOwnProperty(err, 'errors') &&
1987 (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) {
1988 ArrayPrototypePush(keys, 'errors');
1989 }
1990 } catch {

Callers 1

formatRawFunction · 0.70

Calls 8

getStackStringFunction · 0.85
improveStackFunction · 0.85
getStackFramesFunction · 0.85
safeGetCWDFunction · 0.85
markNodeModulesFunction · 0.85
markCwdFunction · 0.85
pathToFileUrlHrefFunction · 0.85
existsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…