Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui): handle trace not being available without exceptions #33427

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/trace-viewer/src/sw/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async function loadTrace(traceUrl: string, traceFileName: string | null, clientI
const backend = traceUrl.endsWith('json') ? new FetchTraceModelBackend(traceUrl) : new ZipTraceModelBackend(traceUrl, fetchProgress);
await traceModel.load(backend, unzipProgress);
} catch (error: any) {
if (error?.message === 'trace not found')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you are mostly addressing the malformed (empty) JSON in live mode. Should we check that at parse time and return empty trac instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice idea as well, but I think it'd be more complex. Just one example, we're treating empty traces as an error while parsing:

if (!ordinals.length)
throw new Error('Cannot find .trace file');

throw error;
// eslint-disable-next-line no-console
console.error(error);
if (error?.message?.includes('Cannot find .trace file') && await traceModel.hasEntry('index.html'))
Expand Down Expand Up @@ -106,6 +108,8 @@ async function doFetch(event: FetchEvent): Promise<Response> {
headers: { 'Content-Type': 'application/json' }
});
} catch (error: any) {
if (error?.message === 'trace not found')
return new Response(null, { status: 404 });
return new Response(JSON.stringify({ error: error?.message }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
Expand Down
2 changes: 2 additions & 0 deletions packages/trace-viewer/src/sw/traceModelBackends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class FetchTraceModelBackend implements TraceModelBackend {
constructor(traceURL: string) {
this._traceURL = traceURL;
this._entriesPromise = fetch(traceURL).then(async response => {
if (response.status === 404)
throw new Error(`trace not found`);
const json = await response.json();
const entries = new Map<string, string>();
for (const entry of json.entries)
Expand Down
18 changes: 12 additions & 6 deletions packages/trace-viewer/src/ui/uiModeTraceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const TraceView: React.FC<{
pathSeparator: string,
}> = ({ item, rootDir, onOpenExternally, revealSource, pathSeparator }) => {
const [model, setModel] = React.useState<{ model: MultiTraceModel, isLive: boolean } | undefined>();
const [counter, setCounter] = React.useState(0);
const [rerender, setRerender] = React.useState(0);
const pollTimer = React.useRef<NodeJS.Timeout | null>(null);

const { outputDir } = React.useMemo(() => {
Expand All @@ -54,7 +54,10 @@ export const TraceView: React.FC<{
// Test finished.
const attachment = result && result.duration >= 0 && result.attachments.find(a => a.name === 'trace');
if (attachment && attachment.path) {
loadSingleTraceFile(filePathToTraceURL(attachment.path)).then(model => setModel({ model, isLive: false }));
loadSingleTraceFile(filePathToTraceURL(attachment.path)).then(model => {
if (model)
setModel({ model, isLive: false });
});
return;
}

Expand All @@ -73,18 +76,19 @@ export const TraceView: React.FC<{
pollTimer.current = setTimeout(async () => {
try {
const model = await loadSingleTraceFile(filePathToTraceURL(traceLocation));
setModel({ model, isLive: true });
if (model)
setModel({ model, isLive: true });
} catch {
setModel(undefined);
} finally {
setCounter(counter + 1);
setRerender(rerender + 1);
}
}, 500);
return () => {
if (pollTimer.current)
clearTimeout(pollTimer.current);
};
}, [outputDir, item, setModel, counter, setCounter, pathSeparator]);
}, [outputDir, item, setModel, rerender, setRerender, pathSeparator]);

return <Workbench
key='workbench'
Expand All @@ -108,11 +112,13 @@ const outputDirForTestCase = (testCase: reporterTypes.TestCase): string | undefi
return undefined;
};

async function loadSingleTraceFile(traceURL: URL): Promise<MultiTraceModel> {
async function loadSingleTraceFile(traceURL: URL): Promise<MultiTraceModel | undefined> {
const params = new URLSearchParams();
params.set('trace', formatUrl(traceURL).toString());
params.set('limit', '1');
const response = await fetch(`contexts?${params.toString()}`);
if (response.status === 404)
return;
const contextEntries = await response.json() as ContextEntry[];
return new MultiTraceModel(contextEntries);
}
Expand Down
Loading