Skip to content

Commit

Permalink
Add error handling for when the profile file yaml parsing fails (#1523)
Browse files Browse the repository at this point in the history
Use various error messages basd on what's missing from the profile import file
  • Loading branch information
VilppeRiskidev authored Nov 6, 2024
1 parent fddbab2 commit 71158d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/profiles-modals/ImportProfileModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
if (read !== null) {
this.profileImportFilePath = files[0];
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
try {
this.profileImportContent = await ProfileUtils.parseYamlToExportFormat(read);
} catch (e: unknown) {
const err = R2Error.fromThrownValue(e);
this.$store.commit('error/handleError', err)
this.closeModal();
return;
}
if (this.profileToOnlineMods.length === 0) {
this.activeStep = 'NO_PACKAGES_IN_IMPORT';
Expand Down
21 changes: 21 additions & 0 deletions src/utils/ProfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ export async function installModsToProfile(

export async function parseYamlToExportFormat(yamlContent: string) {
const parsedYaml = await yaml.parse(yamlContent);
if (!parsedYaml) {
throw new R2Error(
'Failed to parse yaml contents.',
'Yaml parsing failed when trying to import profile via file (The contents of export.r2x file are invalid).',
'Ensure that the profile import file isn\'t corrupted.'
)
}
if (typeof parsedYaml.profileName !== 'string') {
throw new R2Error(
'Failed to read profile name.',
'Reading the profile name after parsing the yaml failed (export.r2x is missing the profileName field).',
'Ensure that the profile import file isn\'t corrupted.'
)
}
if (!Array.isArray(parsedYaml.mods)) {
throw new R2Error(
'Failed to read mod list.',
'Reading mods list after parsing the yaml failed (Mod list of export.r2x is invalid).',
'Ensure that the profile import file isn\'t corrupted.'
)
}
return new ExportFormat(
parsedYaml.profileName,
parsedYaml.mods.map((mod: any) => {
Expand Down

0 comments on commit 71158d9

Please sign in to comment.