From eabb6e1ba99ad46a2de394df931f7012d5886598 Mon Sep 17 00:00:00 2001 From: connoratrug <47183404+connoratrug@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:35:49 +0100 Subject: [PATCH] fix: import profile task keeps running on failure (#4578) fix: import profile task keeps running on failure improve logging output --- .../molgenis/emx2/io/ImportProfileTask.java | 19 +++++++++++++++---- .../emx2/datamodels/TestProfileLoader.java | 11 +++++++++++ .../test/resources/TestProfileWithError.yaml | 8 ++++++++ .../org/molgenis/emx2/io/ImportTableTask.java | 19 +++++++++++++------ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 backend/molgenis-emx2-datamodels/src/test/resources/TestProfileWithError.yaml diff --git a/backend/molgenis-emx2-datamodels/src/main/java/org/molgenis/emx2/io/ImportProfileTask.java b/backend/molgenis-emx2-datamodels/src/main/java/org/molgenis/emx2/io/ImportProfileTask.java index 09e709dbbc..f20ea5c8e4 100644 --- a/backend/molgenis-emx2-datamodels/src/main/java/org/molgenis/emx2/io/ImportProfileTask.java +++ b/backend/molgenis-emx2-datamodels/src/main/java/org/molgenis/emx2/io/ImportProfileTask.java @@ -42,9 +42,15 @@ public void run() { commitTask.setDescription("Committing"); }); } catch (Exception e) { - commitTask.completeWithError("Commit failed: " + e.getMessage()); - this.completeWithError(e.getMessage()); - throw (e); + try { + commitTask.completeWithError("CommitTask failed: " + e.getMessage()); + } catch (MolgenisException e2) { + try { + this.completeWithError("ImportProfileTask failed: " + e2.getMessage()); + } catch (Exception e3) { + throw (e3); + } + } } commitTask.complete(); this.complete(); @@ -56,7 +62,12 @@ void load(Schema schema) { Profiles profiles = getProfiles(schema, schemaFromProfile); // create the schema using the selected profile tags within the big model - SchemaMetadata schemaMetadata = schemaFromProfile.create(); + SchemaMetadata schemaMetadata = null; + try { + schemaMetadata = schemaFromProfile.create(); + } catch (Exception e) { + throw new MolgenisException("Failed to create schema from profile: " + e.getMessage(), e); + } // special option: fixed schema import location for ontologies (not schema or data) Schema ontologySchema; diff --git a/backend/molgenis-emx2-datamodels/src/test/java/org/molgenis/emx2/datamodels/TestProfileLoader.java b/backend/molgenis-emx2-datamodels/src/test/java/org/molgenis/emx2/datamodels/TestProfileLoader.java index 05cb641ecf..1bdb92bfac 100644 --- a/backend/molgenis-emx2-datamodels/src/test/java/org/molgenis/emx2/datamodels/TestProfileLoader.java +++ b/backend/molgenis-emx2-datamodels/src/test/java/org/molgenis/emx2/datamodels/TestProfileLoader.java @@ -4,11 +4,13 @@ import org.junit.jupiter.api.*; import org.molgenis.emx2.Database; +import org.molgenis.emx2.MolgenisException; import org.molgenis.emx2.Schema; import org.molgenis.emx2.datamodels.profiles.Profiles; import org.molgenis.emx2.datamodels.profiles.SchemaFromProfile; import org.molgenis.emx2.io.ImportProfileTask; import org.molgenis.emx2.sql.TestDatabaseFactory; +import org.molgenis.emx2.tasks.TaskStatus; @TestMethodOrder(MethodOrderer.MethodName.class) @Tag("slow") @@ -54,4 +56,13 @@ void testProfileLoader() { assertFalse(testIncludeProfileSchema.getTableNames().contains("Individuals")); assertTrue(testIncludeProfileSchema.getTableNames().contains("Distribution")); } + + @Test + void testProfileLoaderWithError() { + database.dropSchemaIfExists(TEST_PROFILE); + Schema testProfileSchema = database.createSchema(TEST_PROFILE); + var task = new ImportProfileTask(testProfileSchema, "TestProfileWithError.yaml", true); + assertThrows(MolgenisException.class, task::run); + assertEquals(TaskStatus.ERROR, task.getStatus()); + } } diff --git a/backend/molgenis-emx2-datamodels/src/test/resources/TestProfileWithError.yaml b/backend/molgenis-emx2-datamodels/src/test/resources/TestProfileWithError.yaml new file mode 100644 index 0000000000..33110f9fc2 --- /dev/null +++ b/backend/molgenis-emx2-datamodels/src/test/resources/TestProfileWithError.yaml @@ -0,0 +1,8 @@ +--- +name: TestProfileError +description: "Test profile with error" + +profileTags: DataCatalogue + +demoData: _demodata/application/imagetest + diff --git a/backend/molgenis-emx2-io/src/main/java/org/molgenis/emx2/io/ImportTableTask.java b/backend/molgenis-emx2-io/src/main/java/org/molgenis/emx2/io/ImportTableTask.java index 2e942104f6..b8a42b442d 100644 --- a/backend/molgenis-emx2-io/src/main/java/org/molgenis/emx2/io/ImportTableTask.java +++ b/backend/molgenis-emx2-io/src/main/java/org/molgenis/emx2/io/ImportTableTask.java @@ -38,7 +38,7 @@ public void run() { try { source.processTable(table.getName(), new ImportRowProcesssor(table, this)); } catch (Exception e) { - this.setError("Import failed: " + e.getMessage()); + this.setError("Import table (%s) failed: %s".formatted(table.getName(), e.getMessage())); throw e; } @@ -176,12 +176,19 @@ public void process(Iterator iterator, TableStore source) { if (c.isFile() && source instanceof TableAndFileStore && row.getValueMap().get(c.getName()) != null) { - BinaryFileWrapper wrapper = - ((TableAndFileStore) source).getBinaryFileWrapper(row.getString(c.getName())); - if (row.containsName(c.getName() + "_filename")) { - wrapper.setFileName(row.getString(c.getName() + "_filename")); + try { + BinaryFileWrapper wrapper = + ((TableAndFileStore) source).getBinaryFileWrapper(row.getString(c.getName())); + if (row.containsName(c.getName() + "_filename")) { + wrapper.setFileName(row.getString(c.getName() + "_filename")); + } + row.setBinary(c.getName(), wrapper); + } catch (Exception e) { + throw new MolgenisException( + "Failed to read file attachment for column %s row %d" + .formatted(c.getName(), index), + e); } - row.setBinary(c.getName(), wrapper); } } batch.add(row);