Skip to content

Commit

Permalink
fix: consider more unreachable website codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sneko committed Feb 22, 2024
1 parent 90f96c7 commit 286cef4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/features/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { watchGracefulExitInLoop } from '@etabli/src/server/system';
import { getListDiff } from '@etabli/src/utils/comparaison';
import { formatArrayProgress } from '@etabli/src/utils/format';
import { containsHtml } from '@etabli/src/utils/html';
import { handleReachabilityError } from '@etabli/src/utils/request';
import { sleep } from '@etabli/src/utils/sleep';

const __root_dirname = process.cwd();
Expand Down Expand Up @@ -393,13 +394,8 @@ export async function updateRobotsTxtOnDomains() {
});

continue;
} else if (error instanceof Error && ['ENETUNREACH', 'ENOTFOUND'].includes((error.cause as FetchError)?.code || '')) {
// The server is unreachable, since the route may be broken temporarily we skip the domain to be reprocessed next time
} else if (error instanceof Error && (error.cause as any)?.code === 'HPE_INVALID_HEADER_TOKEN') {
// Note: HTTPParserError is not a known type so casting manually (I don't even understand why I can find it on internet... only in a Ruby project)
// Some websites return wrongly formatted headers (like https://mesads.beta.gouv.fr/robots.txt due to the provider Clever Cloud)
// Which makes `fetch()` failing with `HPE_INVALID_HEADER_TOKEN ... Invalid header value char`. We just skip this domain
// because it may be fixed in the future. Also, there is no easy way into new Node.js versions to disable this check.
} else if (error instanceof Error) {
handleReachabilityError(error);
} else {
throw error;
}
Expand Down Expand Up @@ -445,10 +441,15 @@ export async function updateWildcardCertificateOnDomains() {
);

request.on('error', (error) => {
if (error instanceof Error && ['ENETUNREACH', 'ENOTFOUND'].includes((error as FetchError).code || '')) {
// The server is unreachable, since the route may be broken temporarily we skip the domain to be reprocessed next time
resolve(null);
} else {
try {
if (error instanceof Error) {
handleReachabilityError(error);

resolve(null);
} else {
throw error;
}
} catch (catcherError) {
reject(error);
}
});
Expand Down
22 changes: 22 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// When it's an error related to the remote server or the connection we skip it since we have no control over it
export function handleReachabilityError(error: Error) {
if (
![
// The server is unreachable, since the route may be broken temporarily we skip the domain to be reprocessed next time
'ECONNREFUSED',
'ECONNRESET',
'ENETUNREACH',
'ENOTFOUND',
'UND_ERR_SOCKET',
'UND_ERR_CONNECT_TIMEOUT',
// Some websites return wrongly formatted headers (like https://mesads.beta.gouv.fr/robots.txt due to the provider Clever Cloud)
// Which makes `fetch()` failing with `HPE_INVALID_HEADER_TOKEN ... Invalid header value char`.
'HPE_INVALID_HEADER_TOKEN',
// Hostname/IP does not match certificate's altnames (which makes the certificate invalid)
'ERR_TLS_CERT_ALTNAME_INVALID',
'DEPTH_ZERO_SELF_SIGNED_CERT',
].includes((error.cause as any)?.code || '')
) {
throw error;
}
}

0 comments on commit 286cef4

Please sign in to comment.