Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / ErrorHandler.java
1 package com.moiseum.wolnelektury.connection;
2
3 import java.io.IOException;
4
5 import retrofit2.Response;
6
7 import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
8 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
9 import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
10 import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
11 import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
12
13 /**
14  * @author golonkos.
15  */
16
17 public class ErrorHandler<T> {
18
19         private static final String TAG = ErrorHandler.class.getSimpleName();
20         private final Response<T> response;
21
22         public ErrorHandler(Response<T> response) {
23                 this.response = response;
24         }
25
26         public void handle() throws IOException {
27                 // There is no error model returned for this API
28                 switch (response.code()) {
29                         case HTTP_BAD_REQUEST:
30                         case HTTP_NOT_FOUND:
31                         case HTTP_BAD_METHOD:
32                         case HTTP_INTERNAL_ERROR:
33                         case HTTP_FORBIDDEN:
34                         default:
35                                 throw new IOException("Unknown or unhandled exception for response " + response.code() + ", " + response.message());
36                 }
37
38         }
39
40         //      public ErrorModel parseError(Response<T> response) {
41         //              try {
42         //                      Gson gson = new GsonBuilder().create();
43         //                      return gson.fromJson(response.errorBody().string(), ErrorModel.class);
44         //              } catch (IOException | JsonSyntaxException e) {
45         //                      Log.e(TAG, "Error while parsing error json", e);
46         //                      return null;
47         //              }
48         //      }
49
50         public int getResponseCode() {
51                 return response.code();
52         }
53 }