Skip to content

Commit 55545e0

Browse files
committed
Better format error logs and add error descriptions to the output file
1 parent 0ea0f46 commit 55545e0

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

scripts/checkDeadLinks.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,56 @@ async function processFile(filePath, excludePatterns, deadLinks, counters) {
7373
counters.totalLinks += 1;
7474
const result = await checkLink(link);
7575

76+
const relativeFilePath = path.relative(process.cwd(), filePath);
77+
7678
if (result.error) {
77-
console.log(`🔴 Dead link found: ${link} (Status code: ${result.status_code || 'N/A'}) in file: ${filePath}`);
79+
const errorDescription = getErrorDescription(result.status_code);
80+
console.log(`🔴 [${result.status_code || 'N/A'}] ${link} in file: ${relativeFilePath}`);
7881
deadLinks.push({
7982
link: result.link,
8083
status_code: result.status_code,
81-
file: filePath,
82-
error: result.error
84+
file: relativeFilePath,
85+
error: result.error,
86+
error_description: errorDescription
8387
});
8488
counters.deadLinks += 1;
8589
} else {
86-
console.log(`✅ Working link: ${link} in file: ${filePath}`);
90+
console.log(`✅ [OK] ${link} in file: ${relativeFilePath}`);
8791
}
8892
}
8993
} catch (error) {
9094
console.error(`❌ Error processing file ${filePath}: ${error.message}`);
9195
}
9296
}
9397

98+
/**
99+
* Get the description of the HTTP status code.
100+
* @param {number} statusCode - The HTTP status code.
101+
* @returns {string} - The description of the status code.
102+
*/
103+
function getErrorDescription(statusCode) {
104+
const descriptions = {
105+
400: 'Bad Request - The server could not understand the request.',
106+
401: 'Unauthorized - Authentication is required and has failed or has not been provided.',
107+
403: 'Forbidden - The server understood the request, but refuses to authorize it.',
108+
404: 'Not Found - The server cannot find the requested resource.',
109+
500: 'Internal Server Error - The server has encountered a situation it doesn\'t know how to handle.',
110+
502: 'Bad Gateway - The server was acting as a gateway or proxy and received an invalid response.',
111+
503: 'Service Unavailable - The server is not ready to handle the request.',
112+
504: 'Gateway Timeout - The server was acting as a gateway or proxy and did not receive a response in time.',
113+
405: 'Method Not Allowed - The method specified in the Request-Line is not allowed for the resource.',
114+
429: 'Too Many Requests - The user has sent too many requests in a given amount of time.',
115+
503: 'Service Unavailable - The server is currently unavailable (overloaded or down).',
116+
504: 'Gateway Timeout - The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.',
117+
505: 'HTTP Version Not Supported - The server does not support, or refuses to support, the major version of HTTP that was used in the request message.',
118+
511: 'Network Authentication Required - The client needs to authenticate to gain network access.',
119+
599: 'Network Connect Timeout Error - Can be cause by a network issue or a server-side issue.',
120+
// Add more descriptions as needed
121+
};
122+
123+
return descriptions[statusCode] || 'Unknown error, look up status code for details';
124+
}
125+
94126
/**
95127
* Main function to execute the link checking process.
96128
*/

0 commit comments

Comments
 (0)