Skip to content

Commit ccd0f0c

Browse files
authored
Merge pull request #3510 from invoiceninja/DOC-761-docuninja-error-toasts
fix(docuninja): stop dropping error messages and false success toasts
2 parents 414bafe + 86c7597 commit ccd0f0c

24 files changed

Lines changed: 187 additions & 94 deletions

File tree

‎src/App.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export function App() {
144144
if (errorMessage) {
145145
toast.error(errorMessage);
146146
}
147+
} else if (error.response.data.message) {
148+
toast.error(error.response.data.message);
147149
}
148150
}
149151
};

‎src/common/helpers/request.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ client.interceptors.response.use(
8080
if (error.response?.status === 403) {
8181
toast.error('unauthorized_action');
8282

83-
return;
83+
return Promise.reject(error);
8484
}
8585

8686
if (error.response?.status === 429 || error.response?.status === 401) {
@@ -103,7 +103,7 @@ client.interceptors.response.use(
103103

104104
if (error.response?.status === 404) {
105105
toast.error('record_not_found');
106-
return;
106+
return Promise.reject(error);
107107
}
108108

109109
if (
@@ -116,7 +116,7 @@ client.interceptors.response.use(
116116
toast.error(error.response?.data.message || 'error_title');
117117
}
118118

119-
if (error.response?.status && error.response.status === 500) {
119+
if (error.response?.status && error.response.status >= 500) {
120120
toast.error('error_title');
121121
}
122122

‎src/common/queries/docuninja/blueprints.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ export function useUpdateBlueprint() {
187187
.catch((error: AxiosError<ValidationBag>) => {
188188
if (error.response?.status === 422) {
189189
setErrors(error.response.data);
190-
toast.dismiss();
190+
191+
if (error.response.data.errors) {
192+
toast.dismiss();
193+
} else {
194+
toast.error(error.response.data.message);
195+
}
191196
}
192197
})
193198
.finally();

‎src/common/queries/docuninja/users.ts‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
*/
1010

1111
import { useQuery, useQueryClient } from '@tanstack/react-query';
12+
import type { AxiosError } from 'axios';
1213
import { useAtomValue } from 'jotai';
1314
import { invalidationQueryAtom } from '$app/common/atoms/data-table';
1415
import { docuNinjaEndpoint } from '$app/common/helpers';
1516
import { request } from '$app/common/helpers/request';
1617
import { toast } from '$app/common/helpers/toast/toast';
1718
import { $refetch } from '$app/common/hooks/useRefetch';
19+
import { ValidationBag } from '$app/common/interfaces/validation-bag';
1820
import { Params } from '../common/params.interface';
1921

2022
export function useUsersQuery(params: Params) {
@@ -121,17 +123,21 @@ export function useBulk() {
121123
},
122124
skipIntercept: true,
123125
}
124-
).then(() => {
125-
const message = `${action}d_user`;
126-
127-
toast.success(message);
128-
129-
invalidateQueryValue &&
130-
queryClient.invalidateQueries({
131-
queryKey: [invalidateQueryValue],
132-
});
133-
134-
$refetch(['docuninja_users']);
135-
});
126+
)
127+
.then(() => {
128+
const message = `${action}d_user`;
129+
130+
toast.success(message);
131+
132+
invalidateQueryValue &&
133+
queryClient.invalidateQueries({
134+
queryKey: [invalidateQueryValue],
135+
});
136+
137+
$refetch(['docuninja_users']);
138+
})
139+
.catch((error: AxiosError<ValidationBag>) => {
140+
toast.error(error.response?.data?.message || 'error_title');
141+
});
136142
};
137143
}

‎src/components/ValidationAlert.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function ValidationAlert(props: Props) {
2828
{!withoutTopMessage && <p>{props.errors.message}</p>}
2929

3030
<ul>
31-
{Object.keys(props.errors.errors).map((key, index) => (
31+
{Object.keys(props.errors.errors ?? {}).map((key, index) => (
3232
<li key={index}>
3333
{!withoutListBullets && <>&#8211; </>}
3434
{validationMessageAlias(key, props.errors.errors[key])}

‎src/pages/documents/common/components/DocumentCreationDropZone.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ export function DocumentCreationDropZone({ onSelectFiles }: Props) {
7575
.catch((e) => {
7676
if (e.response?.status === 422) {
7777
setErrors(e.response.data);
78-
toast.dismiss();
78+
79+
if (e.response.data.errors) {
80+
toast.dismiss();
81+
} else {
82+
toast.error(e.response.data.message);
83+
}
7984
}
8085
})
8186
.finally(() => setIsFormBusy(false));

‎src/pages/documents/common/helpers.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* @license https://www.elastic.co/licensing/elastic-license
99
*/
1010

11+
import type { AxiosError } from 'axios';
12+
import { toast } from '$app/common/helpers/toast/toast';
13+
1114
function normalizeName(name: string): string {
1215
return name
1316
.replace(/[-_]+/g, ' ')
@@ -30,3 +33,16 @@ export function canActivateUpgradeButton(
3033
): boolean {
3134
return Boolean(pricing && !isLoading && pricing.pro_rata_raw > 0);
3235
}
36+
37+
export async function toastDownloadError(error: AxiosError<Blob>) {
38+
const message: string | undefined = await error.response?.data
39+
?.text()
40+
.then((text) => JSON.parse(text).message)
41+
.catch(() => undefined);
42+
43+
if (error.response?.status === 400) {
44+
toast.error('not_ready_for_download');
45+
} else {
46+
toast.error(message || 'something_went_wrong');
47+
}
48+
}

‎src/pages/documents/common/hooks/useActions.tsx‎

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { DeleteDocumentAction } from '../../show/components/DeleteDocumentAction
3838
import { RestoreDocumentAction } from '../../show/components/RestoreDocumentAction';
3939
import { SendInvitationsModal } from '../../show/components/SendInvitationsModal';
4040
import { CloneAction } from '../components/CloneAction';
41+
import { toastDownloadError } from '../helpers';
4142
import { MakeTemplateAction } from '../components/MakeTemplateAction';
4243
import { VoidAction } from '../components/VoidAction';
4344

@@ -81,15 +82,7 @@ export function useActions(params?: Params) {
8182
downloadBlob(response.data, filename, 'application/pdf');
8283
toast.dismiss();
8384
})
84-
.catch((error) => {
85-
if (error.response?.status === 422) {
86-
toast.error(error.response.data.message || 'something_went_wrong');
87-
} else if (error.response?.status === 400) {
88-
toast.error('not_ready_for_download');
89-
} else {
90-
toast.error('something_went_wrong');
91-
}
92-
});
85+
.catch(toastDownloadError);
9386
};
9487

9588
const downloadAuditLog = (doc: Document) => {
@@ -117,15 +110,7 @@ export function useActions(params?: Params) {
117110
downloadBlob(response.data, filename, 'application/pdf');
118111
toast.dismiss();
119112
})
120-
.catch((error) => {
121-
if (error.response?.status === 422) {
122-
toast.error(error.response.data.message || 'something_went_wrong');
123-
} else if (error.response?.status === 400) {
124-
toast.error('not_ready_for_download');
125-
} else {
126-
toast.error('something_went_wrong');
127-
}
128-
});
113+
.catch(toastDownloadError);
129114
};
130115

131116
const getUserInvitation = (doc: Document) => {

‎src/pages/documents/create/Create.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ export default function Create() {
9494
.catch((error: AxiosError<ValidationBag>) => {
9595
if (error.response?.status === 422) {
9696
setErrors(error.response.data);
97-
toast.dismiss();
97+
98+
if (error.response.data.errors) {
99+
toast.dismiss();
100+
} else {
101+
toast.error(error.response.data.message);
102+
}
98103
}
99104
})
100105
.finally(() => setIsFormBusy(false));
@@ -126,7 +131,7 @@ export default function Create() {
126131
onValueChange={(value) =>
127132
setPayload({ ...payload, description: value })
128133
}
129-
errorMessage={errors?.errors.description}
134+
errorMessage={errors?.errors?.description}
130135
/>
131136
</Element>
132137

‎src/pages/documents/pages/blueprints/create/components/steps/AuthoredBlueprintStep.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export function AuthoredBlueprintStep({
7676
.catch((error: AxiosError<ValidationBag>) => {
7777
if (error.response?.status === 422) {
7878
setErrors(error.response.data);
79-
toast.dismiss();
79+
80+
if (error.response.data.errors) {
81+
toast.dismiss();
82+
} else {
83+
toast.error(error.response.data.message);
84+
}
8085
} else {
8186
toast.error('something_went_wrong');
8287
}
@@ -95,7 +100,7 @@ export function AuthoredBlueprintStep({
95100
<InputField
96101
value={payload.name}
97102
onValueChange={(name) => setPayload({ ...payload, name })}
98-
errorMessage={errors?.errors.name}
103+
errorMessage={errors?.errors?.name}
99104
placeholder={t('template_name')}
100105
/>
101106
</Element>
@@ -107,7 +112,7 @@ export function AuthoredBlueprintStep({
107112
onValueChange={(description) =>
108113
setPayload({ ...payload, description })
109114
}
110-
errorMessage={errors?.errors.description}
115+
errorMessage={errors?.errors?.description}
111116
placeholder={t('description')}
112117
/>
113118
</Element>

0 commit comments

Comments
 (0)