Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Retrofitting Control Flow

Retrofitting Control Flow

How does error handling influence the larger design of app architecture? This talk will attempt to answer this, using the speaker’s experience upgrading the Electric Objects Android applications from Retrofit v1.9 to the v2.0-beta2.

We will cover the deeper architectural ramifications of error strategies, particularly focusing on the question ‘what is the impact of exceptions on control flow?’ using examples from v1 & v2 of Square’s Retrofit library and the supporting code that Electric Objects uses to wrap these network calls.

Presented at Square Engineering's Android Spring Cleaning event on March 10th. https://www.showclix.com/event/square-new-york-presents

67867d2677e7dd983000441ff0b1c089?s=128

NeiL saitug

March 10, 2016
Tweet

More Decks by NeiL saitug

Other Decks in Programming

Transcript

  1. Retrofitting Control Flow

  2. Retrofit

  3. Control Flow

  4. Library Maintainer Decisions' Trickle-Down Impact

  5. Background

  6. Background

  7. Handling Errors v1

  8. try { request.makeRequest(); // if you reach this, request succeeded!

    return request; } catch (RetrofitError error) { Response errorResponse = error.getResponse(); switch (error.getKind()) { case NETWORK: case HTTP: case CONVERSION: case UNEXPECTED: // deal with error }
  9. • NETWORK • HTTP • CONVERSION • UNEXPECTED

  10. void Boolean onMakeRequest() { List<Shoes> shoes = // call to

    get shoes return shoes.isEmpty(); }
  11. void List<Shoe> onMakeRequest() { List<Shoe> myFavoriteShoes = // call to

    get my shoes List<Shoe> friendsFavoriteShoes = // call to get friend's shoes return myFavoriteShoes.addAll(friendsFavoriteShoes); }
  12. None
  13. Handling Errors v2

  14. try { response = request.makeRequest(); if (response.isSuccess()) { return request;

    } else { // server error? } } catch (IOException e) { // network error } catch (Exception ex) { // unexpected/parse error throw ex; }
  15. V1 Success != V2 Success

  16. v1 Success: • connected to the server • received a

    successful response • parsed the response successfully • here's your object.
  17. v2 Success: • connected to the server • received a

    response.
  18. void List<Shoe> onMakeRequest() { Response shoesResponse = // call to

    get my shoes if (!shoesResponse.isSuccess()) return error!!; Response friendsResponse = // call to get friend's shoes if (!friendResponse.isSuccess()) return error!!; // otherwise proceed. }
  19. Try/Catch Blocks

  20. 1 method call -> 1 object returned

  21. 1 method call -> 1 object type || ∞ errors

  22. Retrofit v2: but why?

  23. \\ v1 @GET("/my/shoes") List<Shoe> getShoes(); \\ v2 @GET("/my/shoes") Call<List<Shoe>> getShoes();

  24. Retrofit!! The Best API Glue On The Market!

  25. Thanks! @niftynei