Kaynağa Gözat

添加全局关闭错误日志打印[NetConfig.logEnabled]

drake 4 yıl önce
ebeveyn
işleme
6d92b3769c

+ 13 - 6
net/src/main/java/com/drake/net/NetConfig.kt

@@ -50,8 +50,9 @@ import java.util.concurrent.ExecutionException
  */
 object NetConfig {
 
-    lateinit var host: String
     lateinit var app: Application
+    var host: String = ""
+    var logEnabled = true
 
     var onDialog: DialogCoroutineScope.(FragmentActivity) -> Dialog = {
         val progress = ProgressDialog(activity)
@@ -59,7 +60,9 @@ object NetConfig {
         progress
     }
 
-    var onError: Throwable.() -> Unit = {
+    var onError: Throwable.() -> Unit = onError@{
+        if (!this@NetConfig::app.isInitialized) return@onError
+
         val message = when (this) {
             is NetworkError -> app.getString(R.string.net_network_error)
             is URLError -> app.getString(R.string.net_url_error)
@@ -80,7 +83,8 @@ object NetConfig {
             else -> app.getString(R.string.net_other_error)
         }
 
-        printStackTrace()
+        if (logEnabled)
+            printStackTrace()
         app.toast(message)
     }
 
@@ -90,7 +94,10 @@ object NetConfig {
             is RequestParamsException,
             is ResponseException,
             is NullPointerException -> onError(this)
-            else -> printStackTrace()
+            else -> {
+                if (logEnabled)
+                    printStackTrace()
+            }
         }
     }
 }
@@ -107,7 +114,7 @@ object NetConfig {
  */
 fun Application.initNet(host: String, config: KalleConfig.Builder.() -> Unit = {}) {
     NetConfig.host = host
-    NetConfig.app = this
+    app = this
     val builder = KalleConfig.newBuilder()
     builder.apply {
         network(BroadcastNetwork(this@initNet))
@@ -151,7 +158,7 @@ fun KalleConfig.Builder.onDialog(block: (DialogCoroutineScope.(context: Fragment
  * @param password 缓存数据库密码
  */
 fun KalleConfig.Builder.cacheEnabled(
-    path: String = NetConfig.app.cacheDir.absolutePath,
+    path: String = app.cacheDir.absolutePath,
     password: String = "cache"
 ) {
     val cacheStore = DiskCacheStore.newBuilder(path)

+ 3 - 1
net/src/main/java/com/drake/net/scope/AndroidScope.kt

@@ -19,6 +19,7 @@ package com.drake.net.scope
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleEventObserver
 import androidx.lifecycle.LifecycleOwner
+import com.drake.net.NetConfig
 import kotlinx.coroutines.*
 import kotlin.coroutines.CoroutineContext
 import kotlin.coroutines.EmptyCoroutineContext
@@ -94,7 +95,8 @@ open class AndroidScope(
      * 错误处理
      */
     open fun handleError(e: Throwable) {
-        e.printStackTrace()
+        if (NetConfig.logEnabled)
+            e.printStackTrace()
     }
 
     open fun cancel(cause: CancellationException? = null) {

+ 9 - 2
net/src/main/java/com/drake/net/utils/Fastest.kt

@@ -16,6 +16,7 @@
 
 package com.drake.net.utils
 
+import com.drake.net.NetConfig
 import com.drake.net.transform.DeferredTransform
 import com.yanzhenjie.kalle.NetCancel
 import kotlinx.coroutines.*
@@ -54,7 +55,10 @@ suspend fun <T> CoroutineScope.fastest(
                 it.cancel()
                 val allFail = listDeferred.all { it.isCancelled }
                 if (allFail) deferred.completeExceptionally(e) else {
-                    if (e !is CancellationException) e.printStackTrace()
+                    if (e !is CancellationException) {
+                        if (NetConfig.logEnabled)
+                            e.printStackTrace()
+                    }
                 }
             }
         }
@@ -99,7 +103,10 @@ suspend fun <T, R> CoroutineScope.fastest(
                 it.deferred.cancel()
                 val allFail = listDeferred.all { it.deferred.isCancelled }
                 if (allFail) deferred.completeExceptionally(e) else {
-                    if (e !is CancellationException) e.printStackTrace()
+                    if (e !is CancellationException) {
+                        if (NetConfig.logEnabled)
+                            e.printStackTrace()
+                    }
                 }
             }
         }

+ 2 - 1
temp.md

@@ -266,7 +266,8 @@ internal var onError: Throwable.() -> Unit = {
     is ServerResponseException -> app.getString(R.string.server_error)
     is ResponseException -> msg
     else -> {
-      printStackTrace()
+       if (NetConfig.logEnabled)
+           printStackTrace()
       app.getString(R.string.other_error)
     }
   }