ajax setup过滤机制,获取后台的运行时异常(RuntimeException)做提示信息
2017-04-17 15:27
169 查看
//运行是异常
ajaxSetup 是ajax的一个拦截器,可以拦截表单的提交,也可以拦截后台的运行时异常,做一些提示弹框
ajaxSetup只会补做运行时异常
利用ajax的setup控制将系统的错误信息弹出到页面
timeout.js
$(function() {
// 解决ajax超时问题 by wanghao 2013-10-31 begin
var isChecked = false;
var isCheckedValue = null;
var isTimeout = false;
//这个可以单独的放到另外一个js
var showExceptionInfo = function(msg){
showWindow("exceptionWindow","错误提示");
$("#exceptionWindow .ball_layer_pointout06").text("系统服务异常,异常信息:"+msg);
};
$.ajaxSetup({
"cache" : false,
"complete" : function(XMLHttpRequest, textStatus) {
// 在每个ajax请求后进行删除权限验证
if (XMLHttpRequest.responseText == "IS_CAN_DELETE_TRUE" || XMLHttpRequest.responseText == "IS_CAN_DELETE_FALSE") {
// 如果本身已经获取到删除权限,不再继续验证,从而避免死循环
return;
}
},
"error" : function(textStatus, errorThrown) {
if (textStatus.status == 200) {
return;
}
unDisplayProgress();
// 全局处理异常,所有的业务代码都不再需要手动处理error!!!by wanghao 2013-12-3
var text = textStatus.responseText;
var start = text.indexOf("<h1>");
var end = text.indexOf("</h1>");
var error = text.substring(start + 4, end);
if (error == "TIME_OUT" && !isTimeout) {
isTimeout = true;
alert("操作超时,为保证系统安全,请重新登录,谢谢!");
window.location = basePath;
return;
} else if (!error) {
// console.log("后台异常,但是无异常信息,不影响系统功能!"+text);
} else {
showExceptionInfo(error);
}
}
});
var renderDeleteButton = function(isCanDelete) {
// console.log(new Date() + "-->" +isCanDelete);
if (isCanDelete == "IS_CAN_DELETE_TRUE") {
$("link[title='extra']").attr("disabled", "disabled");
$(".del_report_btn").css("display", "inline-block");
$(".table_delete_btn").css("display", "inline-block");
$(".del_btn").css("display", "inline-block");
$(".table_delete_dis_btn").css("display", "inline-block");
} else if (isCanDelete == "IS_CAN_DELETE_FALSE") {
$("link[title='extra']").removeAttr("disabled");
$(".del_report_btn").remove();
$(".table_delete_btn").remove();
$(".del_btn").remove();
$(".table_delete_dis_btn").remove();
}
};
// 立即执行删除权限验证
//checkIsCanDelete();
});
jsp:
这个错误消息框可以在simesh中配置,装饰到每一个页面
<div id="exceptionWindow" class="ball_box ball_box06" style="display:none;z-index:99999999;width:290px;">
<div class="bg_boder">
<h2>
<a href="#" class="del"></a>错误提示
</h2>
<div class="ball_layer_pointout06"></div>
<div class="ball_btn_box">
<center>
<a href="javascript:void(0)" class="ball_btn_true" >确定</a>
</center>
</div>
</div>
</div>
//后台处理时抛出运行是异常即可
@Service("overviewService")
public class OverviewServiceImpl implements IOverviewService {
private Logger logger = Logger.getLogger(OverviewServiceImpl.class);
@Override
public SummeryOfResourcesVO getSummeryOfResource() {
logger.info("查询一组资源使用情况(云主机、虚拟内核、虚拟内存、安全组)信息");
final SummeryOfResourcesVO vo = new SummeryOfResourcesVO();
final JSONObject[] provider = new JSONObject[1];
final String[] reslt= new String[1];
/**取资源使用情况*/
HttpClientRequest request = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/resource_used");
try {
request.process(new HttpClientResponseHandler() {
@Override
public
4000
void handle(String response, String retCode, String msg) throws HttpClientException,ManoException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
JSONObject obj = JSONObject.parseObject(response);
if(obj.get("result")!=null){
System.out.println(obj.get("result").toString());
JSONObject jsonObj = JSONObject.parseObject(obj.get("result").toString());
if (jsonObj == null){
//&&!reslt[0].equals("0")
return;
}
// 一 、请求并计算 云主机信息
Integer vms = jsonObj.getInteger(Constants.OverView.VMS);
Integer used_vms = jsonObj.getInteger(Constants.OverView.USED_VMS);
Integer unused_vms = 0;
if (vms == null || vms.equals("")) {
vms = 0;
} else if (used_vms == null || used_vms.equals("")) {
used_vms = 0;
} else {
unused_vms = vms - used_vms;
}
vo.setCloudPlatformAll(String.valueOf(vms));
vo.setCloudPlatformUsed(String.valueOf(used_vms));
vo.setCloudPlatformUnUsed(String.valueOf(unused_vms));
// 二 、请求 虚拟内核
Integer cpus = jsonObj.getInteger(Constants.OverView.CPUS);
Integer used_cpus = jsonObj.getInteger(Constants.OverView.USED_CPUS);
Integer unused_cpus = 0;
if (cpus == null || cpus.equals("")) {
cpus = 0;
} else if (used_cpus == null || used_cpus.equals("")) {
used_cpus = 0;
} else {
unused_cpus = cpus - used_cpus;
}
vo.setVirtualKernelAll(String.valueOf(cpus));
vo.setVirtualKernelUsed(String.valueOf(used_cpus));
vo.setVirtualKernelUnUsed(String.valueOf(unused_cpus));
// 三、请求 虚拟内存
Integer mems = jsonObj.getInteger(Constants.OverView.MEMS);
Integer used_mem = jsonObj.getInteger(Constants.OverView.USED_MEM);
Integer unused_mem = 0;
if (mems == null || mems.equals("")) {
mems = 0;
} else if (used_mem == null || used_mem.equals("")) {
used_mem = 0;
} else {
unused_mem = mems - used_mem;
}
vo.setVirtualMemoryAll(String.valueOf(mems));
vo.setVirtualMemoryUsed(String.valueOf(used_mem));
vo.setVirtualMemoryUnUsed(String.valueOf(unused_mem));
// 四、请求 安全组
Integer security_group = jsonObj.getInteger(Constants.OverView.SECURITY_GROUP);
Integer used_security_group = jsonObj.getInteger(Constants.OverView.USED_SECURITY_GROUP);
Integer unused_security_group = 0;
if (security_group == null || security_group.equals("")) {
security_group = 0;
} else if (used_security_group == null || used_security_group.equals("")) {
used_security_group = 0;
} else {
unused_security_group = security_group - used_security_group;
}
vo.setSecurityGroupAll(String.valueOf(security_group));
vo.setSecurityGroupUsed(String.valueOf(used_security_group));
vo.setSecurityGroupUnUsed(String.valueOf(unused_security_group));
}else{
/**provider连接数*/
HttpClientRequest requestProvider = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") +
"/rest/provider_network_cn");
try {
requestProvider.process(new HttpClientResponseHandler() {
@Override
public void handle(String response, String retCode, String msg) throws HttpClientException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
provider[0] = JSONObject.parseObject(response);
System.out.println(provider[0].get("result").toString()+"rrrrrrrrrrrrr");
String jsonObj = provider[0].get("result").toString();
reslt[0] =jsonObj;
if (jsonObj == null)
return;
}
});
} catch (HttpClientException e) {
e.printStackTrace();
}
//////////////////ManoException是运行是异常
if(reslt[0].equals("0")){
throw new ManoException("0","没建立与openstack的连接,请检查openstack资源中openstack提供者是否已添加");
}else{
throw new ManoException("1","没有数据");
}
//throw new HttpClientException("没建立连接");
}
}});
} catch (HttpClientException e) {
throw new ManoException("1","没建立连接");
}
logger.info("SummeryOfResourcesVO is " + vo.toString());
return vo;
}
}
//运行时异常
package com.certusnet.nfv.mano.exception;
import java.util.Locale;
import javax.ws.rs.ext.Provider;
import org.springframework.web.context.ContextLoaderListener;
/**
* ManoException,统一处理异常编码和异常信息
* @author Biwei
*
*/
@Provider
public class ManoException extends RuntimeException {
public static final long serialVersionUID = 11111111111L;
public ManoException(Throwable throwable) {
super(throwable);
}
public ManoException(String code, String msg) {
super(msg);
this.code = code;
}
/**
* message key
*/
private String code;
/**
* message params
*/
private Object[] params;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
}
ajaxSetup 是ajax的一个拦截器,可以拦截表单的提交,也可以拦截后台的运行时异常,做一些提示弹框
ajaxSetup只会补做运行时异常
利用ajax的setup控制将系统的错误信息弹出到页面
timeout.js
$(function() {
// 解决ajax超时问题 by wanghao 2013-10-31 begin
var isChecked = false;
var isCheckedValue = null;
var isTimeout = false;
//这个可以单独的放到另外一个js
var showExceptionInfo = function(msg){
showWindow("exceptionWindow","错误提示");
$("#exceptionWindow .ball_layer_pointout06").text("系统服务异常,异常信息:"+msg);
};
$.ajaxSetup({
"cache" : false,
"complete" : function(XMLHttpRequest, textStatus) {
// 在每个ajax请求后进行删除权限验证
if (XMLHttpRequest.responseText == "IS_CAN_DELETE_TRUE" || XMLHttpRequest.responseText == "IS_CAN_DELETE_FALSE") {
// 如果本身已经获取到删除权限,不再继续验证,从而避免死循环
return;
}
},
"error" : function(textStatus, errorThrown) {
if (textStatus.status == 200) {
return;
}
unDisplayProgress();
// 全局处理异常,所有的业务代码都不再需要手动处理error!!!by wanghao 2013-12-3
var text = textStatus.responseText;
var start = text.indexOf("<h1>");
var end = text.indexOf("</h1>");
var error = text.substring(start + 4, end);
if (error == "TIME_OUT" && !isTimeout) {
isTimeout = true;
alert("操作超时,为保证系统安全,请重新登录,谢谢!");
window.location = basePath;
return;
} else if (!error) {
// console.log("后台异常,但是无异常信息,不影响系统功能!"+text);
} else {
showExceptionInfo(error);
}
}
});
var renderDeleteButton = function(isCanDelete) {
// console.log(new Date() + "-->" +isCanDelete);
if (isCanDelete == "IS_CAN_DELETE_TRUE") {
$("link[title='extra']").attr("disabled", "disabled");
$(".del_report_btn").css("display", "inline-block");
$(".table_delete_btn").css("display", "inline-block");
$(".del_btn").css("display", "inline-block");
$(".table_delete_dis_btn").css("display", "inline-block");
} else if (isCanDelete == "IS_CAN_DELETE_FALSE") {
$("link[title='extra']").removeAttr("disabled");
$(".del_report_btn").remove();
$(".table_delete_btn").remove();
$(".del_btn").remove();
$(".table_delete_dis_btn").remove();
}
};
// 立即执行删除权限验证
//checkIsCanDelete();
});
jsp:
这个错误消息框可以在simesh中配置,装饰到每一个页面
<div id="exceptionWindow" class="ball_box ball_box06" style="display:none;z-index:99999999;width:290px;">
<div class="bg_boder">
<h2>
<a href="#" class="del"></a>错误提示
</h2>
<div class="ball_layer_pointout06"></div>
<div class="ball_btn_box">
<center>
<a href="javascript:void(0)" class="ball_btn_true" >确定</a>
</center>
</div>
</div>
</div>
//后台处理时抛出运行是异常即可
@Service("overviewService")
public class OverviewServiceImpl implements IOverviewService {
private Logger logger = Logger.getLogger(OverviewServiceImpl.class);
@Override
public SummeryOfResourcesVO getSummeryOfResource() {
logger.info("查询一组资源使用情况(云主机、虚拟内核、虚拟内存、安全组)信息");
final SummeryOfResourcesVO vo = new SummeryOfResourcesVO();
final JSONObject[] provider = new JSONObject[1];
final String[] reslt= new String[1];
/**取资源使用情况*/
HttpClientRequest request = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/resource_used");
try {
request.process(new HttpClientResponseHandler() {
@Override
public
4000
void handle(String response, String retCode, String msg) throws HttpClientException,ManoException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
JSONObject obj = JSONObject.parseObject(response);
if(obj.get("result")!=null){
System.out.println(obj.get("result").toString());
JSONObject jsonObj = JSONObject.parseObject(obj.get("result").toString());
if (jsonObj == null){
//&&!reslt[0].equals("0")
return;
}
// 一 、请求并计算 云主机信息
Integer vms = jsonObj.getInteger(Constants.OverView.VMS);
Integer used_vms = jsonObj.getInteger(Constants.OverView.USED_VMS);
Integer unused_vms = 0;
if (vms == null || vms.equals("")) {
vms = 0;
} else if (used_vms == null || used_vms.equals("")) {
used_vms = 0;
} else {
unused_vms = vms - used_vms;
}
vo.setCloudPlatformAll(String.valueOf(vms));
vo.setCloudPlatformUsed(String.valueOf(used_vms));
vo.setCloudPlatformUnUsed(String.valueOf(unused_vms));
// 二 、请求 虚拟内核
Integer cpus = jsonObj.getInteger(Constants.OverView.CPUS);
Integer used_cpus = jsonObj.getInteger(Constants.OverView.USED_CPUS);
Integer unused_cpus = 0;
if (cpus == null || cpus.equals("")) {
cpus = 0;
} else if (used_cpus == null || used_cpus.equals("")) {
used_cpus = 0;
} else {
unused_cpus = cpus - used_cpus;
}
vo.setVirtualKernelAll(String.valueOf(cpus));
vo.setVirtualKernelUsed(String.valueOf(used_cpus));
vo.setVirtualKernelUnUsed(String.valueOf(unused_cpus));
// 三、请求 虚拟内存
Integer mems = jsonObj.getInteger(Constants.OverView.MEMS);
Integer used_mem = jsonObj.getInteger(Constants.OverView.USED_MEM);
Integer unused_mem = 0;
if (mems == null || mems.equals("")) {
mems = 0;
} else if (used_mem == null || used_mem.equals("")) {
used_mem = 0;
} else {
unused_mem = mems - used_mem;
}
vo.setVirtualMemoryAll(String.valueOf(mems));
vo.setVirtualMemoryUsed(String.valueOf(used_mem));
vo.setVirtualMemoryUnUsed(String.valueOf(unused_mem));
// 四、请求 安全组
Integer security_group = jsonObj.getInteger(Constants.OverView.SECURITY_GROUP);
Integer used_security_group = jsonObj.getInteger(Constants.OverView.USED_SECURITY_GROUP);
Integer unused_security_group = 0;
if (security_group == null || security_group.equals("")) {
security_group = 0;
} else if (used_security_group == null || used_security_group.equals("")) {
used_security_group = 0;
} else {
unused_security_group = security_group - used_security_group;
}
vo.setSecurityGroupAll(String.valueOf(security_group));
vo.setSecurityGroupUsed(String.valueOf(used_security_group));
vo.setSecurityGroupUnUsed(String.valueOf(unused_security_group));
}else{
/**provider连接数*/
HttpClientRequest requestProvider = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") +
"/rest/provider_network_cn");
try {
requestProvider.process(new HttpClientResponseHandler() {
@Override
public void handle(String response, String retCode, String msg) throws HttpClientException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
provider[0] = JSONObject.parseObject(response);
System.out.println(provider[0].get("result").toString()+"rrrrrrrrrrrrr");
String jsonObj = provider[0].get("result").toString();
reslt[0] =jsonObj;
if (jsonObj == null)
return;
}
});
} catch (HttpClientException e) {
e.printStackTrace();
}
//////////////////ManoException是运行是异常
if(reslt[0].equals("0")){
throw new ManoException("0","没建立与openstack的连接,请检查openstack资源中openstack提供者是否已添加");
}else{
throw new ManoException("1","没有数据");
}
//throw new HttpClientException("没建立连接");
}
}});
} catch (HttpClientException e) {
throw new ManoException("1","没建立连接");
}
logger.info("SummeryOfResourcesVO is " + vo.toString());
return vo;
}
}
//运行时异常
package com.certusnet.nfv.mano.exception;
import java.util.Locale;
import javax.ws.rs.ext.Provider;
import org.springframework.web.context.ContextLoaderListener;
/**
* ManoException,统一处理异常编码和异常信息
* @author Biwei
*
*/
@Provider
public class ManoException extends RuntimeException {
public static final long serialVersionUID = 11111111111L;
public ManoException(Throwable throwable) {
super(throwable);
}
public ManoException(String code, String msg) {
super(msg);
this.code = code;
}
/**
* message key
*/
private String code;
/**
* message params
*/
private Object[] params;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
}
相关文章推荐
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- 安装LR11 时,安装Microsoft Visual c++2005 sp1运行时组件,就会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息
- 运行HBase应用开发程序产生异常,提示信息包含org.apache.hadoop.hbase.ipc.controller.ServerRpcControllerFactory的解决办法
- qtp 获取弹出提示框(异常框)的文本显示信息(正则表达式和自定义函数应用)
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- Android SurfaceView运行机制剖析--处理切换到后台再重新进入程序时的异常
- win764位loadrunner安装问题:提示:少了Microsoft Visual c++2005 sp1运行时组件,安装时会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息,无法正常安装;
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- Android SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- Android游戏开发19:SurfaceView运行机制剖析--处理切换到后台再重新进入程序时的异常
- SURFACEVIEW运行机制详解—剖析BACK与HOME按键及切入后台等异常处理
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- 【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- (转)【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理!
- JS去空格、IFrame提示信息做后台操作、Cookie存值获取累加,调整过期时间。
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- 使用图片预加载,解决断网后无法从后台获取提示网络异常的logo图片的问题
- C++运行时获取类型信息的type_info类与bad_typeid异常
- QTP自带订票实现循环运行,参数化和将异常提示信息输出