<commit_before>package org.luxons.sevenwonders.errors;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice
public class ExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    private static final String ERROR_CODE_VALIDATION = "VALIDATION_ERROR";

    private static final String ERROR_MSG_VALIDATION = "Input invalid";

    private final MessageSource messageSource;

    @Autowired
    public ExceptionHandler(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleValidationError(MethodArgumentNotValidException exception) {
        logger.error("Invalid input", exception);
        List<ObjectError> errors = exception.getBindingResult().getAllErrors();
        UIError uiError = new UIError(ERROR_CODE_VALIDATION, ERROR_MSG_VALIDATION, ErrorType.VALIDATION);
        uiError.setValidationErrors(errors);
        return uiError;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleGenericUserError(UserInputException exception) {
        logger.error("Incorrect user input: " + exception.getMessage());
        String messageKey = exception.getMessageResourceKey();
        String message = messageSource.getMessage(messageKey, exception.getParams(), messageKey, Locale.US);
        return new UIError(messageKey, message, ErrorType.USER);
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleApiError(ApiMisuseException exception) {
        logger.error("Invalid API input", exception);
        return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL);
    }

    @MessageExceptionHandler<commit_msg>Add conversion exception handling for bad input JSON format<commit_after>package org.luxons.sevenwonders.errors;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;

@ControllerAdvice
public class ExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);

    private static final String ERROR_CODE_VALIDATION = "VALIDATION_ERROR";

    private static final String ERROR_CODE_CONVERSION = "MESSAGE_FORMAT_ERROR";

    private static final String ERROR_MSG_VALIDATION = "Invalid input data";

    private static final String ERROR_MSG_CONVERSION = "Invalid input format";

    private final MessageSource messageSource;

    @Autowired
    public ExceptionHandler(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleValidationError(MethodArgumentNotValidException exception) {
        logger.error("Invalid input", exception);
        List<ObjectError> errors = exception.getBindingResult().getAllErrors();
        UIError uiError = new UIError(ERROR_CODE_VALIDATION, ERROR_MSG_VALIDATION, ErrorType.VALIDATION);
        uiError.setValidationErrors(errors);
        return uiError;
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleConversionError(MessageConversionException exception) {
        logger.error("Error interpreting the message", exception);
        return new UIError(ERROR_CODE_CONVERSION, ERROR_MSG_CONVERSION, ErrorType.VALIDATION);
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleGenericUserError(UserInputException exception) {
        logger.error("Incorrect user input: " + exception.getMessage());
        String messageKey = exception.getMessageResourceKey();
        String message = messageSource.getMessage(messageKey, exception.getParams(), messageKey, Locale.US);
        return new UIError(messageKey, message, ErrorType.USER);
    }

    @MessageExceptionHandler
    @SendToUser("/queue/errors")
    private UIError handleApiError(ApiMisuseException exception) {
        logger.error("Invalid API input", exception);
        return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL);
    }

    @MessageExceptionHandler
{'fraction_non_alphanumeric': 0.08436911487758945, 'fraction_numerical': 0.0007532956685499058, 'mean_word_length': 5.014722536806342, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1694116', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1313, 'n_words': 276}