gpt4 book ai didi

android - 抛出异常并在 "error"级别的 logcat 中打印其堆栈跟踪

转载 作者:行者123 更新时间:2023-11-29 23:05:38 28 4
gpt4 key购买 nike

我有一个可抛出的对象,我想在 logcat 中使用“Log.e”日志记录级别(错误)打印它的堆栈跟踪。我的方法抛出异常,但堆栈跟踪打印为警告。

这是我的代码:

@Throws(RuntimeException::class)
fun throwErrorIfNotHttp (e: Throwable) {
if (e is HttpException) {
val code = e.code()
} else if (e is SocketTimeoutException) {
Log.e("time out", "time out")
} else if (e is IOException) {
Log.e("File error", "file error")
} else {
throw RuntimeException(e) // This should appear as error
}
}

这是异常的图像。

enter image description here

编辑

我的扩展函数:

fun <T> Observable<Response<T>>.makeRequest(
context: Context,
executeOnError: ((Any?) -> Unit)? = null, executeOnSuccess: (T?) -> Unit
): Disposable? {

val disposable: Disposable = this
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (it.isSuccessful) {
executeOnSuccess(it?.body())
} else {
val error = getExceptionFromResponse(it, context)
handleError(error, context)

}
}, {
throwErrorIfNotHttp(it)
})

return disposable

}

如何让它在错误选项卡而不是警告选项卡中显示?

最佳答案

尝试添加到您的 else 语句中

Log.e(TAG, "mymessage",e);

完整代码:

@Throws(RuntimeException::class)
fun throwErrorIfNotHttp (e: Throwable) {
if (e is HttpException) {
val code = e.code()
} else if (e is SocketTimeoutException) {
Log.e(TAG, "time out")
} else if (e is IOException) {
Log.e(TAG, "file error")
} else {
Log.e(TAG, "mymessage",e);
throw RuntimeException(e) // This should appear as error
}
}

companion object{
val TAG = "class_tag"
}

关于android - 抛出异常并在 "error"级别的 logcat 中打印其堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56594454/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com