package com.style24.front.support.controller;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gagaframework.web.rest.server.GagaResponseStatus;
import com.style24.front.support.util.ViewUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * Error Controller
 * 		Disabling the Whitelabel Error Page
 * 		- application.yml 파일에 다음을 추가
 * 			server.error.path: /error
 * 			server.error.whitelabel.enabled: false
 * 
 * @author gagamel
 * @since 2020. 9. 11
 */
@Controller
@Slf4j
public class TsfErrorController implements ErrorController {

	/**
	 * 이 메서드는 스프링 부트 2.3.x부터 deprecated됨.
	 * 이 메서드 대신 custom path를 지정하려면 server.error.path 속성으로 지정해야 한다.
	 */
	@Override
	public String getErrorPath() {
		return null;
	}

	/**
	 * Error
	 */
	@RequestMapping("/error")
	public String error(HttpServletRequest request) throws HttpRequestMethodNotSupportedException {
		Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

		if (status != null) {
			Integer statusCode = Integer.valueOf(status.toString());
			log.error("Error StatusCode: {}", statusCode);

			if (statusCode == GagaResponseStatus.NOT_FOUND.getCode()) {
				return ViewUtils.getDeviceViewName("error/404");
			}
		}

		;

		log.error("Error Message: {}", request.getAttribute(RequestDispatcher.ERROR_MESSAGE));

		return ViewUtils.getDeviceViewName("error/500");
	}

}
