package edu.upc.caminstech.equipstic.client.exception;

import java.util.Optional;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClientResponseException;

/**
 * Excepció del Client que exposa la resposta retornada pel servidor.
 * <p>
 * Es tracta d'una {@link RuntimeException} enriquida amb un nou mètode
 * {@link #getResponse()} per accedir a la resposta retornada pel servidor.
 */
public class EquipsTicClientException extends RuntimeException {

    private static final long serialVersionUID = 5559008561162893859L;

    private final Optional<RestClientResponseException> cause;

    public EquipsTicClientException(RestClientResponseException cause) {
        super(cause);
        this.cause = Optional.of(cause);
    }

    public EquipsTicClientException(String message, RestClientResponseException cause) {
        super(message, cause);
        this.cause = Optional.of(cause);
    }

    public EquipsTicClientException(ResponseEntity<?> entity) {
        super();
        Assert.notNull(entity, "Entity can not be null");
        this.cause = null;
    }

    public EquipsTicClientException(ResponseEntity<?> entity, String message) {
        super(message);
        Assert.notNull(entity, "Entity can not be null");
        this.cause = null;
    }

    /**
     * Retorna el codi d'estat HTTP de la resposta del servidor.
     */
    public Optional<HttpStatus> getStatus() {
        return cause.map(c -> HttpStatus.valueOf(c.getRawStatusCode()));
    }

    /**
     * Retorna les capçaleres de la resposta del servidor.
     */
    public Optional<HttpHeaders> getHeaders() {
        return cause.map(RestClientResponseException::getResponseHeaders);
    }

    /**
     * Retorna el cos de la resposta del servidor.
     */
    public Optional<String> getBody() {
        return cause.map(RestClientResponseException::getResponseBodyAsString);
    }

    @Override
    public String getMessage() {
        return String.format("%s: %s - %s", super.getMessage(), getStatus().orElse(null),
                cause.map(RestClientResponseException::getStatusText).orElse(null));
    }

}

@@ -49,16 +49,66 @@
 ption;%0A%0A
+import java.io.IOException;%0Aimport java.util.Map;%0A
 import j
@@ -372,16 +372,123 @@
 ption;%0A%0A
+import com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0A
 /**%0A * E
@@ -2243,24 +2243,78 @@
 Message() %7B%0A
+        Optional%3CString%3E msg = getResponseMessage();%0A%0A
         retu
@@ -2407,16 +2407,27 @@
         
+msg.orElse(
 cause.ma
@@ -2485,17 +2485,525 @@
 e(null))
-;
+);%0A    %7D%0A%0A    private Optional%3CString%3E getResponseMessage() %7B%0A        if (!cause.isPresent()) %7B%0A            return Optional.ofNullable(null);%0A        %7D%0A        String json = cause.get().getResponseBodyAsString();%0A%0A        try %7B%0A            Map%3CString, String%3E map = new ObjectMapper().readValue(json, new TypeReference%3CMap%3CString, String%3E%3E() %7B%0A            %7D);%0A            return Optional.ofNullable(map.get(%22message%22));%0A        %7D catch (IOException e) %7B%0A            return Optional.ofNullable(null);%0A        %7D
 %0A    %7D%0A%0A

clarify error messages in some cases