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

import java.io.IOException;

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 {

    @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(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;
    }
}

@@ -123,16 +123,85 @@
 ception;
+%0Aimport java.util.Objects;%0A%0Aimport org.jetbrains.annotations.NotNull;
 %0A%0Aimport
@@ -443,16 +443,29 @@
 ptor %7B%0A%0A
+    @NotNull%0A
     @Ove
@@ -841,16 +841,39 @@
 adValue(
+Objects.requireNonNull(
 response
@@ -879,16 +879,17 @@
 e.body()
+)
 .byteStr

Fix Spotbugs