Skip to content

Commit c086694

Browse files
committed
unify errors
1 parent 9ee58cf commit c086694

File tree

4 files changed

+27
-48
lines changed

4 files changed

+27
-48
lines changed

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/pluginapi/Command.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class Command {
3434
val query = apiUrl.query ?: return null // get query string or return null if none
3535

3636
val commandName = query.split("&")
37-
.mapNotNull { it.split("=", limit = 2).takeIf { it.size == 2 } }
37+
.mapNotNull { it -> it.split("=", limit = 2).takeIf { it.size == 2 } }
3838
.firstOrNull { it[0] == CommandQueryName }
3939
?.let { URLDecoder.decode(it[1], StandardCharsets.UTF_8) }
4040

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/pluginapi/PluginApiHttpResponse.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.digma.intellij.plugin.ui.jcef.pluginapi
22

3+
import org.digma.intellij.plugin.common.objectToJson
34
import java.io.InputStream
45

56
class PluginApiHttpResponse(
@@ -13,4 +14,20 @@ class PluginApiHttpResponse(
1314
//don't convert contentStream to string, this input stream should be read by the http layer. and it may be a large stream.
1415
return "PluginApiHttpResponse(status=$status, headers=$headers, contentLength=$contentLength, contentType=$contentType, contentStream=$contentStream)"
1516
}
17+
18+
companion object {
19+
fun createErrorResponse(statusCode: Int, error: ErrorData): PluginApiHttpResponse {
20+
val errorJson = objectToJson(error).toByteArray()
21+
return PluginApiHttpResponse(
22+
statusCode,
23+
headers = mutableMapOf(
24+
"Content-Type" to "application/json",
25+
"Content-Length" to errorJson.size.toString()
26+
),
27+
contentLength = errorJson.size.toLong(),
28+
contentType = "application/json",
29+
contentStream = errorJson.inputStream()
30+
)
31+
}
32+
}
1633
}

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/pluginapi/PluginApiResourceHandler.kt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ class PluginApiResourceHandler(val project: Project) : CefResourceHandler {
4141
request.identifier
4242
)
4343

44-
apiResponse = PluginApiHttpResponse(
45-
500,
46-
mutableMapOf(),
47-
null,
48-
"text/plain",
49-
"encountered multi part post data. it is not supported by Digma plugin api handler.".byteInputStream()
50-
)
44+
val error = ErrorData("encountered multi part post data. it is not supported by Digma plugin api handler.")
45+
apiResponse = PluginApiHttpResponse.createErrorResponse(500, error)
5146
callback.Continue()
5247
return true
5348
}
@@ -63,13 +58,8 @@ class PluginApiResourceHandler(val project: Project) : CefResourceHandler {
6358
request.identifier
6459
)
6560

66-
apiResponse = PluginApiHttpResponse(
67-
500,
68-
mutableMapOf(),
69-
null,
70-
"text/plain",
71-
"encountered file element in post data. it is not supported by Digma plugin api handler.".byteInputStream()
72-
)
61+
val error = ErrorData("encountered file element in post data. it is not supported by Digma plugin api handler.")
62+
apiResponse = PluginApiHttpResponse.createErrorResponse(500, error)
7363
callback.Continue()
7464
return true
7565
}
@@ -115,13 +105,8 @@ class PluginApiResourceHandler(val project: Project) : CefResourceHandler {
115105
Log.warnWithException(logger, e, "processRequest {} failed, [request id:{}]", requestUrl, requestId)
116106
ErrorReporter.Companion.getInstance().reportError("ApiProxyResourceHandler.processRequest", e)
117107

118-
apiResponse = PluginApiHttpResponse(
119-
500,
120-
mutableMapOf(),
121-
null,
122-
"text/plain",
123-
"encountered exception in proxy [$e]. please check the logs".byteInputStream()
124-
)
108+
val error = ErrorData("encountered exception in plugin api handler [$e]. please check the logs.")
109+
apiResponse = PluginApiHttpResponse.createErrorResponse(500, error)
125110
} finally {
126111
callback.Continue()
127112
}

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/pluginapi/RecentActivityBadgeCommand.kt

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.digma.intellij.plugin.ui.jcef.pluginapi
33
import com.fasterxml.jackson.annotation.JsonCreator
44
import com.fasterxml.jackson.annotation.JsonProperty
55
import com.intellij.openapi.project.Project
6-
import org.digma.intellij.plugin.common.objectToJson
76
import org.digma.intellij.plugin.ui.jcef.jsonToObject
87
import org.digma.intellij.plugin.ui.recentactivity.RecentActivityToolWindowIconChanger
98
import java.beans.ConstructorProperties
@@ -24,17 +23,7 @@ class RecentActivityBadgeCommand : Command() {
2423
if (badgeRequest == null) {
2524
// Return 400 Bad Request, with a small error body
2625
val error = ErrorData("Invalid or missing badge request")
27-
val errorJson = objectToJson(error).toByteArray()
28-
return PluginApiHttpResponse(
29-
status = 400,
30-
headers = mutableMapOf(
31-
"Content-Type" to "application/json",
32-
"Content-Length" to errorJson.size.toString()
33-
),
34-
contentLength = errorJson.size.toLong(),
35-
contentType = "application/json",
36-
contentStream = errorJson.inputStream()
37-
)
26+
return PluginApiHttpResponse.createErrorResponse(400, error)
3827
}
3928

4029
val recentActivityToolWindowIconChanger = RecentActivityToolWindowIconChanger.getInstance(project)
@@ -52,21 +41,9 @@ class RecentActivityBadgeCommand : Command() {
5241
contentStream = null
5342
)
5443
} catch (e: Throwable) {
55-
val errorMessage = e.message ?: e.toString()
56-
val error = ErrorData(errorMessage)
57-
val errorJson = objectToJson(error).toByteArray()
58-
return PluginApiHttpResponse(
59-
status = 500,
60-
headers = mutableMapOf(
61-
"Content-Type" to "application/json",
62-
"Content-Length" to errorJson.size.toString()
63-
),
64-
contentLength = errorJson.size.toLong(),
65-
contentType = "application/json",
66-
contentStream = errorJson.inputStream()
67-
)
44+
val error = ErrorData(e.message ?: e.toString())
45+
return PluginApiHttpResponse.createErrorResponse(500, error)
6846
}
69-
7047
}
7148
}
7249

0 commit comments

Comments
 (0)