Skip to content

Commit 9e49f3c

Browse files
authored
Revise file/attachment exception handling (#6949)
1 parent 463bb63 commit 9e49f3c

3 files changed

Lines changed: 36 additions & 26 deletions

File tree

api/src/org/labkey/api/assay/AbstractAssayTsvDataHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ else if (o instanceof MvFieldWrapper mvWrapper)
943943
}
944944
catch (ConvertHelper.FileConversionException e)
945945
{
946-
throw new ApiUsageException(e);
946+
errors.add(new PropertyValidationError(e.getMessage(), pd.getName()));
947947
}
948948
}
949949
}

api/src/org/labkey/api/assay/DefaultAssayRunCreator.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,23 +482,25 @@ public ExpExperiment saveExperimentRun(
482482

483483
return batch;
484484
}
485-
catch (ExperimentException | IOException | ConvertHelper.FileConversionException e)
485+
catch (ExperimentException | IOException | ConvertHelper.FileConversionException | BatchValidationException e)
486486
{
487+
// TODO: This is better done as a post-rollback task on the transaction
487488
// clean up the run results file dir here if it was created, for non-async imports
488489
AssayResultsFileWriter<?> resultsFileWriter = new AssayResultsFileWriter<>(context.getProtocol(), run, null);
489490
resultsFileWriter.cleanupPostedFiles(context.getContainer(), false);
490491

491492
cleanPrimaryFile(context);
492493

493-
if (e instanceof ExperimentException)
494-
throw (ExperimentException)e;
495-
else if (e instanceof ConvertHelper.FileConversionException)
496-
throw new ApiUsageException(e.getMessage(), e);
497-
else
498-
throw new ExperimentException(e);
499-
}
500-
catch (BatchValidationException e)
501-
{
494+
if (e instanceof ExperimentException ee)
495+
throw ee;
496+
497+
// HACK: Rethrowing these as ApiUsageException avoids any upstream consequences of wrapping them in ExperimentException.
498+
// Namely, that they are logged to the server/mothership. There has to be a better way.
499+
if (e instanceof ConvertHelper.FileConversionException fce)
500+
throw new ApiUsageException(fce.getMessage(), fce);
501+
else if (e instanceof BatchValidationException bve)
502+
throw new ApiUsageException(bve.getMessage(), bve);
503+
502504
throw new ExperimentException(e);
503505
}
504506
}

api/src/org/labkey/api/dataiterator/AttachmentDataIterator.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
import java.io.InputStream;
4444
import java.util.ArrayList;
4545

46-
/**
47-
* Moved from ListQueryUpdateService.java
48-
* by iansigmon on 2/16/16.
49-
*/
5046
public class AttachmentDataIterator extends WrapperDataIterator
5147
{
5248
final VirtualFile attachmentDir;
@@ -92,19 +88,21 @@ public boolean next() throws BatchValidationException
9288
for (_AttachmentUploadHelper p : attachmentColumns)
9389
{
9490
Object attachmentValue = get(p.index);
91+
if (null == attachmentValue)
92+
continue;
93+
9594
String filename;
9695
AttachmentFile attachmentFile;
9796

98-
if (null == attachmentValue)
99-
continue;
100-
else if (attachmentValue instanceof String str)
97+
if (attachmentValue instanceof String str)
10198
{
10299
if (null == attachmentDir)
103100
{
104-
errors.addRowError(new ValidationException("Row " + get(0) + ": " + "Can't upload '" + str + "' to field " + p.domainProperty.getName() + " with type " + p.domainProperty.getType().getLabel() + "."));
101+
errors.addRowError(propertyValidationException(p.domainProperty, attachmentValue));
105102
return false;
106103
}
107-
filename = (String) attachmentValue;
104+
105+
filename = str;
108106
InputStream aIS = attachmentDir.getDir(p.domainProperty.getName()).getInputStream(p.uniquifier.uniquify(filename));
109107
if (aIS == null)
110108
{
@@ -113,25 +111,25 @@ else if (attachmentValue instanceof String str)
113111
}
114112
attachmentFile = new InputStreamAttachmentFile(aIS, filename);
115113
}
116-
else if (attachmentValue instanceof AttachmentFile)
114+
else if (attachmentValue instanceof AttachmentFile file)
117115
{
118-
attachmentFile = (AttachmentFile) attachmentValue;
116+
attachmentFile = file;
119117
filename = attachmentFile.getFilename();
120118
}
121-
else if (attachmentValue instanceof File)
119+
else if (attachmentValue instanceof File file)
122120
{
123-
attachmentFile = new FileAttachmentFile((File) attachmentValue);
121+
attachmentFile = new FileAttachmentFile(file);
124122
filename = attachmentFile.getFilename();
125123
}
126124
else
127125
{
128-
errors.addRowError(new ValidationException("Row " + get(0) + ": " + "Unable to create attachment file."));
126+
errors.addRowError(propertyValidationException(p.domainProperty, attachmentValue));
129127
return false;
130128
}
131129

132130
if (entityIdIndex == 0)
133131
{
134-
errors.addRowError(new ValidationException("Row " + get(0) + ": " + "Unable to create attachment file."));
132+
errors.addRowError(rowValidationException("Unable to create attachment file."));
135133
return false;
136134
}
137135

@@ -172,6 +170,16 @@ else if (attachmentValue instanceof File)
172170
}
173171
}
174172

173+
private ValidationException propertyValidationException(DomainProperty property, Object value)
174+
{
175+
return rowValidationException(String.format("Can't upload '%s' to field %s with type %s.", value, property.getName(), property.getType().getLabel()));
176+
}
177+
178+
private ValidationException rowValidationException(String message)
179+
{
180+
return new ValidationException("Row " + get(0) + ": " + message);
181+
}
182+
175183
public static DataIteratorBuilder getAttachmentDataIteratorBuilder(TableInfo ti, @NotNull final DataIteratorBuilder builder,
176184
final User user,
177185
@Nullable final VirtualFile attachmentDir,

0 commit comments

Comments
 (0)