Skip to content

Commit

Permalink
feat: FileException 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
gitchannn committed Mar 5, 2024
1 parent 0577060 commit e50818e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sunflower.server.application.TranslationService;
import sunflower.server.application.dto.BrfFileDto;
import sunflower.server.application.dto.TranslationStatusDto;
import sunflower.server.exception.ErrorCode;
import sunflower.server.exception.FileException;

import java.net.URI;
Expand All @@ -31,7 +32,7 @@ public class TranslationApiController implements TranslationApiControllerDocs {
@PostMapping(consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<PdfRegisterResponse> registerPdf(@RequestPart("file") MultipartFile file) {
if (file.isEmpty()) {
throw new FileException(1, "빈 파일입니다.");
throw new FileException(ErrorCode.P);
}

final Long id = translationService.register(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sunflower.server.application.event.BrailleTranslateEvent;
import sunflower.server.client.BrailleTranslationClient;
import sunflower.server.entity.Translations;
import sunflower.server.exception.FileException;
import sunflower.server.repository.TranslationsRepository;
import sunflower.server.util.FileUtil;

Expand Down Expand Up @@ -44,7 +45,7 @@ public void downloadLatexFile(final BrailleTranslateEvent event) {
final File latexFile = FileUtil.findFile(translations.getLatexPath());

if (!latexFile.exists()) {
throw new RuntimeException("파일이 존재하지 않습니다!");
throw new FileException("파일이 존재하지 않습니다!");
}

final String brfContent = brailleTranslationClient.translate(latexFile);
Expand Down
29 changes: 23 additions & 6 deletions server/src/main/java/sunflower/server/exception/FileException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@
@Getter
public class FileException extends RuntimeException {

private final int code;
private final ErrorCode code;
private final String message;

public FileException(final int code, final String message) {
super(message);
public FileException(final ErrorCode code, final Throwable cause, final String message) {
super(cause);
this.code = code;
this.message = message;
}

public FileException(final int code, final String message, final Throwable cause) {
super(message, cause);
this.code = code;
public FileException(final ErrorCode code, final Throwable cause) {
this(code, cause, null);
}

public FileException(final Throwable cause, final String message) {
this(null, cause, message);
}

public FileException(final ErrorCode code) {
this(code, null, null);
}

public FileException(final Throwable cause) {
this(null, cause, null);
}

public FileException(final String message) {
this(null, null, message);
}
}
14 changes: 8 additions & 6 deletions server/src/main/java/sunflower/server/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import sunflower.server.exception.ErrorCode;
import sunflower.server.exception.FileException;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -22,7 +24,7 @@ public static String saveFile(Object file, String fileName, Path directoryPath)
try {
Files.createDirectories(directoryPath);
} catch (IOException e) {
throw new RuntimeException("Unable to create directory: " + directoryPath, e);
throw new FileException(e, "Unable to create directory: " + directoryPath);
}
}

Expand All @@ -34,7 +36,7 @@ public static String saveFile(Object file, String fileName, Path directoryPath)
} else if (file instanceof byte[]) {
path = saveZipFile((byte[]) file, fileName, directoryPath);
} else {
throw new IllegalArgumentException("Unsupported file type");
throw new FileException("Unsupported file type");
}

return path.toString();
Expand All @@ -46,7 +48,7 @@ private static Path saveMultipartFile(MultipartFile file, String fileName, Path
Files.copy(file.getInputStream(), filePath);
return filePath;
} catch (IOException e) {
throw new RuntimeException(e);
throw new FileException(ErrorCode.P, e);
}
}

Expand All @@ -56,7 +58,7 @@ private static Path saveStringContent(String content, String fileName, Path dire
writer.write(content);
return filePath;
} catch (IOException e) {
throw new RuntimeException(e);
throw new FileException(ErrorCode.B, e);
}
}

Expand All @@ -74,7 +76,7 @@ private static Path saveZipFile(byte[] content, String fileName, Path directoryP
}
return Paths.get(path);
} catch (IOException e) {
throw new RuntimeException(e);
throw new FileException(ErrorCode.L, e);
}
}

Expand All @@ -90,7 +92,7 @@ public static String readFile(final File file) {
try {
return new String(Files.readAllBytes(Paths.get(file.getPath())));
} catch (IOException e) {
throw new RuntimeException(e);
throw new FileException(e);
}
}
}

0 comments on commit e50818e

Please sign in to comment.