Skip to content

Commit 6eae041

Browse files
committed
add codeNotGenerated error
1 parent d403a9f commit 6eae041

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.skku.se7.error.exceptions;
2+
3+
public class CodeNotGeneratedException extends RuntimeException{
4+
}

backend/src/main/java/com/skku/se7/error/handler/ControllerExceptionHandler.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ public ResponseEntity<ExceptionResponse> timeOutException(TimeOutException e, Ht
171171
return ResponseEntity.badRequest().body(ExceptionResponse.fromException(e, request, exceptionDetail));
172172
}
173173

174+
@ExceptionHandler(CodeNotGeneratedException.class)
175+
public ResponseEntity<ExceptionResponse> codeNotGeneratedException(CodeNotGeneratedException e, HttpServletRequest request) throws IOException{
176+
String javaCode = getRequestJson(request, GreenRequest.class).getJavaCode();
177+
ExceptionDetail exceptionDetail = ExceptionDetail.builder()
178+
.field("javaCode")
179+
.given(javaCode)
180+
.reasonMessage("실행할 파일이 존재하지 않습니다!!")
181+
.build();
182+
return ResponseEntity.badRequest().body(ExceptionResponse.fromException(e, request, exceptionDetail));
183+
}
184+
174185
private <T> T getRequestJson(HttpServletRequest request, Class<T> jsonType) throws IOException {
175186
ServletInputStream inputStream = request.getInputStream();
176187
String messageBody = StreamUtils.copyToString(inputStream,

backend/src/main/java/com/skku/se7/service/converter/code/JavaCodeCompiler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.skku.se7.service.converter.code;
22

3+
import com.skku.se7.error.exceptions.CodeNotGeneratedException;
34
import com.skku.se7.error.exceptions.CompileException;
45
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
@@ -67,6 +68,9 @@ public static String[] createFile(String className, String code){
6768
public static void compileSourceFile(String filePath){
6869
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
6970
File sourceFile = new File(filePath);
71+
if(!sourceFile.exists()){
72+
throw new CodeNotGeneratedException();
73+
}
7074

7175
OutputStream outputStream = new OutputStream() {
7276
private StringBuilder sb = new StringBuilder();
@@ -87,5 +91,4 @@ public String toString() {
8791
throw new CompileException();
8892
}
8993
}
90-
9194
}

backend/src/test/java/com/skku/se7/service/JavaRunnerTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package com.skku.se7.service;
22

3-
import com.skku.se7.error.exceptions.CannotFindMainMethodException;
4-
import com.skku.se7.error.exceptions.CompileException;
5-
import com.skku.se7.error.exceptions.MaliciousCodeException;
6-
7-
import com.skku.se7.error.exceptions.TimeOutException;
8-
9-
import com.skku.se7.error.exceptions.MaliciousImportException;
3+
import com.skku.se7.error.exceptions.*;
104

115
import com.skku.se7.service.converter.code.JavaCodeCompiler;
126
import com.skku.se7.service.converter.code.JavaRunner;
@@ -17,6 +11,7 @@
1711
import org.springframework.beans.factory.annotation.Autowired;
1812
import org.springframework.boot.test.context.SpringBootTest;
1913

14+
import java.security.SecureRandom;
2015
import java.util.concurrent.TimeoutException;
2116

2217
@SpringBootTest
@@ -332,4 +327,12 @@ public void Timeout() throws Exception{
332327
//then
333328
Assertions.assertThrows(TimeOutException.class, () -> codeConverter.executeSynchronously(infiniteLoop));
334329
}
330+
331+
@Test
332+
public void codeGenerateTest() throws Exception{
333+
//given
334+
String path = "wrongPath";
335+
336+
Assertions.assertThrows(CodeNotGeneratedException.class, ()->javaCodeCompiler.compileSourceFile(path));
337+
}
335338
}

0 commit comments

Comments
 (0)