Browse Source

添加全局默认错误处理针对NetException

drake 4 years ago
parent
commit
761f2800c5

+ 2 - 0
docs/global-error-handle.md

@@ -67,6 +67,7 @@ catch里面的`it`属于异常对象, 这里列举可能存在的异常
 | WriteException | 写入异常 |
 | ReadException | 读取异常 |
 | ResponseException | 响应异常, 这里属于后端返回的错误码和其定义的成功码不匹配 |
+| NetException | 网络异常, 未知的网络异常, 一般情况应当继承他来创建自定义的网络异常 |
 
 假设你重写`DefaultConvert`可以改变异常发生的条件, 当然你在转换器或者拦截器中抛出任何异常都会被捕获或者全局处理, 这里你可以自定义你的异常
 
@@ -102,5 +103,6 @@ catch里面的`it`属于异常对象, 这里列举可能存在的异常
 <string name="net_server_error">服务响应错误</string>
 <string name="net_image_error">图片下载错误</string>
 <string name="net_null_error">数据为空</string>
+<string name="net_error">未知网络错误</string>
 <string name="net_other_error">未知错误</string>
 ```

+ 3 - 4
net/src/main/java/com/drake/net/NetConfig.kt

@@ -82,6 +82,7 @@ object NetConfig {
             is WriteException -> app.getString(R.string.net_write_exception)
             is ReadException -> app.getString(R.string.net_read_exception)
             is ResponseException -> msg
+            is NetException -> app.getString(R.string.net_error)
             else -> app.getString(R.string.net_other_error)
         }
 
@@ -99,7 +100,6 @@ object NetConfig {
     }
 }
 
-
 /**
  * 初始化框架
  * @param host 请求url的主机名
@@ -123,7 +123,7 @@ fun Application.initNet(host: String, config: KalleConfig.Builder.() -> Unit = {
 
 
 /**
- * 该函数指定某些Observer的onError中的默认错误信息处理
+ * 设置全局错误处理
  *
  * @see NetConfig.onError
  */
@@ -132,7 +132,7 @@ fun KalleConfig.Builder.onError(block: Throwable.() -> Unit) {
 }
 
 /**
- * 该函数指定某些Observer的onError中的默认错误信息处理
+ * 设置缺省页的全局错误处理
  *
  * 如果不设置默认只有 解析数据错误 | 后台自定义错误 会显示吐司
  * @see NetConfig.onStateError
@@ -141,7 +141,6 @@ fun KalleConfig.Builder.onStateError(block: Throwable.(view: View) -> Unit) {
     NetConfig.onStateError = block
 }
 
-
 /**
  * 设置使用DialogObserver默认弹出的加载对话框
  * 默认使用系统自带的ProgressDialog

+ 1 - 0
net/src/main/res/values/strings.xml

@@ -33,6 +33,7 @@
     <string name="net_server_error">服务响应错误</string>
     <string name="net_image_error">图片下载错误</string>
     <string name="net_null_error">数据为空</string>
+    <string name="net_error">未知网络错误</string>
     <string name="net_other_error">未知错误</string>
 
     <!--对话框-->