package ru.nbch.indicsearchservice.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.nbch.indicsearchservice.dto.response.InternalExceptionResponseDto;

@RestControllerAdvice
public class ExceptionHandlerAdvice {
    @ExceptionHandler(InternalException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public @ResponseBody InternalExceptionResponseDto handleInternalException(InternalException ex) {
        return ex.getInternalExceptionResponseDto();
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public @ResponseBody InternalExceptionResponseDto handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
        return new InternalExceptionResponseDto("Invalid JSON " + ex);
    }
}
