Skip to content

Commit a0c6fc1

Browse files
committed
Moving statusCode datatype from string to int
1 parent 4698590 commit a0c6fc1

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import software.amazon.awssdk.services.codewhispererruntime.model.Range
2121
import software.amazon.awssdk.services.codewhispererruntime.model.StartTestGenerationResponse
2222
import software.amazon.awssdk.services.codewhispererruntime.model.TargetCode
2323
import software.amazon.awssdk.services.codewhispererruntime.model.TestGenerationJobStatus
24+
import software.amazon.awssdk.services.codewhispererruntime.model.ThrottlingException
2425
import software.amazon.awssdk.services.codewhispererstreaming.model.ExportContext
2526
import software.amazon.awssdk.services.codewhispererstreaming.model.ExportIntent
2627
import software.aws.toolkits.core.utils.debug
@@ -71,7 +72,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
7172

7273
private fun throwIfCancelled(session: Session) {
7374
if (!session.isGeneratingTests) {
74-
throw testGenStoppedError()
75+
testGenStoppedError()
7576
}
7677
}
7778

@@ -128,13 +129,15 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
128129
userInput = prompt
129130
)
130131
} catch (e: Exception) {
131-
LOG.error(e) { "Failed to create test generation job" }
132-
// TODO: Not able to emit e.statusCode directly as statusCode is private property
133-
// Cannot access 'statusCode': it is invisible (private in a supertype) in 'ThrottlingException'
132+
val statusCode = when {
133+
e is ThrottlingException -> e.statusCode()
134+
else -> 500
135+
}
136+
LOG.error(e) { "Unexpected error while creating test generation job" }
134137
val errorMessage = getTelemetryErrorMessage(e, CodeWhispererConstants.FeatureName.TEST_GENERATION)
135-
throw codeTestServerException(
138+
codeTestServerException(
136139
"CreateTestJobError: $errorMessage",
137-
"500",
140+
statusCode,
138141
"CreateTestJobError",
139142
message("testgen.error.generic_technical_error_message")
140143
)
@@ -191,9 +194,9 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
191194
// update test summary card
192195
} else {
193196
// If job status is Completed and has no ShortAnswer then there might be some issue in the backend.
194-
throw codeTestServerException(
197+
codeTestServerException(
195198
"TestGenFailedError: " + message("testgen.message.failed"),
196-
"500",
199+
500,
197200
"TestGenFailedError",
198201
message("testgen.error.generic_technical_error_message")
199202
)
@@ -206,15 +209,14 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
206209
if (testGenerationResponse.testGenerationJob().shortAnswer() != null) {
207210
shortAnswer = parseShortAnswerString(testGenerationResponse.testGenerationJob().shortAnswer())
208211
if (shortAnswer.stopIteration == "true") {
209-
throw Exception("${shortAnswer.planSummary}")
210-
throw codeTestServerException("TestGenFailedError: ${shortAnswer.planSummary}", "400", "TestGenFailedError", shortAnswer.planSummary)
212+
codeTestServerException("TestGenFailedError: ${shortAnswer.planSummary}", 400, "TestGenFailedError", shortAnswer.planSummary)
211213
}
212214
}
213215

214216
// If job status is Failed and has no ShortAnswer then there might be some issue in the backend.
215-
throw codeTestServerException(
217+
codeTestServerException(
216218
"TestGenFailedError: " + message("testgen.message.failed"),
217-
"500",
219+
500,
218220
"TestGenFailedError",
219221
message("testgen.error.generic_technical_error_message")
220222
)
@@ -229,7 +231,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
229231
if (previousIterationContext == null && testGenerationResponse.testGenerationJob().shortAnswer() != null) {
230232
shortAnswer = parseShortAnswerString(testGenerationResponse.testGenerationJob().shortAnswer())
231233
if (shortAnswer.stopIteration == "true") {
232-
throw codeTestServerException("TestGenFailedError: ${shortAnswer.planSummary}", "400", "TestGenFailedError", shortAnswer.planSummary)
234+
codeTestServerException("TestGenFailedError: ${shortAnswer.planSummary}", 400, "TestGenFailedError", shortAnswer.planSummary)
233235
}
234236
codeTestChatHelper.updateAnswer(
235237
CodeTestChatMessageContent(
@@ -261,9 +263,9 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
261263
},
262264
{ e ->
263265
LOG.error(e) { "ExportResultArchive failed: ${e.message}" }
264-
throw codeTestServerException(
266+
codeTestServerException(
265267
"ExportResultsArchiveError: ${e.message}",
266-
"500",
268+
500,
267269
"ExportResultsArchiveError",
268270
message("testgen.error.generic_technical_error_message")
269271
)
@@ -529,7 +531,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
529531
} catch (e: Exception) {
530532
// Add an answer for displaying error message
531533
val errorMessage = when {
532-
e is CodeTestException && e.statusCode == "400" &&
534+
e is CodeTestException && e.statusCode == 400 &&
533535
e.message?.startsWith("CreateTestJobError: Maximum") == true ->
534536
message("testgen.error.maximum_generations_reach")
535537

@@ -556,7 +558,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin
556558
result = if (e.message == message("testgen.message.cancelled")) MetricResult.Cancelled else MetricResult.Failed,
557559
reason = (e as CodeTestException).code ?: "DefaultError",
558560
reasonDesc = if (e.message == message("testgen.message.cancelled")) "${e.code}: ${e.message}" else e.message,
559-
httpStatusCode = e.statusCode ?: "400",
561+
httpStatusCode = e.statusCode.toString(),
560562
perfClientLatency = (Instant.now().toEpochMilli() - session.startTimeOfTestGeneration),
561563
isCodeBlockSelected = session.isCodeBlockSelected,
562564
artifactsUploadDuration = session.artifactUploadDuration,

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ class CodeTestChatController(
605605
artifactsUploadDuration = session.artifactUploadDuration,
606606
buildPayloadBytes = session.srcPayloadSize,
607607
buildZipFileBytes = session.srcZipFileSize,
608-
requestId = session.startTestGenerationRequestId
608+
requestId = session.startTestGenerationRequestId,
609+
httpStatusCode = "200"
609610
)
610611
codeTestChatHelper.addAnswer(
611612
CodeTestChatMessageContent(
@@ -799,7 +800,8 @@ class CodeTestChatController(
799800
artifactsUploadDuration = session.artifactUploadDuration,
800801
buildPayloadBytes = session.srcPayloadSize,
801802
buildZipFileBytes = session.srcZipFileSize,
802-
requestId = session.startTestGenerationRequestId
803+
requestId = session.startTestGenerationRequestId,
804+
httpStatusCode = "200"
803805
)
804806
sessionCleanUp(message.tabId)
805807
}

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/codetest/CodeTestException.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ import software.aws.toolkits.resources.message
77

88
open class CodeTestException(
99
override val message: String?,
10-
val statusCode: String? = "400",
10+
val statusCode: Int = 400,
1111
val code: String? = "DefaultError",
1212
val uiMessage: String? = message(
1313
"testgen.error.generic_error_message"
1414
),
1515
) : RuntimeException()
1616

1717
internal fun noFileOpenError(): Nothing =
18-
throw CodeTestException(message("codewhisperer.codescan.no_file_open"), "400", "ProjectZipError")
18+
throw CodeTestException(message("codewhisperer.codescan.no_file_open"), 400, "ProjectZipError")
1919

2020
internal fun fileTooLarge(): Nothing =
21-
throw CodeTestException(message("codewhisperer.codescan.file_too_large_telemetry"), "400", "ProjectZipError")
21+
throw CodeTestException(message("codewhisperer.codescan.file_too_large_telemetry"), 400, "ProjectZipError")
2222

2323
internal fun cannotFindFile(errorMessage: String, filepath: String): Nothing =
2424
error(message("codewhisperer.codescan.file_not_found", filepath, errorMessage))
2525

2626
internal fun cannotFindValidFile(errorMessage: String): Nothing =
27-
throw CodeTestException(errorMessage, "400", "ProjectZipError")
27+
throw CodeTestException(errorMessage, 400, "ProjectZipError")
2828

2929
internal fun cannotFindBuildArtifacts(errorMessage: String): Nothing =
30-
throw CodeTestException(errorMessage, "400", "ProjectZipError")
30+
throw CodeTestException(errorMessage, 400, "ProjectZipError")
3131

3232
internal fun invalidSourceZipError(): Nothing =
33-
throw CodeTestException(message("codewhisperer.codescan.invalid_source_zip_telemetry"), "400", "InvalidSourceZipError")
33+
throw CodeTestException(message("codewhisperer.codescan.invalid_source_zip_telemetry"), 400, "InvalidSourceZipError")
3434

3535
fun codeTestServerException(
3636
errorMessage: String,
37-
statusCode: String?,
37+
statusCode: Int,
3838
code: String? = "DefaultError",
3939
uiMessage: String? = message(
4040
"testgen.error.generic_technical_error_message"
@@ -43,4 +43,4 @@ fun codeTestServerException(
4343
throw CodeTestException(errorMessage, statusCode, code, uiMessage)
4444

4545
fun testGenStoppedError(): Nothing =
46-
throw CodeTestException(message("testgen.message.cancelled"), "400", "TestGenCancelled", message("testgen.message.cancelled"))
46+
throw CodeTestException(message("testgen.message.cancelled"), 400, "TestGenCancelled", message("testgen.message.cancelled"))

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/codetest/sessionconfig/CodeTestSessionConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CodeTestSessionConfig(
9595
else -> e.message
9696
}
9797
LOG.debug { "Error creating payload metadata: $errorMessage" }
98-
throw cannotFindBuildArtifacts(errorMessage ?: message("testgen.message.failed"))
98+
cannotFindBuildArtifacts(errorMessage ?: message("testgen.message.failed"))
9999
}
100100

101101
// Copy all the included source files to the source zip
@@ -219,7 +219,7 @@ class CodeTestSessionConfig(
219219

220220
if (maxCountLanguage == null) {
221221
programmingLanguage = CodeWhispererUnknownLanguage.INSTANCE
222-
throw cannotFindValidFile("Amazon Q: doesn't contain valid files to generate tests")
222+
cannotFindValidFile("Amazon Q: doesn't contain valid files to generate tests")
223223
}
224224
programmingLanguage = maxCountLanguage
225225
return PayloadMetadata(files, currentTotalFileSize, currentTotalLines, maxCountLanguage.toTelemetryType())

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererZipUploadManager.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ class CodeWhispererZipUploadManager(private val project: Project) {
107107
LOG.debug { "$featureUseCase: Artifact failed to upload in the S3 bucket: ${e.message}" }
108108
val errorMessage = getTelemetryErrorMessage(e, featureUseCase)
109109
when (featureUseCase) {
110-
CodeWhispererConstants.FeatureName.CODE_REVIEW -> throw codeScanServerException("CreateUploadUrlException: $errorMessage")
111-
CodeWhispererConstants.FeatureName.TEST_GENERATION -> throw codeTestServerException(
110+
CodeWhispererConstants.FeatureName.CODE_REVIEW -> codeScanServerException("CreateUploadUrlException: $errorMessage")
111+
CodeWhispererConstants.FeatureName.TEST_GENERATION -> codeTestServerException(
112112
"UploadTestArtifactToS3Error: $errorMessage",
113-
"403",
113+
403,
114114
"UploadTestArtifactToS3Error",
115115
message("testgen.error.generic_technical_error_message")
116116
)
@@ -145,10 +145,10 @@ class CodeWhispererZipUploadManager(private val project: Project) {
145145
LOG.debug { "$featureUseCase: Create Upload URL failed: ${e.message}" }
146146
val errorMessage = getTelemetryErrorMessage(e, featureUseCase)
147147
when (featureUseCase) {
148-
CodeWhispererConstants.FeatureName.CODE_REVIEW -> throw codeScanServerException("CreateUploadUrlException: $errorMessage")
149-
CodeWhispererConstants.FeatureName.TEST_GENERATION -> throw codeTestServerException(
148+
CodeWhispererConstants.FeatureName.CODE_REVIEW -> codeScanServerException("CreateUploadUrlException: $errorMessage")
149+
CodeWhispererConstants.FeatureName.TEST_GENERATION -> codeTestServerException(
150150
"CreateUploadUrlError: $errorMessage",
151-
"500",
151+
500,
152152
"CreateUploadUrlError",
153153
message("testgen.error.generic_technical_error_message")
154154
)

0 commit comments

Comments
 (0)