Browse Source

网络请求异常位置追踪

drake 2 years ago
parent
commit
e405a643bb

+ 17 - 16
net/src/main/java/com/drake/net/NetCoroutine.kt

@@ -2,6 +2,7 @@
 
 package com.drake.net
 
+import com.drake.net.interfaces.NetDeferred
 import com.drake.net.request.BodyRequest
 import com.drake.net.request.Method
 import com.drake.net.request.UrlRequest
@@ -22,7 +23,7 @@ inline fun <reified M> CoroutineScope.Get(
     path: String,
     tag: Any? = null,
     noinline block: (UrlRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     UrlRequest().apply {
         setPath(path)
@@ -31,7 +32,7 @@ inline fun <reified M> CoroutineScope.Get(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -44,7 +45,7 @@ inline fun <reified M> CoroutineScope.Post(
     path: String,
     tag: Any? = null,
     noinline block: (BodyRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     BodyRequest().apply {
         setPath(path)
@@ -53,7 +54,7 @@ inline fun <reified M> CoroutineScope.Post(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -66,7 +67,7 @@ inline fun <reified M> CoroutineScope.Head(
     path: String,
     tag: Any? = null,
     noinline block: (UrlRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     UrlRequest().apply {
         setPath(path)
@@ -75,7 +76,7 @@ inline fun <reified M> CoroutineScope.Head(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -88,7 +89,7 @@ inline fun <reified M> CoroutineScope.Options(
     path: String,
     tag: Any? = null,
     noinline block: (UrlRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     UrlRequest().apply {
         setPath(path)
@@ -97,7 +98,7 @@ inline fun <reified M> CoroutineScope.Options(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -110,7 +111,7 @@ inline fun <reified M> CoroutineScope.Trace(
     path: String,
     tag: Any? = null,
     noinline block: (UrlRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     UrlRequest().apply {
         setPath(path)
@@ -119,7 +120,7 @@ inline fun <reified M> CoroutineScope.Trace(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -132,7 +133,7 @@ inline fun <reified M> CoroutineScope.Delete(
     path: String,
     tag: Any? = null,
     noinline block: (BodyRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     BodyRequest().apply {
         setPath(path)
@@ -141,7 +142,7 @@ inline fun <reified M> CoroutineScope.Delete(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -154,7 +155,7 @@ inline fun <reified M> CoroutineScope.Put(
     path: String,
     tag: Any? = null,
     noinline block: (BodyRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     BodyRequest().apply {
         setPath(path)
@@ -163,7 +164,7 @@ inline fun <reified M> CoroutineScope.Put(
         tag(tag)
         block?.invoke(this)
     }.execute()
-}
+})
 
 /**
  * 异步网络请求
@@ -176,7 +177,7 @@ inline fun <reified M> CoroutineScope.Patch(
     path: String,
     tag: Any? = null,
     noinline block: (BodyRequest.() -> Unit)? = null
-): Deferred<M> = async(Dispatchers.IO + SupervisorJob()) {
+): Deferred<M> = NetDeferred(async(Dispatchers.IO + SupervisorJob()) {
     if (!isActive) throw CancellationException()
     BodyRequest().apply {
         setPath(path)
@@ -186,5 +187,5 @@ inline fun <reified M> CoroutineScope.Patch(
         block?.invoke(this)
     }.execute()
 }
-
+)
 // </editor-fold>

+ 4 - 6
net/src/main/java/com/drake/net/exception/NetException.kt

@@ -30,14 +30,12 @@ import java.io.IOException
 open class NetException(
     open val request: Request,
     message: String? = null,
-    cause: Throwable? = null
+    cause: Throwable? = null,
 ) : IOException(message, cause) {
 
+    var occurred: String = ""
+
     override fun getLocalizedMessage(): String? {
-        return if (message != null) {
-            "$message (${request.url})"
-        } else {
-            "(${request.url})"
-        }
+        return "${if (message == null) "" else message + " "}${request.url}$occurred"
     }
 }

+ 9 - 2
net/src/main/java/com/drake/net/exception/URLParseException.kt

@@ -8,5 +8,12 @@ package com.drake.net.exception
  */
 open class URLParseException(
     message: String? = null,
-    cause: Throwable? = null
-) : Exception(message, cause)
+    cause: Throwable? = null,
+) : Exception(message, cause) {
+
+    var occurred: String = ""
+
+    override fun getLocalizedMessage(): String? {
+        return super.getLocalizedMessage() + occurred
+    }
+}

+ 23 - 0
net/src/main/java/com/drake/net/interfaces/NetDeferred.kt

@@ -0,0 +1,23 @@
+package com.drake.net.interfaces
+
+import com.drake.net.exception.NetException
+import com.drake.net.exception.URLParseException
+import kotlinx.coroutines.Deferred
+
+@PublishedApi
+internal class NetDeferred<M>(private val deferred: Deferred<M>) : Deferred<M> by deferred {
+
+    override suspend fun await(): M {
+        // 追踪到网络请求异常发生位置
+        val occurred = Throwable().stackTrace.getOrNull(1)?.run { " At (${fileName}:${lineNumber})" }
+        return try {
+            deferred.await()
+        } catch (e: Exception) {
+            when {
+                occurred != null && e is NetException -> e.occurred = occurred
+                occurred != null && e is URLParseException -> e.occurred = occurred
+            }
+            throw  e
+        }
+    }
+}