Skip to content

Commit

Permalink
Merge pull request #330 from visdesignlab/isu324-alttxt-err-handl
Browse files Browse the repository at this point in the history
Add error handling for alt text API
  • Loading branch information
NateLanza authored Apr 1, 2024
2 parents e9ad950 + 44c87b8 commit 6d06f8f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions e2e-tests/alttext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ test('Alt Text', async ({ page }) => {
await expect(plotInformation).toBeVisible();
await plotInformation.click();

// Test error message for aggregated plots
await page.getByRole('radio', { name: 'Degree' }).check();
const aggErrMsg = await page.getByText("Alt text generation is not yet supported for aggregated plots. To generate an alt text, set aggregation to 'None' in the left sidebar.");
await expect(aggErrMsg).toBeVisible();
await page.getByRole('radio', { name: 'None' }).check();

const editPlotInformationButton = await page.getByLabel('Toggle editable descriptions');
await expect(editPlotInformationButton).toBeVisible();
await editPlotInformationButton.click();
Expand Down
26 changes: 24 additions & 2 deletions packages/app/src/components/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,33 @@ export const Body = ({ yOffset, data, config }: Props) => {
})
}, [provObject.provenance, sessionId, workspace]);

async function generateAltText() {
/**
* Generates alt text for a plot based on the current state and configuration.
* If an error occurs during the generation, an error message is returned.
* Alt text generation currently is not supported for aggregated plots
* and an error message is returned if the plot is aggregated.
* @returns A promise that resolves to the generated alt text.
*/
async function generateAltText(): Promise<string> {
const state = provObject.provenance.getState();
const config = getAltTextConfig(state, data, getRows(data, state));

const response = await api.generateAltText(true, config);
if (config.firstAggregateBy !== "None") {
return "Alt text generation is not yet supported for aggregated plots. To generate an alt text, set aggregation to 'None' in the left sidebar.";
}

let response;
try {
response = await api.generateAltText(true, config);
} catch (e: any) {
if (e.response.status === 500) {
return "Server error while generating alt text. Please try again later. If the issue persists, please contact an UpSet developer at [email protected].";
} else if (e.response.status === 400) {
return "Error generating alt text. Contact an upset developer at [email protected].";
} else {
return "Unknown error while generating alt text: " + e.response.statusText + ". Please contact an UpSet developer at [email protected].";
}
}
return response.alttxt.longDescription;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/upset/src/components/AltTextSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export const AltTextSidebar: FC<Props> = ({ open, close, generateAltText }) => {
setTextDescription(resp);
}

if (currState.firstAggregateBy === 'None') {
generate();
}
generate();
}, [currState]);

// this useEffect resets the plot information when the edit is toggled off
Expand Down

0 comments on commit 6d06f8f

Please sign in to comment.