How to deal with network exceptions in kotlin’s way

1、 Preface @ h_ 404_ 5@

The previous article on several ways rxjava handles business exceptions once introduced that retrofit exceptions can be handled in a variety of ways@ H_ 404_ 5@

Among them, you can use rxjava's error handling operators, which are specially used to handle exceptions@ H_ 404_ 5@

Just two operators:

2、 Use kotlin's properties @ h_ 404_ 5@

This time I try to handle exceptions in combination with the characteristics of the kotlin extension function@ H_ 404_ 5@

The response returned by the network request mostly takes the following form: @ h_ 404_ 5@

For client-side development, we will encapsulate an httpresponse of the base class@ H_ 404_ 5@

Unproguard is an empty interface, which is mainly used to facilitate app to retain some classes in case of confusion@ H_ 404_ 5@

Normally, we will handle exceptions in the observer's onerror as follows: @ h_ 404_ 5@

If we use the error handling operator of rxjava, we can write the following extension function: @ h_ 404_ 5@

Use of extension function errorreturn: @ h_ 404_ 5@

In this way, there is no need to handle exceptions in onerror, and errorreturn is a high-order function. Its action parameter passes a function, which is specially used to handle exceptions. The exception handling of each network request will not be the same. You can use this function to pass different exception handling@ H_ 404_ 5@

Summary @ h_ 404_ 5@

By making rational use of kotlin's extension function, you can write elegant code. The use of higher-order functions can achieve further abstraction.

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>