Skip to content

Commit

Permalink
Improve log parsing error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jmerle committed Apr 18, 2024
1 parent 9c63e2d commit c1900ba
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/ErrorAlert.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Alert, AlertProps } from '@mantine/core';
import { IconAlertCircle } from '@tabler/icons-react';
import { ReactNode } from 'react';
import { AlgorithmParseError } from '../utils/algorithm.tsx';

export interface ErrorAlertProps extends Partial<AlertProps> {
error: Error;
Expand All @@ -9,7 +10,7 @@ export interface ErrorAlertProps extends Partial<AlertProps> {
export function ErrorAlert({ error, ...alertProps }: ErrorAlertProps): ReactNode {
return (
<Alert icon={<IconAlertCircle size={16} />} title="Error" color="red" {...alertProps}>
{error.message}
{error instanceof AlgorithmParseError ? error.node : error.message}
</Alert>
);
}
2 changes: 1 addition & 1 deletion src/pages/home/AlgorithmDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
downloadAlgorithmResults,
getAlgorithmLogsUrl,
parseAlgorithmLogs,
} from '../../utils/algorithm.ts';
} from '../../utils/algorithm.tsx';
import { formatNumber, formatTimestamp } from '../../utils/format.ts';

export interface AlgorithmDetailProps {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/LoadFromFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNavigate } from 'react-router-dom';
import { ErrorAlert } from '../../components/ErrorAlert.tsx';
import { useAsync } from '../../hooks/use-async.ts';
import { useStore } from '../../store.ts';
import { parseAlgorithmLogs } from '../../utils/algorithm.ts';
import { parseAlgorithmLogs } from '../../utils/algorithm.tsx';
import { HomeCard } from './HomeCard.tsx';

function DropzoneContent(): ReactNode {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/LoadFromUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNavigate, useSearchParams } from 'react-router-dom';
import { ErrorAlert } from '../../components/ErrorAlert.tsx';
import { useAsync } from '../../hooks/use-async.ts';
import { useStore } from '../../store.ts';
import { parseAlgorithmLogs } from '../../utils/algorithm.ts';
import { parseAlgorithmLogs } from '../../utils/algorithm.tsx';
import { HomeCard } from './HomeCard.tsx';

export function LoadFromUrl(): ReactNode {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/visualizer/AlgorithmSummaryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactNode } from 'react';
import { ScrollableCodeHighlight } from '../../components/ScrollableCodeHighlight.tsx';
import { useAsync } from '../../hooks/use-async.ts';
import { useStore } from '../../store.ts';
import { downloadAlgorithmLogs, downloadAlgorithmResults } from '../../utils/algorithm.ts';
import { downloadAlgorithmLogs, downloadAlgorithmResults } from '../../utils/algorithm.tsx';
import { formatTimestamp } from '../../utils/format.ts';
import { VisualizerCard } from './VisualizerCard.tsx';

Expand Down
28 changes: 26 additions & 2 deletions src/utils/algorithm.ts → src/utils/algorithm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Code, Text } from '@mantine/core';
import { ReactNode } from 'react';
import {
ActivityLogRow,
Algorithm,
Expand All @@ -22,6 +24,12 @@ import {
} from '../models.ts';
import { authenticatedAxios } from './axios.ts';

export class AlgorithmParseError extends Error {
public constructor(public readonly node: ReactNode) {
super('Failed to parse algorithm logs');
}
}

function getColumnValues(columns: string[], indices: number[]): number[] {
const values: number[] = [];

Expand Down Expand Up @@ -219,7 +227,14 @@ function getAlgorithmData(logLines: string[]): AlgorithmDataRow[] {
} catch (err) {
console.log(line);
console.error(err);
throw new Error('Sandbox logs are in invalid format, please see the prerequisites section above.');

throw new AlgorithmParseError(
/* prettier-ignore */
<>
<Text size="sm">Logs are in invalid format, please see the prerequisites section above. Could not parse the following line:</Text>
<Text size="sm">{line}</Text>
</>,
);
}
}

Expand All @@ -232,8 +247,17 @@ export function parseAlgorithmLogs(logs: string, summary?: AlgorithmSummary): Al
const activityLogs = getActivityLogs(logLines);
const data = getAlgorithmData(logLines);

if (activityLogs.length === 0 && data.length === 0) {
throw new AlgorithmParseError(
"Logs are empty, either something went wrong during with your submission or your backtester logs in a different format than Prosperity's submission environment.",
);
}

if (activityLogs.length === 0 || data.length === 0) {
throw new Error('Logs are in invalid format, please see the prerequisites section above.');
throw new AlgorithmParseError(
/* prettier-ignore */
<Text size="sm">Logs are in invalid format, please see the prerequisites section above. You are likely missing a <Code>logger.flush()</Code> call at the end of <Code>Trader.run()</Code>.</Text>,
);
}

return {
Expand Down

0 comments on commit c1900ba

Please sign in to comment.