package de.avatic.taric.error;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.swing.text.html.HTML;

@ControllerAdvice
public class ErrorController {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler
    public  ResponseEntity<ErrorDTO> handleException(ArgumentException ex) {
        var dto = new ErrorDTO();
        dto.setField(ex.getArg());
        dto.setMessage("Invalid argument");
        return new ResponseEntity<>(dto, HttpStatus.BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public  ResponseEntity<ErrorDTO> handleException(InternalError ex) {
        var dto = new ErrorDTO();
        dto.setField(ex.getArg());
        dto.setMessage("Internal error");
        return new ResponseEntity<>(dto, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
