Skip to content

Commit ba00033

Browse files
Add error handling to readProfileFile
1 parent 172e7dd commit ba00033

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/components/profiles-modals/ImportProfileModal.vue

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,25 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
117117
return;
118118
}
119119
120-
let read: string | null = null;
120+
let read: string = '';
121121
try {
122122
read = await ProfileUtils.readProfileFile(files[0]);
123123
} catch (e: unknown) {
124-
if(e instanceof R2Error) {
125-
this.$store.commit('error/handleError', e)
126-
this.closeModal();
127-
return;
128-
}
124+
const err = R2Error.fromThrownValue(e);
125+
this.$store.commit('error/handleError', err);
126+
this.closeModal();
127+
return;
129128
}
130129
131-
if (read) {
132-
this.profileImportFilePath = files[0];
133-
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
130+
this.profileImportFilePath = files[0];
131+
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
134132
135-
if (this.profileToOnlineMods.length === 0) {
136-
this.activeStep = 'NO_PACKAGES_IN_IMPORT';
137-
return;
138-
}
139-
140-
this.activeStep = 'REVIEW_IMPORT';
133+
if (this.profileToOnlineMods.length === 0) {
134+
this.activeStep = 'NO_PACKAGES_IN_IMPORT';
135+
return;
141136
}
137+
138+
this.activeStep = 'REVIEW_IMPORT';
142139
}
143140
144141
// Fired when user has accepted the mods to be imported in the review phase.

src/utils/ProfileUtils.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,23 @@ export async function populateImportedProfile(
180180
}
181181
}
182182

183-
export async function readProfileFile(file: string) {
184-
let read = '';
183+
export async function readProfileFile(file: string): Promise<string> {
184+
let read: string | undefined;
185185
if (file.endsWith('.r2x')) {
186186
read = (await FsProvider.instance.readFile(file)).toString();
187187
} else if (file.endsWith('.r2z')) {
188-
const result: Buffer | null = await ZipProvider.instance.readFile(file, "export.r2x");
189-
if (!result) {
190-
throw new R2Error(
191-
'Error when reading file contents',
192-
'Reading the .r2z file contents failed. File contents be empty or corrupted.',
193-
'Ensure that the imported profile file is valid.'
194-
);
195-
}
196-
read = result.toString();
188+
await ZipProvider.instance.readFile(file, "export.r2x").then(
189+
(value) => { read = value?.toString() },
190+
() => { return null; }
191+
);
192+
}
193+
194+
if (!read) {
195+
throw new R2Error(
196+
'Error when reading file contents',
197+
'Reading the .r2z file contents failed. File contents be empty or corrupted.',
198+
'Ensure that the imported profile file is valid.'
199+
);
197200
}
198201
return read;
199202
}

0 commit comments

Comments
 (0)