/*
 * Copyright (C) 2015 Information Management Services, Inc.
 */
package com.imsweb.seerapi.client;

import java.io.IOException;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;

import com.fasterxml.jackson.databind.ObjectMapper;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Interceptor to catch all non-200 responses and convert them to exceptions.
 */
public class ErrorInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());

        if (response.code() != 200) {
            // convert body to error response
            ErrorResponse error = null;
            if (response.body() != null) {
                try {
                    error = new ObjectMapper().readValue(Objects.requireNonNull(response.body()).byteStream(), ErrorResponse.class);
                }
                catch (IOException e) {
                    // sometimes the error message is not right format (like for 404 errors)
                }
            }

            String message = error == null ? "Error code " + response.code() : error.getMessage();

            switch (response.code()) {
                case 401:  // unauthorized
                    throw new NotAuthorizedException(message);
                case 400:  // bad request
                    throw new BadRequestException(message);
                case 404:
                    throw new NotFoundException(message);
                default:
                    throw new SeerApiException(message);
            }
        }

        return response;
    }
}

@@ -127,34 +127,8 @@
 ion;
-%0Aimport java.util.Objects;
 %0A%0Aim
@@ -273,16 +273,45 @@
 esponse;
+%0Aimport okhttp3.ResponseBody;
 %0A%0A/**%0A *
@@ -632,24 +632,65 @@
 ) != 200) %7B%0A
+            ErrorResponse error = null;%0A%0A
             
@@ -731,37 +731,32 @@
             
-Error
 Response
  error = nul
@@ -743,29 +743,43 @@
 Response
- error = null
+Body body = response.body()
 ;%0A      
@@ -788,31 +788,20 @@
     if (
-response.
 body
-()
  != null
@@ -887,47 +887,12 @@
 lue(
-Objects.requireNonNull(response.
 body
-())
 .byt

Fix Spotbugs