Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add throwable type adapter #458

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.intellij.remoterobot

import com.google.gson.GsonBuilder
import com.intellij.remoterobot.client.IdeRobotApi
import com.intellij.remoterobot.client.IdeRobotClient
import com.intellij.remoterobot.data.RemoteComponent
Expand All @@ -14,6 +15,7 @@ import com.intellij.remoterobot.fixtures.Fixture
import com.intellij.remoterobot.search.Finder
import com.intellij.remoterobot.search.locators.Locator
import com.intellij.remoterobot.utils.DefaultHttpClient
import com.intellij.remoterobot.utils.ThrowableTypeAdapter
import com.intellij.remoterobot.utils.waitFor
import okhttp3.OkHttpClient
import org.intellij.lang.annotations.Language
Expand All @@ -28,15 +30,19 @@ import javax.imageio.ImageIO
@DslMarker
annotation class RemoteCommand


class RemoteRobot @JvmOverloads constructor(
robotServerUrl: String,
okHttpClient: OkHttpClient = DefaultHttpClient.client,
secret: String? = null
) : SearchContext, JavaScriptApi, LambdaApi {

private val gson = GsonBuilder()
.registerTypeAdapter(Throwable::class.java, ThrowableTypeAdapter)
.create()

override val ideRobotClient = IdeRobotClient(
Retrofit.Builder().baseUrl(robotServerUrl)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build()
.create(IdeRobotApi::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.intellij.remoterobot.utils

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter

object ThrowableTypeAdapter : TypeAdapter<Throwable>() {

override fun write(writer: JsonWriter, value: Throwable?) {
if (value == null) {
writer.nullValue()
return
}
writer.beginObject()

// Include exception type name to give more context; for example, NullPointerException might
// not have a message
writer.name("type")
writer.value(value::class.java.getSimpleName())

writer.name("message")
writer.value(value.message)

val cause = value.cause
if (cause != null) {
writer.name("cause")
write(writer, cause)
}

writer.endObject()
}

override fun read(reader: JsonReader): Throwable {
throw UnsupportedOperationException()
}
}