fix: changed the error message for registry timeout to be more usefull

This commit is contained in:
Mateusz Matejuk 2026-02-26 16:17:24 +01:00
parent 3227f5311c
commit 62746db9fb
No known key found for this signature in database
GPG key ID: 133E967D0378BE8C
2 changed files with 60 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import {expect, jest, test} from '@jest/globals';
import * as path from 'path'; import * as path from 'path';
import {loginStandard, logout} from '../src/docker'; import {loginStandard, logout} from '../src/docker';
import * as core from '@actions/core';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker'; import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
@ -37,6 +38,50 @@ test('loginStandard calls exec', async () => {
}); });
}); });
test('loginStandard throws plain error on auth failure', async () => {
jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return {
exitCode: 1,
stdout: '',
stderr: 'Error response from daemon: unauthorized: incorrect username or password'
};
});
await expect(loginStandard('https://ghcr.io', 'user', 'wrongpass')).rejects.toThrow(
'Error response from daemon: unauthorized: incorrect username or password'
);
});
test('loginStandard appends network hint on timeout error', async () => {
jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return {
exitCode: 1,
stdout: '',
stderr: 'Error response from daemon: Get "https://ghcr.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)'
};
});
jest.spyOn(core, 'warning').mockImplementation(() => undefined);
await expect(loginStandard('https://ghcr.io', 'user', 'pass')).rejects.toThrow(
/context deadline exceeded[\s\S]*Hint: looks like a network connectivity issue/
);
});
test('loginStandard appends network hint on dial tcp error', async () => {
jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
return {
exitCode: 1,
stdout: '',
stderr: 'Error response from daemon: Get "https://ghcr.io/v2/": dial tcp 140.82.112.21:443: i/o timeout'
};
});
jest.spyOn(core, 'warning').mockImplementation(() => undefined);
await expect(loginStandard('https://ghcr.io', 'user', 'pass')).rejects.toThrow(
/dial tcp[\s\S]*Hint: looks like a network connectivity issue/
);
});
test('logout calls exec', async () => { test('logout calls exec', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore

View file

@ -74,8 +74,22 @@ async function loginExec(registry: string, username: string, password: string, s
env: envs env: envs
}).then(res => { }).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) { if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim()); const errMsg = res.stderr.trim();
// if the docker daemon cannot reach the registry at all, user should get a more useful hint
// rather than just forwarding the raw timeout/dial error
if (isNetworkError(errMsg)) {
throw new Error(
`${errMsg}\n\nHint: looks like a network connectivity issue - verify the runner can reach ${registry} (check firewall rules, proxy settings, and DNS resolution).`
);
}
throw new Error(errMsg);
} }
core.info('Login Succeeded!'); core.info('Login Succeeded!');
}); });
} }
// checks if a docker daemon error msg looks like a connectivity/timeout problem
// vs an actual auth failure or other non-network error
function isNetworkError(msg: string): boolean {
return /context deadline exceeded|request canceled|i\/o timeout|dial tcp|connection refused|no such host/.test(msg);
}