Skip to content

Commit ae7e8cd

Browse files
authored
Merge pull request #1961 from appwrite/csv-imports-errors
CSV import error
2 parents 493b760 + f9f861a commit ae7e8cd

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

src/lib/components/csvImportBox.svelte

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,42 @@
2727
const importItems: Writable<ImportItemsMap> = writable(new Map());
2828
2929
async function showCompletionNotification(
30-
databaseId: string,
31-
collectionId: string,
32-
importData: Payload
30+
database: string,
31+
collection: string,
32+
payload: Payload
3333
) {
34-
await invalidate(Dependencies.DOCUMENTS);
35-
const url = `${base}/project-${page.params.region}-${page.params.project}/databases/database-${databaseId}/collection-${collectionId}`;
34+
const isSuccess = payload.status === 'completed';
35+
const isError = !isSuccess && !!payload.errors;
3636
37-
// extract clean message from nested backend error.
38-
const match = importData.errors.join('').match(/message: '(.*)' Message:/i);
39-
const errorMessage = match?.[1];
37+
if (!isSuccess && !isError) return;
4038
41-
const type = importData.status === 'completed' ? 'success' : 'error';
42-
const message =
43-
importData.status === 'completed'
44-
? 'CSV import finished successfully.'
45-
: `${errorMessage}`;
39+
let errorMessage = 'Import failed. Check your CSV for correct fields and required values.';
40+
if (isError && Array.isArray(payload.errors)) {
41+
try {
42+
// the `errors` is a list of json encoded string.
43+
errorMessage = JSON.parse(payload.errors[0]).message;
44+
} catch {
45+
// do nothing, fallback to default message.
46+
}
47+
}
48+
49+
const type = isSuccess ? 'success' : 'error';
50+
const message = isError ? errorMessage : 'CSV import finished successfully.';
51+
const url = `${base}/project-${page.params.region}-${page.params.project}/databases/database-${database}/collection-${collection}`;
4652
4753
addNotification({
4854
type,
4955
message,
5056
isHtml: true,
5157
buttons:
52-
collectionId === page.params.collection || type === 'error'
53-
? undefined
54-
: [
55-
{
56-
name: 'View documents',
57-
method: () => goto(url)
58-
}
59-
]
58+
isSuccess && collection !== page.params.collection
59+
? [{ name: 'View documents', method: () => goto(url) }]
60+
: undefined
6061
});
62+
63+
if (isSuccess) {
64+
await invalidate(Dependencies.DOCUMENTS);
65+
}
6166
}
6267
6368
async function updateOrAddItem(importData: Payload | Models.Migration) {

src/lib/stores/preferences.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ function createPreferences() {
107107
}
108108
);
109109
},
110-
111110
getCustomCollectionColumns: (collectionId: string): Preferences['columns'] => {
112111
return preferences?.collections?.[collectionId] ?? [];
113112
},

src/routes/(console)/project-[region]-[project]/databases/database-[database]/collection-[collection]/table.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import { toLocaleDateTime } from '$lib/helpers/date';
3434
import DualTimeView from '$lib/components/dualTimeView.svelte';
3535
import { flags } from '$lib/flags';
36+
3637
export let data: PageData;
3738
3839
const databaseId = page.params.database;
@@ -255,12 +256,13 @@
255256
{@const datetime = document[id]}
256257
{@const formatted = formatColumn(document[id])}
257258
{@const isDatetimeAttribute = attr.type === 'datetime'}
259+
{@const isEncryptedAttribute = isString(attr) && attr.encrypt}
258260
{#if isDatetimeAttribute}
259261
<DualTimeView time={datetime}>
260262
<span slot="title">Timestamp</span>
261263
{toLocaleDateTime(datetime, true)}
262264
</DualTimeView>
263-
{:else if isString(attr) && attr.encrypt && showEncrypt}
265+
{:else if isEncryptedAttribute && showEncrypt}
264266
<button on:click={(e) => e.preventDefault()}>
265267
<InteractiveText
266268
copy={false}

src/routes/(console)/project-[region]-[project]/settings/migrations/details.svelte

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020
} from '@appwrite.io/pink-svelte';
2121
import { Button } from '$lib/elements/forms';
2222
23-
export let migration: Models.Migration = null;
2423
export let show = false;
24+
export let migration: Models.Migration = null;
25+
26+
// convert to proper json objects from string!
27+
migration.errors = migration.errors.map((err) => {
28+
try {
29+
return JSON.parse(err);
30+
} catch {
31+
return err;
32+
}
33+
});
2534
2635
type StatusCounters = {
2736
[resource in 'Database' | 'Collection' | 'Function' | 'Users']?: StatusCounter;
@@ -55,7 +64,7 @@
5564
};
5665
5766
let tab = 'details' as 'details' | 'logs';
58-
let logs = JSON.stringify(migration, null, 2);
67+
const logs = JSON.stringify(migration, null, 2);
5968
</script>
6069

6170
<Modal

0 commit comments

Comments
 (0)