Skip to content

Commit 71158d9

Browse files
Add error handling for when the profile file yaml parsing fails (#1523)
Use various error messages basd on what's missing from the profile import file
1 parent fddbab2 commit 71158d9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/components/profiles-modals/ImportProfileModal.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
121121
122122
if (read !== null) {
123123
this.profileImportFilePath = files[0];
124-
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
124+
try {
125+
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
126+
} catch (e: unknown) {
127+
const err = R2Error.fromThrownValue(e);
128+
this.$store.commit('error/handleError', err)
129+
this.closeModal();
130+
return;
131+
}
125132
126133
if (this.profileToOnlineMods.length === 0) {
127134
this.activeStep = 'NO_PACKAGES_IN_IMPORT';

src/utils/ProfileUtils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ export async function installModsToProfile(
125125

126126
export async function parseYamlToExportFormat(yamlContent: string) {
127127
const parsedYaml = await yaml.parse(yamlContent);
128+
if (!parsedYaml) {
129+
throw new R2Error(
130+
'Failed to parse yaml contents.',
131+
'Yaml parsing failed when trying to import profile via file (The contents of export.r2x file are invalid).',
132+
'Ensure that the profile import file isn\'t corrupted.'
133+
)
134+
}
135+
if (typeof parsedYaml.profileName !== 'string') {
136+
throw new R2Error(
137+
'Failed to read profile name.',
138+
'Reading the profile name after parsing the yaml failed (export.r2x is missing the profileName field).',
139+
'Ensure that the profile import file isn\'t corrupted.'
140+
)
141+
}
142+
if (!Array.isArray(parsedYaml.mods)) {
143+
throw new R2Error(
144+
'Failed to read mod list.',
145+
'Reading mods list after parsing the yaml failed (Mod list of export.r2x is invalid).',
146+
'Ensure that the profile import file isn\'t corrupted.'
147+
)
148+
}
128149
return new ExportFormat(
129150
parsedYaml.profileName,
130151
parsedYaml.mods.map((mod: any) => {

0 commit comments

Comments
 (0)