package com.jero.common.exception;
import com.google.gson.Gson;
import com.jero.common.api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Administrator
*/
@Slf4j
@Component
public class LoginException {
private static HttpServletResponse response;
@Autowired
public void setResponse(HttpServletResponse response){
this.response = response;
}
public static void loginException(Integer errorCode,String errorMessage){
String reponseJson = new Gson().toJson(Result.error(errorCode,errorMessage));
response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("utf-8");
OutputStream out = null;
try {
out = response.getOutputStream();
out.write(reponseJson.getBytes());
}catch (IOException ex){
ex.printStackTrace();
}finally {
if(out != null){
try {
out.close();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
log.error(errorMessage);
}
}