@@ -5,6 +5,7 @@ import com.project.codereview.client.github.GithubReviewClient
55import com.project.codereview.client.google.GoogleGeminiClient
66import com.project.codereview.core.dto.GithubPayload
77import com.project.codereview.core.dto.GithubReviewDto
8+ import org.slf4j.LoggerFactory
89import org.springframework.stereotype.Service
910
1011@Service
@@ -13,16 +14,38 @@ class ReviewWorker(
1314 private val githubReviewClient : GithubReviewClient ,
1415 private val failedTaskManager : FailedTaskManager
1516) {
17+ private val logger = LoggerFactory .getLogger(ReviewWorker ::class .java)
18+
1619 suspend fun process (payload : GithubPayload , task : DiffTaskPreparer .ReviewTask ) {
1720 val prompt = " ```diff\n ${task.part.content} \n ```"
1821 val filePath = task.part.filePath
1922
23+ val reviewResult = runCatching {
24+ googleGeminiClient.chat(filePath, prompt)
25+ }.onFailure { e ->
26+ logger.warn(" [Gemini error] - $payload , $task " )
27+ logger.warn(" [Gemini error] - {}" , e.message)
28+ handleGeminiError(e.message ? : " " , prompt, payload, task)
29+ }
30+
31+ val review = reviewResult.getOrNull() ? : return
32+
2033 runCatching {
21- val review = googleGeminiClient.chat(filePath, prompt)
22- if (review != null ) {
23- githubReviewClient.addReviewComment(GithubReviewDto (task.payload, task.part, payload.installation.id, review))
24- }
25- }.onFailure {
34+ githubReviewClient.addReviewComment(GithubReviewDto (task.payload, task.part, payload.installation.id, review))
35+ }.onFailure { e ->
36+ logger.warn(" [Github error] - $payload , $task " )
37+ logger.warn(" [Github error] - cause {}" , e.message)
38+ }
39+ }
40+
41+ private fun handleGeminiError (
42+ e : String ,
43+ prompt : String ,
44+ payload : GithubPayload ,
45+ task : DiffTaskPreparer .ReviewTask
46+ ) {
47+ val isTooManyRequestError = e.contains(" 429 Too Many Requests" ) || e.contains(" 429" )
48+ if (isTooManyRequestError) {
2649 failedTaskManager.add(FailedTaskManager .OriginalTask (payload, task.part), prompt)
2750 }
2851 }
0 commit comments