package io.fabric8.kubernetes.api;

import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.StatusError;

import java.io.InputStream;

import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ExceptionResponseMapper implements ResponseExceptionMapper<Exception> {
    @Override
    public Exception fromResponse(Response response) {
        try {

            ObjectMapper mapper = new ObjectMapper();
            Status error = mapper.readValue((InputStream) response.getEntity(), Status.class);

            return new KubernetesApiException(error.getMessage());
        } catch (Exception ex) {
            return new Exception(
                    "Could not deserialize server side exception: " + ex.getMessage());
        }
    }
}
@@ -40,46 +40,51 @@
 ort 
-io.fabric8.kubernetes.api.model.Status
+com.fasterxml.jackson.databind.ObjectMapper
 ;%0Aim
@@ -130,191 +130,175 @@
 atus
-Error
 ;%0A
-%0A
 import 
-java.io.InputStream;%0A%0Aimport javax.ws.rs.core.Response
+org.apache.cxf.jaxrs.client.ResponseExceptionMapper;%0A%0Aimport javax.ws.rs.WebApplicationException
 ;%0A
-%0A
 import 
-org.apache.cxf.jaxrs.client.ResponseExceptionMapper;%0A%0Aimport com.fasterxml.jackson.databind.ObjectMapper
+javax.ws.rs.core.Response;%0Aimport java.io.InputStream
 ;%0A%0Ap
@@ -464,17 +464,16 @@
   try %7B%0A
-%0A
         
@@ -534,156 +534,562 @@
     
-Status error = mapper.readValue((InputStream) response.getEntity(), Status.class);%0A%0A            return new KubernetesApiException(error.getMessage()
+Object entity = response.getEntity();%0A            String message = %22No message%22;%0A            if (entity instanceof InputStream) %7B%0A                Status error = mapper.readValue((InputStream) entity, Status.class);%0A                if (error != null) %7B%0A                    message = error.getMessage();%0A                %7D%0A            %7D else if (entity != null) %7B%0A                message = entity.toString();%0A            %7D%0A            message = %22HTTP %22 + response.getStatus() + %22 %22 + message;%0A            return new WebApplicationException(message, response
 );%0A 

return nicer exception messages when kube REST calls barf