fix(storageState): provide nice error message for storage state issues (#13019)
This commit is contained in:
parent
3688e74e3e
commit
91408f2c5e
|
|
@ -38,6 +38,7 @@ import type { BrowserType } from './browserType';
|
||||||
import { Artifact } from './artifact';
|
import { Artifact } from './artifact';
|
||||||
import { APIRequestContext } from './fetch';
|
import { APIRequestContext } from './fetch';
|
||||||
import { createInstrumentation } from './clientInstrumentation';
|
import { createInstrumentation } from './clientInstrumentation';
|
||||||
|
import { rewriteErrorMessage } from '../utils/stackTrace';
|
||||||
|
|
||||||
export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel> implements api.BrowserContext {
|
export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel> implements api.BrowserContext {
|
||||||
_pages = new Set<Page>();
|
_pages = new Set<Page>();
|
||||||
|
|
@ -347,6 +348,17 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function prepareStorageState(options: BrowserContextOptions): Promise<channels.BrowserNewContextParams['storageState']> {
|
||||||
|
if (typeof options.storageState !== 'string')
|
||||||
|
return options.storageState;
|
||||||
|
try {
|
||||||
|
return JSON.parse(await fs.promises.readFile(options.storageState, 'utf8'));
|
||||||
|
} catch (e) {
|
||||||
|
rewriteErrorMessage(e, `Error reading storage state from ${options.storageState}:\n` + e.message);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function prepareBrowserContextParams(options: BrowserContextOptions): Promise<channels.BrowserNewContextParams> {
|
export async function prepareBrowserContextParams(options: BrowserContextOptions): Promise<channels.BrowserNewContextParams> {
|
||||||
if (options.videoSize && !options.videosPath)
|
if (options.videoSize && !options.videosPath)
|
||||||
throw new Error(`"videoSize" option requires "videosPath" to be specified`);
|
throw new Error(`"videoSize" option requires "videosPath" to be specified`);
|
||||||
|
|
@ -357,7 +369,7 @@ export async function prepareBrowserContextParams(options: BrowserContextOptions
|
||||||
viewport: options.viewport === null ? undefined : options.viewport,
|
viewport: options.viewport === null ? undefined : options.viewport,
|
||||||
noDefaultViewport: options.viewport === null,
|
noDefaultViewport: options.viewport === null,
|
||||||
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
|
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
|
||||||
storageState: typeof options.storageState === 'string' ? JSON.parse(await fs.promises.readFile(options.storageState, 'utf8')) : options.storageState,
|
storageState: await prepareStorageState(options),
|
||||||
};
|
};
|
||||||
if (!contextParams.recordVideo && options.videosPath) {
|
if (!contextParams.recordVideo && options.videosPath) {
|
||||||
contextParams.recordVideo = {
|
contextParams.recordVideo = {
|
||||||
|
|
|
||||||
|
|
@ -194,3 +194,20 @@ it('should not restore localStorage twice', async ({ contextFactory }) => {
|
||||||
|
|
||||||
await context.close();
|
await context.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle missing file', async ({ contextFactory }, testInfo) => {
|
||||||
|
const file = testInfo.outputPath('does-not-exist.json');
|
||||||
|
const error = await contextFactory({
|
||||||
|
storageState: file,
|
||||||
|
}).catch(e => e);
|
||||||
|
expect(error.message).toContain(`Error reading storage state from ${file}:\nENOENT`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle malformed file', async ({ contextFactory }, testInfo) => {
|
||||||
|
const file = testInfo.outputPath('state.json');
|
||||||
|
fs.writeFileSync(file, 'not-json', 'utf-8');
|
||||||
|
const error = await contextFactory({
|
||||||
|
storageState: file,
|
||||||
|
}).catch(e => e);
|
||||||
|
expect(error.message).toContain(`Error reading storage state from ${file}:\nUnexpected token o in JSON at position 1`);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue