Skip to content

Commit

Permalink
Throw FileStorageException and handle in ResponseEntityExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
pvannierop committed Jul 3, 2024
1 parent 8f215d6 commit 8eba6f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.radarbase.appserver.exception;

public class FileStorageException extends RuntimeException {
private static final long serialVersionUID = -793674245766939L;

public FileStorageException(String message) {
super(message);
}

public FileStorageException(String message, Object object) {
super(message + " " + object.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public final ResponseEntity<ErrorDetails> handleInvalidUserDetailsException(Exce
return handleEntityWithCause(ex, request);
}

@ExceptionHandler(FileStorageException.class)
public final ResponseEntity<ErrorDetails> handleFileStorageException(Exception ex, WebRequest request) {
return handleEntityWithCause(ex, request);
}

public ResponseEntity<ErrorDetails> handleEntityWithCause(Exception ex, WebRequest request) {
String cause = ex.getCause() != null ? ex.getCause().getMessage() : null;
HttpStatus status = ex.getClass().getAnnotation(ResponseStatus.class).value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.radarbase.appserver.config.S3StorageProperties;
import org.radarbase.appserver.exception.FileStorageException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -62,7 +63,7 @@ public String store(MultipartFile file, String projectId, String subjectId, Stri
.stream(file.getInputStream(), file.getSize(), -1)
.build());
} catch (Exception e) {
throw new RuntimeException("Could not store file", e);
throw new FileStorageException("There was a problem storing the file on the object storage", e);
}
return filePath.getFullPath();
}
Expand Down

0 comments on commit 8eba6f8

Please sign in to comment.