Skip to content

Commit

Permalink
fix: enhance error message when image is not there (podman-desktop#3587)
Browse files Browse the repository at this point in the history
fixes podman-desktop#3496
Signed-off-by: Florent Benoit <[email protected]>
  • Loading branch information
benoitf authored Aug 21, 2023
1 parent 7025ab5 commit 4a95323
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/main/src/plugin/container-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type * as podmanDesktopAPI from '@podman-desktop/api';
import nock from 'nock';
import { LibpodDockerode } from './dockerode/libpod-dockerode.js';
import moment from 'moment';
import type { ProviderContainerConnectionInfo } from './api/provider-info.js';

/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -118,6 +119,10 @@ class TestContainerProviderRegistry extends ContainerProviderRegistry {
addContainerProvider(name: string, provider: podmanDesktopAPI.ContainerProviderConnection): void {
this.containerProviders.set(name, provider);
}

getMatchingEngineFromConnection(providerContainerConnectionInfo: ProviderContainerConnectionInfo): Dockerode {
return this.getMatchingEngineFromConnection(providerContainerConnectionInfo);
}
}

let containerRegistry: TestContainerProviderRegistry;
Expand Down Expand Up @@ -734,3 +739,32 @@ describe('listContainers', () => {
expect(container.State).toBe('running');
});
});

test('pull unknown image ', async () => {
const getMatchingEngineFromConnectionSpy = vi.spyOn(containerRegistry, 'getMatchingEngineFromConnection');

const pullMock = vi.fn();

const fakeDockerode = {
pull: pullMock,
modem: {
followProgress: vi.fn(),
},
} as unknown as Dockerode;

getMatchingEngineFromConnectionSpy.mockReturnValue(fakeDockerode);

const containerConnectionInfo = {} as ProviderContainerConnectionInfo;

// add statusCode on the error
const error = new Error('access denied');
(error as any).statusCode = 403;

pullMock.mockRejectedValue(error);

const callback = vi.fn();
// check that we have a nice error message
await expect(containerRegistry.pullImage(containerConnectionInfo, 'unknown-image', callback)).rejects.toThrow(
'access to image "unknown-image" is denied (403 error). Can also be that image does not exist',
);
});
5 changes: 5 additions & 0 deletions packages/main/src/plugin/container-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ export class ContainerProviderRegistry {

return promise;
} catch (error) {
// Provides a better error message for 403 errors
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (error instanceof Error && (error as any)?.statusCode === 403) {
error.message = `access to image "${imageName}" is denied (403 error). Can also be that image does not exist.`;
}
telemetryOptions = { error: error };
throw error;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/src/lib/image/PullImage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function pullImage() {
pullFinished = true;
} catch (error) {
const errorMessage = error.message ? error.message : error;
pullError = 'Could not connect to ' + selectedProviderConnection.name + ': ' + errorMessage;
pullError = `Error while pulling image from ${selectedProviderConnection.name}: ${errorMessage}`;
pullInProgress = false;
}
}
Expand Down

0 comments on commit 4a95323

Please sign in to comment.