Skip to content

Commit

Permalink
smaller improvement to error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cammiida committed Dec 11, 2024
1 parent d5f91ae commit e426ae2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/layout/SigneeList/SigneeListComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React from 'react';
import { useParams } from 'react-router-dom';

import { queryOptions, useQuery } from '@tanstack/react-query';
import { z } from 'zod';
import { isAxiosError } from 'axios';
import { z, ZodError } from 'zod';

import { AppTable } from 'src/app-components/Table/Table';
import { Caption } from 'src/components/form/caption/Caption';
Expand Down Expand Up @@ -45,10 +46,17 @@ async function fetchSigneeList(partyId: string, instanceGuid: string): Promise<S

try {
const response = await httpGet(url);
const parsed = z.object({ signeeStates: z.array(signeeStateSchema) }).safeParse(response);
const parsed = z.object({ signeeStates: z.array(signeeStateSchema) }).parse(response);

if (!parsed.success) {
// TODO: alarm? telemetri?
const sortedSigneeStates = parsed.signeeStates.toSorted((a, b) => (a.name ?? '').localeCompare(b.name ?? ''));

return { errors: null, data: sortedSigneeStates };
} catch (error: unknown) {
if (error instanceof ZodError) {
// // TODO: alarm? telemetri?
window.logErrorOnce(
`Did not get the expected response from the server. The response didn't match the expected schema: \n${error}`,
);
return {
data: null,
errors: [
Expand All @@ -65,16 +73,14 @@ async function fetchSigneeList(partyId: string, instanceGuid: string): Promise<S
};
}

const sortedSigneeStates = parsed.data.signeeStates.toSorted((a, b) => (a.name ?? '').localeCompare(b.name ?? ''));
if (isAxiosError(error)) {
const parsed = problemDetailsSchema.safeParse(error.response?.data);

return { errors: null, data: sortedSigneeStates };
} catch (error) {
const parsed = problemDetailsSchema.safeParse(error.response.data);

if (!parsed.success) {
throw new Error('signee_list.unknown_api_error');
if (parsed.success) {
throw new Error(parsed.data.detail);
}
}
throw new Error(parsed.data.detail);
throw new Error('signee_list.unknown_api_error');
}
}

Expand Down

0 comments on commit e426ae2

Please sign in to comment.