What is the correct way for ViewModel and Repository to handle a VolleyError?

I have an Android app on Java that use the Volley library. I’m taking over the work of a former intern who make a RequestUtils class to create his own requests. I need to upgrade his work in order to make a more reliable app and I read a lot of documentation to know the best practices.

I need to get the user informations from my server to display it in my Activity, and if there is an error make a Toast. I use ViewModel to avoid HTTP request at each rotation change or whatever, and currently it’s the best for me if there is a request in the case of the Activity being entirely recreate because of the phone killing it in the background.

I’m at the point that I have a UserViewModel class and UserRepository class, and in this Repository class I want to use the request that he made for that, except that I don’t know the best way (I have 3 different solutions at the moment) to pass the VolleyError to the ViewModel if there is any.

I also want to remind that is not a best practice to have a LiveData in a Repository class.

There is some code that I have.

RequestUtils.java getUser :

public static void getUserInfos(Response.Listener<User> responseListener, Response.ErrorListener errorListener) {
        GsonRequestByClass<User> gsonRequest = new GsonRequestByClass<>(Request.Method.GET, STRING_URL + "users/" + ID, User.class, responseListener, errorListener);
        HTTPRequest.getInstance().addToQueue(gsonRequest);
    }

The method is currently called in the onCreate method of my MainActivity.java, I want to use it in my UserRepository:

import static com.example.app.utils.RequestsUtils.getUser;

public class MainActivity extends AppCompatActivity {
    //...
    User user;
    
    //...
    protected void onCreate(Bundle savedInstanceState) {

        //BEFORE
        getUser(new Response.Listener<User>() {
                    @Override
                    public void onResponse(User response) {
                        user = response;
                        //do smth
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //There is a Toast here
                    }
                });

        //NOW
        UserViewModel userModel  = new ViewModelProvider(this).get(UserViewModel.class);
        userModel.getUser1().observe(this, userResp->{
            user = userResp;
            //do smth
        }); //how to handle error ?
    }
}

UserViewModel.java

public class UserViewModel extends ViewModel {
    private UserRepository userRepository;

    private MutableLiveData<User> userLiveData;

    public UserViewModel(){
        userRepository = new UserRepository();
    }

//solution 1
    public LiveData<User> getUser1() {
        if (userLiveData == null){
            if (!userRepository.retrieveUserInfo1()){
                userLiveData = new MutableLiveData<>();
                userLiveData.setValue(userRepository.getUser());
            } else {
                //how indicate that there is an error for the Toast in activity ?
            }
        }
        return userLiveData;
    }

//solution 2
     public LiveData<User> getUser2() {
        if (userLiveData == null){
            Response<User> reponse = userRepository.retrieveUserInfo2();
            if (reponse.isSuccess()){
                userLiveData.setValue(reponse.result);
            } else {
                //make smth with response.error
            }
        }
        return userLiveData;
    }

//solution 3
    public LiveData<User> getUser3() {
        if (userLiveData == null){
            try {
                userLiveData = userRepository.retrieveUserInfo3();
            } catch (VolleyError e) {
                throw new RuntimeException(e);
            }
        }
        return userLiveData;
    }

}

UserRepository.java

public class UserRepository {
    private User user;
    private boolean errorReq;
    Response<User> responseReq;

    public User getUser(){
        return user;
    }


//solution 1
    public boolean retrieveUserInfo1() {
            getUserInfos(new Response.Listener<User>() {
                @Override
                public void onResponse(User response) {
                    user = response;
                    errorReq = false;
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    errorReq = true;
                }
            });
        return errorReq;
    }


//solution 2
    public Response<User> retrieveUserInfo2() {
                getUserInfos(new Response.Listener<User>() {
                    @Override
                    public void onResponse(User response) {
                        responseReq = Response.success(response, null);
                        user = response;
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        responseReq = Response.error(error);    
                    }
                });
            return responseReq;
        }

//solution 3
     public Response<User> retrieveUserInfo3() throws VolleyError {
                getUserInfos(new Response.Listener<User>() {
                    @Override
                    public void onResponse(User response) {
                        errorReq = false;
                        user = response;
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        errorReq = true;
                    }
                });
            if (errorReq){
                throw new VolleyError();
            }
            return user;
        }

}

If anyone can help me ! This is my first question on SO, I hope I write it correctly and explain it well, tell me otherwise.

New contributor

Ninny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật