引发异常并在“错误”级别在logcat中打印其堆栈跟踪

时间:2019-06-14 08:37:11

标签: android exception kotlin logcat

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

这是我的代码:

@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

}

如何使其显示在“错误”标签而不是“警告”标签中?

1 个答案:

答案 0 :(得分:1)

尝试添加到您的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"
}