package ch.uzh.csg.reimbursement.rest;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import ch.uzh.csg.reimbursement.dto.ErrorDto;
import ch.uzh.csg.reimbursement.model.exception.BusinessException;
import ch.uzh.csg.reimbursement.model.exception.ServiceException;

@ControllerAdvice
public class GlobalControllerExceptionHandler {

	private Logger logger = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

	@ResponseStatus(BAD_REQUEST)
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody ErrorDto handleRuntimeException(HttpServletRequest req, RuntimeException ex) {
		if(!(ex instanceof BusinessException)) {
			logger.error(ex.getMessage(), ex);
			ex = new ServiceException();
		}
		return new ErrorDto(ex);
	}
}

//TODO: GET /api/user/{uid}/signature returns a complete NullPointerException if there is no seignature yet
//TODO: GET /api/user/{uid}/signature returns a complete UserNotFoundException if there is a wrong username
//TODO: GET /api/exchange-rate returns a ServiceException instead of a No or Wrongdate exception

//TODO: Nullpointer could crash onlogoutsucesshandler
@@ -1113,373 +1113,4 @@
 %09%7D%0A%7D
-%0A%0A//TODO: GET /api/user/%7Buid%7D/signature returns a complete NullPointerException if there is no seignature yet%0A//TODO: GET /api/user/%7Buid%7D/signature returns a complete UserNotFoundException if there is a wrong username%0A//TODO: GET /api/exchange-rate returns a ServiceException instead of a No or Wrongdate exception%0A%0A//TODO: Nullpointer could crash onlogoutsucesshandler

Move TODOs to Asana https://app.asana.com/0/31739078849413/34673668940652