From 71158d9777d483a8f7258108da342a7f789f86d2 Mon Sep 17 00:00:00 2001 From: Vili Manninen <104134747+VilppeRiskidev@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:58:14 +0200 Subject: [PATCH] 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 --- .../profiles-modals/ImportProfileModal.vue | 9 +++++++- src/utils/ProfileUtils.ts | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/components/profiles-modals/ImportProfileModal.vue b/src/components/profiles-modals/ImportProfileModal.vue index 7b97c03b..594c7e75 100644 --- a/src/components/profiles-modals/ImportProfileModal.vue +++ b/src/components/profiles-modals/ImportProfileModal.vue @@ -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'; diff --git a/src/utils/ProfileUtils.ts b/src/utils/ProfileUtils.ts index e76d5e38..0ff5b192 100644 --- a/src/utils/ProfileUtils.ts +++ b/src/utils/ProfileUtils.ts @@ -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) => {