Browse Source

修复一处异常处理

weizhengliang 3 years ago
parent
commit
ba21d34cd8

+ 24 - 20
common/src/main/code/com/wdkl/ncs/android/lib/utils/ConnectionObserver.kt

@@ -127,28 +127,32 @@ abstract class BaseObserver<T>(private val context: Context) : Observer<T> {
     }
 
     override fun onError(e: Throwable) {
-        if (e is HttpException) {
-            var errorJSon = e.response().errorBody()!!.getJsonString()
-            if (errorJSon.contains("html")){
-                this.onError(ExceptionHandle.handleException(e))
-                return
-            }
-            val obj = JSONObject(errorJSon)
-            if (obj.has("code")){
-                val result = ExceptionHandle.ResponeThrowable(e,420)
-                result.customMessage = obj.getString("message")
-                this.onError(result)
-            }else{
-                this.onError(ExceptionHandle.handleException(e))
-            }
-        } else {
-            this.onError(ExceptionHandle.ResponeThrowable(e, 1000).then {
-                if (e.message == null) {
-                    it.customMessage = "加载失败!"
+        try {
+            if (e is HttpException) {
+                var errorJSon = e.response().errorBody()!!.getJsonString()
+                if (errorJSon.contains("html")) {
+                    this.onError(ExceptionHandle.handleException(e))
+                    return
+                }
+                val obj = JSONObject(errorJSon)
+                if (obj.has("code")) {
+                    val result = ExceptionHandle.ResponeThrowable(e, 420)
+                    result.customMessage = obj.getString("message")
+                    this.onError(result)
                 } else {
-                    it.customMessage = e.message
+                    this.onError(ExceptionHandle.handleException(e))
                 }
-            })
+            } else {
+                this.onError(ExceptionHandle.ResponeThrowable(e, 1000).then {
+                    if (e.message == null) {
+                        it.customMessage = "加载失败!"
+                    } else {
+                        it.customMessage = e.message
+                    }
+                })
+            }
+        } catch (exception: Exception) {
+            this.onError(e)
         }
 
     }