1 package com.moiseum.wolnelektury.connection;
3 import java.io.IOException;
5 import retrofit2.Response;
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;
17 public class ErrorHandler<T> {
19 private static final String TAG = ErrorHandler.class.getSimpleName();
20 private final Response<T> response;
22 public ErrorHandler(Response<T> response) {
23 this.response = response;
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:
32 case HTTP_INTERNAL_ERROR:
35 throw new IOException("Unknown or unhandled exception for response " + response.code() + ", " + response.message());
40 // public ErrorModel parseError(Response<T> response) {
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);
50 public int getResponseCode() {
51 return response.code();