Skip to content

Commit 2aa1ede

Browse files
committed
surface Docker Hub OIDC error responses
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent abd2ef4 commit 2aa1ede

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

__tests__/dockerhub.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,14 @@ describe('getOIDCToken', () => {
126126
expect(core.info).toHaveBeenCalledWith('Docker Hub OIDC token request rate limited, retrying in 0ms (attempt 1/5)');
127127
});
128128

129-
test('throws Docker Hub API errors', async () => {
130-
postSpy.mockResolvedValue(httpResponse(400, JSON.stringify({description: 'bad connection'})));
131-
await expect(dockerhub.getOIDCToken('docker.io', 'dbowie')).rejects.toThrow('Docker Hub API: bad status code 400: bad connection');
129+
test('throws Docker Hub OIDC error responses', async () => {
130+
postSpy.mockResolvedValue(httpResponse(400, JSON.stringify({error: 'invalid_request', error_description: 'bad connection', error_uri: 'https://docs.docker.com'})));
131+
await expect(dockerhub.getOIDCToken('docker.io', 'dbowie')).rejects.toThrow('Docker Hub API: bad status code 400: {"error":"invalid_request","error_description":"bad connection","error_uri":"https://docs.docker.com"}');
132+
});
133+
134+
test('throws rate limited Docker Hub OIDC error response after retries', async () => {
135+
postSpy.mockResolvedValue(httpResponse(429, JSON.stringify({error: 'rate_limited', error_description: 'slow down'}), {'retry-after': '0'}));
136+
await expect(dockerhub.getOIDCToken('docker.io', 'dbowie')).rejects.toThrow('Docker Hub API: bad status code 429: {"error":"rate_limited","error_description":"slow down"}');
137+
expect(postSpy).toHaveBeenCalledTimes(6);
132138
});
133139
});

src/dockerhub.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,19 @@ const handleResponse = async (resp: httpm.HttpClientResponse): Promise<string> =
108108
};
109109

110110
const parseError = (statusCode: number, body: string): Error => {
111-
if (statusCode === 401) {
112-
throw new Error(`Docker Hub API: operation not permitted`);
113-
}
114111
if (body) {
115-
const errResp = parseErrorBody(body);
116-
for (const k of ['description', 'message', 'detail', 'error']) {
117-
if (errResp[k]) {
118-
throw new Error(`Docker Hub API: bad status code ${statusCode}: ${errResp[k]}`);
119-
}
112+
let errResp: unknown;
113+
try {
114+
errResp = JSON.parse(body);
115+
} catch {
116+
errResp = undefined;
117+
}
118+
if (errResp !== undefined) {
119+
throw new Error(`Docker Hub API: bad status code ${statusCode}: ${JSON.stringify(errResp)}`);
120120
}
121121
}
122-
throw new Error(`Docker Hub API: bad status code ${statusCode}`);
123-
};
124-
125-
const parseErrorBody = (body: string): Record<string, string> => {
126-
try {
127-
return <Record<string, string>>JSON.parse(body);
128-
} catch {
129-
return {};
122+
if (statusCode === 401) {
123+
throw new Error(`Docker Hub API: operation not permitted`);
130124
}
125+
throw new Error(`Docker Hub API: bad status code ${statusCode}`);
131126
};

0 commit comments

Comments
 (0)