Skip to content

Commit

Permalink
fix: Excel API exception handling. (#4627)
Browse files Browse the repository at this point in the history
* Extracted creation of temp file from output stream, so exception gets handled correctly

* Added explicit error message when writing xls cell contents

* Fixed error handling for all xls api mappings
  • Loading branch information
harmbrugge authored Jan 23, 2025
1 parent 9e3c3af commit ab7be0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,19 @@ private void writeRowsToSheet(
// write a contents row
org.apache.poi.ss.usermodel.Row excelRow = sheet.createRow(rowNum);
for (Map.Entry<String, Integer> entry : columnNameIndexMap.entrySet()) {
excelRow.createCell(entry.getValue()).setCellValue(row.getString(entry.getKey()));
try {
excelRow.createCell(entry.getValue()).setCellValue(row.getString(entry.getKey()));
} catch (IllegalArgumentException e) {
throw new MolgenisException(
"Error writing table '"
+ name
+ "', column '"
+ entry.getKey()
+ "', at row "
+ rowNum
+ ": "
+ e.getMessage());
}
}
rowNum++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ static void getExcel(Context ctx) throws IOException {
boolean includeSystemColumns = includeSystemColumns(ctx);
Path tempDir = Files.createTempDirectory(MolgenisWebservice.TEMPFILES_DELETE_ON_EXIT);
tempDir.toFile().deleteOnExit();
try (OutputStream outputStream = ctx.res().getOutputStream()) {
Path excelFile = tempDir.resolve("download.xlsx");
if (ctx.queryParam("emx1") != null) {
MolgenisIO.toEmx1ExcelFile(excelFile, schema);
} else {
MolgenisIO.toExcelFile(excelFile, schema, includeSystemColumns);
}

Path excelFile = tempDir.resolve("download.xlsx");
if (ctx.queryParam("emx1") != null) {
MolgenisIO.toEmx1ExcelFile(excelFile, schema);
} else {
MolgenisIO.toExcelFile(excelFile, schema, includeSystemColumns);
}

try (OutputStream outputStream = ctx.outputStream()) {
ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
ctx.header(
"Content-Disposition",
Expand All @@ -99,11 +100,11 @@ static void getExcelTable(Context ctx) throws IOException {
Table table = MolgenisWebservice.getTableByIdOrName(ctx);
Path tempDir = Files.createTempDirectory(MolgenisWebservice.TEMPFILES_DELETE_ON_EXIT);
tempDir.toFile().deleteOnExit();
Path excelFile = tempDir.resolve("download.xlsx");
TableStore excelStore = new TableStoreForXlsxFile(excelFile);
excelStore.writeTable(
table.getName(), getDownloadColumns(ctx, table), getDownloadRows(ctx, table));
try (OutputStream outputStream = ctx.res().getOutputStream()) {
Path excelFile = tempDir.resolve("download.xlsx");
TableStore excelStore = new TableStoreForXlsxFile(excelFile);
excelStore.writeTable(
table.getName(), getDownloadColumns(ctx, table), getDownloadRows(ctx, table));
ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
ctx.header(
"Content-Disposition",
Expand All @@ -121,10 +122,10 @@ static void getExcelTable(Context ctx) throws IOException {
static void getExcelReport(Context ctx) throws IOException {
Path tempDir = Files.createTempDirectory(MolgenisWebservice.TEMPFILES_DELETE_ON_EXIT);
tempDir.toFile().deleteOnExit();
Path excelFile = tempDir.resolve("download.xlsx");
TableStore excelStore = new TableStoreForXlsxFile(excelFile);
generateReportsToStore(ctx, excelStore);
try (OutputStream outputStream = ctx.res().getOutputStream()) {
Path excelFile = tempDir.resolve("download.xlsx");
TableStore excelStore = new TableStoreForXlsxFile(excelFile);
generateReportsToStore(ctx, excelStore);
ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
ctx.header("Content-Disposition", "attachment; filename=report.xlsx");
outputStream.write(Files.readAllBytes(excelFile));
Expand Down

0 comments on commit ab7be0c

Please sign in to comment.