package cn.com.qmth.stmms.api.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;

import cn.com.qmth.stmms.admin.exam.parameter.BaseParameterController;
import cn.com.qmth.stmms.api.utils.AuthInfoUtils;
import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;

public class BaseApiController extends BaseParameterController {

    @ExceptionHandler
    public void exception(HttpServletResponse response, Exception ex) throws IOException {
        response.addHeader(AuthInfoUtils.ERROR_MESSAGE_HEADER_KEY, StringUtils.trimToEmpty(ex.getMessage()));
        if (ex instanceof ApiException) {
            ApiException e = (ApiException) ex;
            response.sendError(e.getCode());
        } else {
            response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }
}
