Skip to content

Commit 76d8368

Browse files
committed
Used AlertError to handle all error responses
1 parent 98574f7 commit 76d8368

File tree

22 files changed

+138
-49
lines changed

22 files changed

+138
-49
lines changed

__tests__/StandardErrorAlert.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('StandardErrorAlert', () => {
1212
expect(result.container.textContent).toContain('error message');
1313
});
1414

15-
it('AlertError with detail message', async () => {
15+
it('AlertError with string detail', async () => {
1616
const result = render(StandardErrorAlert, {
1717
error: new AlertError({ detail: 'error message' })
1818
});
@@ -21,6 +21,26 @@ describe('StandardErrorAlert', () => {
2121
expect(result.container.textContent).not.toContain('There has been an error');
2222
});
2323

24+
it('AlertError with object detail and __root__ loc', async () => {
25+
const result = render(StandardErrorAlert, {
26+
error: new AlertError(
27+
{
28+
detail: [
29+
{
30+
loc: ['body', '__root__'],
31+
msg: 'error message',
32+
type: 'value_error'
33+
}
34+
]
35+
},
36+
422
37+
)
38+
});
39+
expect(result.container.textContent).toContain('error message');
40+
expect(result.container.textContent).not.toContain('detail');
41+
expect(result.container.textContent).not.toContain('There has been an error');
42+
});
43+
2444
it('AlertError with generic object message', async () => {
2545
const result = render(StandardErrorAlert, {
2646
error: new AlertError({ foo: 'bar' })

src/lib/common/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ function getSimpleValidationMessage(reason, statusCode) {
7373
if (!isValueError(err)) {
7474
return null;
7575
}
76+
const loc = err.loc.length > 1 && err.loc[0] === 'body' ? err.loc.slice(1) : err.loc;
7677
return {
77-
loc: err.loc.length > 1 && err.loc[0] === 'body' ? err.loc.slice(1) : err.loc,
78+
loc: loc.length === 1 && loc[0] === '__root__' ? [] : loc,
7879
msg: err.msg
7980
};
8081
}

src/lib/components/common/StandardErrorAlert.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
1313
function updateErrorMessage() {
1414
if (error instanceof AlertError) {
15-
if (typeof error.reason === 'string') {
15+
const simpleMessage = error.getSimpleValidationMessage();
16+
if (simpleMessage) {
17+
errorString = simpleMessage;
18+
formatAsPre = false;
19+
} else if (typeof error.reason === 'string') {
1620
errorString = error.reason;
1721
formatAsPre = false;
18-
} else if ('detail' in error.reason) {
19-
errorString = /** @type {string}*/ (error.reason.detail);
22+
} else if ('detail' in error.reason && typeof error.reason.detail === 'string') {
23+
errorString = error.reason.detail;
2024
formatAsPre = false;
2125
} else {
2226
errorString = JSON.stringify(error.reason, undefined, 2);
@@ -25,7 +29,7 @@
2529
} else if (error instanceof Error) {
2630
errorString = error.message;
2731
formatAsPre = false;
28-
} else if (typeof error === 'object' && 'detail' in error) {
32+
} else if (typeof error === 'object' && 'detail' in error && typeof error.detail === 'string') {
2933
errorString = error.detail;
3034
formatAsPre = false;
3135
} else {

src/lib/components/v2/admin/UserEditor.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { page } from '$app/stores';
44
import { nullifyEmptyStrings, removeNullValues } from '$lib/common/component_utilities';
55
import {
6+
AlertError,
67
displayStandardErrorAlert,
78
getValidationMessagesMap,
89
validateErrorMapKeys
@@ -75,7 +76,10 @@
7576
if (errorsMap && validateErrorMapKeys(errorsMap, handledErrorKeys)) {
7677
validationErrors = errorsMap;
7778
} else {
78-
genericErrorAlert = displayStandardErrorAlert(result, 'genericError');
79+
genericErrorAlert = displayStandardErrorAlert(
80+
new AlertError(result, response.status),
81+
'genericError'
82+
);
7983
}
8084
return;
8185
}

src/lib/components/v2/jobs/JobInfoModal.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import StatusBadge from '$lib/components/jobs/StatusBadge.svelte';
3-
import { displayStandardErrorAlert } from '$lib/common/errors';
3+
import { AlertError, displayStandardErrorAlert } from '$lib/common/errors';
44
import { page } from '$app/stores';
55
import Modal from '../../common/Modal.svelte';
66
@@ -37,7 +37,10 @@
3737
if (response.ok) {
3838
job = result;
3939
} else {
40-
errorAlert = displayStandardErrorAlert(result, 'workflowJobError');
40+
errorAlert = displayStandardErrorAlert(
41+
new AlertError(result, response.status),
42+
'workflowJobError'
43+
);
4144
}
4245
}
4346
</script>

src/lib/components/v2/jobs/JobsList.svelte

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import JobInfoModal from '$lib/components/v2/jobs/JobInfoModal.svelte';
77
import JobLogsModal from '$lib/components/v2/jobs/JobLogsModal.svelte';
88
import Th from '$lib/components/common/filterable/Th.svelte';
9-
import { displayStandardErrorAlert } from '$lib/common/errors';
9+
import { AlertError, displayStandardErrorAlert } from '$lib/common/errors';
1010
import { onDestroy, onMount } from 'svelte';
1111
import { removeDuplicatedItems } from '$lib/common/component_utilities';
1212
import StandardDismissableAlert from '../../common/StandardDismissableAlert.svelte';
@@ -63,11 +63,7 @@
6363
$: tableHandler.filter(statusFilter, 'status', check.isEqualTo);
6464
$: tableHandler.filter(projectFilter, (row) => row.project_dump.id.toString(), check.isEqualTo);
6565
$: tableHandler.filter(workflowFilter, (row) => row.workflow_dump.id.toString(), check.isEqualTo);
66-
$: tableHandler.filter(
67-
datasetFilter,
68-
(row) => row.dataset_dump.id.toString(),
69-
check.isEqualTo
70-
);
66+
$: tableHandler.filter(datasetFilter, (row) => row.dataset_dump.id.toString(), check.isEqualTo);
7167
7268
$: rebuildSlimSelectOptions($rows);
7369
@@ -123,8 +119,11 @@
123119
jobCancelledMessage = 'Job cancellation request received. The job will stop in a few seconds';
124120
} else {
125121
console.error('Error stopping job');
126-
const errorResponse = await response.json();
127-
errorAlert = displayStandardErrorAlert(errorResponse, 'jobUpdatesError');
122+
const result = await response.json();
123+
errorAlert = displayStandardErrorAlert(
124+
new AlertError(result, response.status),
125+
'jobUpdatesError'
126+
);
128127
}
129128
}
130129

src/lib/components/v2/projects/ProjectInfoModal.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { displayStandardErrorAlert } from '$lib/common/errors';
2+
import { AlertError, displayStandardErrorAlert } from '$lib/common/errors';
33
44
// ProjectInfoModal component
55
import { projectInfoModalV2 } from '$lib/stores/projectStores';
@@ -30,14 +30,14 @@
3030
method: 'GET',
3131
credentials: 'include'
3232
});
33+
const result = await response.json();
3334
if (response.ok) {
3435
/** @type {import('$lib/types-v2.js').DatasetV2[]} */
35-
const result = await response.json();
3636
result.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
3737
datasets = result;
3838
} else {
3939
datasetErrorAlert = displayStandardErrorAlert(
40-
await response.json(),
40+
new AlertError(result, response.status),
4141
'errorAlert-projectInfoModal'
4242
);
4343
}

src/lib/components/v2/tasks/AddSingleTask.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Ajv from 'ajv';
33
import { replaceEmptyStrings } from '$lib/common/component_utilities';
44
import {
5+
AlertError,
56
displayStandardErrorAlert,
67
getValidationMessagesMap,
78
validateErrorMapKeys
@@ -147,7 +148,10 @@
147148
if (errorsMap && validateErrorMapKeys(errorsMap, handledErrorKeys)) {
148149
validationErrors = errorsMap;
149150
} else {
150-
errorAlert = displayStandardErrorAlert(result, 'errorAlert-createTask');
151+
errorAlert = displayStandardErrorAlert(
152+
new AlertError(result, response.status),
153+
'errorAlert-createTask'
154+
);
151155
}
152156
}
153157
}

src/lib/components/v2/tasks/CustomEnvTask.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import manifestSchema from './manifest_v2.json';
55
import { replaceEmptyStrings } from '$lib/common/component_utilities';
66
import {
7+
AlertError,
78
displayStandardErrorAlert,
89
getValidationMessagesMap,
910
validateErrorMapKeys
@@ -124,7 +125,10 @@
124125
if (errorsMap && validateErrorMapKeys(errorsMap, handledErrorKeys)) {
125126
validationErrors = errorsMap;
126127
} else {
127-
errorAlert = displayStandardErrorAlert(result, 'errorAlert-customEnvTask');
128+
errorAlert = displayStandardErrorAlert(
129+
new AlertError(result, response.status),
130+
'errorAlert-customEnvTask'
131+
);
128132
}
129133
}
130134
} finally {

src/lib/components/v2/tasks/TaskCollectionLogsModal.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { displayStandardErrorAlert } from '$lib/common/errors';
2+
import { AlertError, displayStandardErrorAlert } from '$lib/common/errors';
33
import { modalTaskCollectionId } from '$lib/stores/taskStores';
44
import { onDestroy } from 'svelte';
55
import Modal from '../../common/Modal.svelte';
@@ -25,7 +25,10 @@
2525
logs = result.data.log;
2626
} else {
2727
console.error('Failed to fetch collection logs', result);
28-
errorAlert = displayStandardErrorAlert(result, 'collectionTaskLogsError');
28+
errorAlert = displayStandardErrorAlert(
29+
new AlertError(result, response.status),
30+
'collectionTaskLogsError'
31+
);
2932
}
3033
}
3134
});

0 commit comments

Comments
 (0)