Skip to content

Commit

Permalink
Bedre graphql feilhåndtering
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Dec 23, 2024
1 parent 7848d36 commit 8e87160
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/utils/fetchErrors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export class GraphqlError extends Error {
errors: { message: string }[];
constructor(message: string, errors: { message: string }[]) {
super(message);
this.errors = errors;
}
}

export class UnautorizedError extends Error {
response: Response;
constructor(response: Response) {
Expand Down
1 change: 0 additions & 1 deletion src/view/dialog/DialogTrad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ const ValgtDialog = ({
};

const DialogLoader = () => {
console.log('DialogLoader fallback');
return (
<div className="flex w-full bg-gray-100 items-center justify-center">
<Loader size="2xlarge" />
Expand Down
7 changes: 4 additions & 3 deletions src/view/dialogProvider/dialogGraphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DialogApi } from '../../api/UseApiBasePath';
import { sjekkStatuskode, toJson } from '../../utils/Fetch';
import { DialogData, KladdData } from '../../utils/Typer';
import { GraphqlError } from '../../utils/fetchErrors';

const query = `
query($fnr: String!, $bareMedAktiviteter: Boolean) {
Expand Down Expand Up @@ -47,18 +48,18 @@ const queryBody = (fnr: string) => ({
}
});

interface GraphqlError {
interface GraphqlErrorMessage {
message: string;
}

export interface GraphqlResponse<T> {
data: T;
errors: GraphqlError[];
errors: GraphqlErrorMessage[];
}

const sjekkGraphqlFeil = <T>(response: GraphqlResponse<T>): Promise<GraphqlResponse<T>> => {
if ((response?.errors?.length || 0) != 0) {
return Promise.reject('Kunne ikke hente dialoger');
return Promise.reject(new GraphqlError('Kunne ikke hente dialoger', response.errors));
}
return Promise.resolve(response);
};
Expand Down
2 changes: 1 addition & 1 deletion src/view/dialogProvider/dialogStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const useDialogStore = create(
if (e instanceof UnautorizedError) {
captureMessage('UnauthorizedError 401 på henting av sist oppdatert');
} else {
captureMessage('Kunne ikke hente sist oppdatert');
captureMaybeError(`Kunne ikke hente sist oppdatert ${e.toString()}`, e);

Check failure on line 152 in src/view/dialogProvider/dialogStore.ts

View workflow job for this annotation

GitHub Actions / build

'e' is of type 'unknown'.

Check failure on line 152 in src/view/dialogProvider/dialogStore.ts

View workflow job for this annotation

GitHub Actions / Deploy to dev extern / Deploy application to dev

'e' is of type 'unknown'.

Check failure on line 152 in src/view/dialogProvider/dialogStore.ts

View workflow job for this annotation

GitHub Actions / Deploy application to dev intern / Deploy application to CDN

'e' is of type 'unknown'.
}
}
},
Expand Down

0 comments on commit 8e87160

Please sign in to comment.