<commit_before>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));
    }

}<commit_msg>change: clarify error messages in some cases<commit_after>package edu.upc.caminstech.equipstic.client.exception;

import java.io.IOException;
import java.util.Map;
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;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 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() {
        Optional<String> msg = getResponseMessage();

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

    private Optional<String> getResponseMessage() {
        if (!cause.isPresent()) {
            return Optional.ofNullable(null);
        }
        String json = cause.get().getResponseBodyAsString();

        try {
            Map<String, String> map = new ObjectMapper().readValue(json, new TypeReference<Map<String, String>>() {
            });
            return Optional.ofNullable(map.get("message"));
        } catch (IOException e) {
            return Optional.ofNullable(null);
        }
    }

}
{'fraction_non_alphanumeric': 0.10306976744186047, 'fraction_numerical': 0.007069767441860465, 'mean_word_length': 3.3530364372469634, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1570410', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1473, 'n_words': 351}