Skip to content

Commit 8605080

Browse files
Nivaldo Bondançafacebook-github-bot
authored andcommitted
Fix code in online formatter
Summary: Online formatter was using an API that got updated, but we didn't reflect that and our test only caught when we actually did the bump :-/ We really need to setup things so that this does not happen anymore, like better testing Reviewed By: davidtorosyan Differential Revision: D58271711 fbshipit-source-id: 1d7acfc9093cb673e3765cd7ed5d7908e782d819
1 parent dfdb66a commit 8605080

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

  • online_formatter/src/main/kotlin

online_formatter/src/main/kotlin/main.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package com.facebook.ktfmt.onlineformatter
1919
import com.amazonaws.services.lambda.runtime.Context
2020
import com.amazonaws.services.lambda.runtime.RequestHandler
2121
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
22+
import com.facebook.ktfmt.cli.ParseResult
2223
import com.facebook.ktfmt.cli.ParsedArgs
2324
import com.facebook.ktfmt.format.Formatter
2425
import com.google.gson.Gson
25-
import java.io.ByteArrayOutputStream
26-
import java.io.PrintStream
2726

2827
class Handler : RequestHandler<APIGatewayProxyRequestEvent, String> {
2928
init {
@@ -35,16 +34,24 @@ class Handler : RequestHandler<APIGatewayProxyRequestEvent, String> {
3534
return gson.toJson(
3635
try {
3736
val request = gson.fromJson(event.body, Request::class.java)
38-
val parsingErrors = ByteArrayOutputStream()
3937
val style = request.style
40-
val parsedArgs =
41-
ParsedArgs.parseOptions(
42-
PrintStream(parsingErrors), if (style == null) arrayOf() else arrayOf(style))
43-
Response(
44-
Formatter.format(
45-
parsedArgs.formattingOptions.copy(maxWidth = request.maxWidth ?: 100),
46-
request.source ?: ""),
47-
parsingErrors.toString().ifEmpty { null })
38+
val parseResult = ParsedArgs.parseOptions(listOfNotNull(style).toTypedArray())
39+
when (parseResult) {
40+
is ParseResult.Ok -> {
41+
val parsedArgs = parseResult.parsedValue
42+
Response(
43+
source =
44+
Formatter.format(
45+
parsedArgs.formattingOptions.copy(maxWidth = request.maxWidth ?: 100),
46+
request.source.orEmpty(),
47+
),
48+
err = null,
49+
)
50+
}
51+
is ParseResult.Error -> {
52+
Response(null, parseResult.errorMessage)
53+
}
54+
}
4855
} catch (e: Exception) {
4956
Response(null, e.message)
5057
})

0 commit comments

Comments
 (0)