初步完成简单异常处理
This commit is contained in:
parent
acbdf63875
commit
ebc965fa21
@ -18,4 +18,5 @@ public class ASESpringUtil {
|
|||||||
public <T> T getBean(Class<T> tClass){
|
public <T> T getBean(Class<T> tClass){
|
||||||
return applicationContext.getBean(tClass);
|
return applicationContext.getBean(tClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ public class DataModelRepositorySearcher {
|
|||||||
private boolean present;
|
private boolean present;
|
||||||
|
|
||||||
public void getDataModelRepositoryClass(String subSystem, String dataModel){
|
public void getDataModelRepositoryClass(String subSystem, String dataModel){
|
||||||
|
dataModel = doCheckName(dataModel);
|
||||||
try {
|
try {
|
||||||
this.repositoryClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
|
this.repositoryClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
|
||||||
this.setPresent(true);
|
this.setPresent(true);
|
||||||
@ -32,13 +33,21 @@ public class DataModelRepositorySearcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T getDataModelRepositoryInstance(Class<T> dataModeRepositoryClass) {
|
public <T> T getDataModelRepositoryInstance() {
|
||||||
if(isPresent()) {
|
if(isPresent()) {
|
||||||
return springUtil.getBean(dataModeRepositoryClass);
|
return (T) springUtil.getBean(repositoryClass);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String doCheckName(String string) {
|
||||||
|
char[] charArray = string.toCharArray();
|
||||||
|
if(Character.isLowerCase(charArray[0])) charArray[0] -= 32;
|
||||||
|
else return string;
|
||||||
|
return String.valueOf(charArray);
|
||||||
|
}
|
||||||
|
|
||||||
private String dataModelFullNameGenerator(String subSystem, String dataModel){
|
private String dataModelFullNameGenerator(String subSystem, String dataModel){
|
||||||
return new String(repositoryPath + subSystem + "." + dataModel + "Repository");
|
return new String(repositoryPath + subSystem + "." + dataModel + "Repository");
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ public class DataModelSearcher {
|
|||||||
private boolean present;
|
private boolean present;
|
||||||
|
|
||||||
public void getDataModelClass(String subSystem, String dataModel) {
|
public void getDataModelClass(String subSystem, String dataModel) {
|
||||||
|
dataModel = doCheckName(dataModel);
|
||||||
try {
|
try {
|
||||||
dataModelClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
|
dataModelClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
|
||||||
this.setPresent(true);
|
this.setPresent(true);
|
||||||
@ -56,6 +57,13 @@ public class DataModelSearcher {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String doCheckName(String string) {
|
||||||
|
char[] charArray = string.toCharArray();
|
||||||
|
if(Character.isLowerCase(charArray[0])) charArray[0] -= 32;
|
||||||
|
else return string;
|
||||||
|
return String.valueOf(charArray);
|
||||||
|
}
|
||||||
|
|
||||||
private String dataModelFullNameGenerator(String subSystem, String dataModel){
|
private String dataModelFullNameGenerator(String subSystem, String dataModel){
|
||||||
return new String(modelPath + subSystem + "." + dataModel);
|
return new String(modelPath + subSystem + "." + dataModel);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.codesdream.ase.component.error;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "error")
|
||||||
|
public class ErrorResponse
|
||||||
|
{
|
||||||
|
public ErrorResponse(String message, List<String> details) {
|
||||||
|
super();
|
||||||
|
this.message = message;
|
||||||
|
this.details = details;
|
||||||
|
}
|
||||||
|
|
||||||
|
//General error message about nature of error
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
//Specific errors in API request processing
|
||||||
|
private List<String> details;
|
||||||
|
|
||||||
|
//Getter and setters
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.codesdream.ase.controller;
|
||||||
|
|
||||||
|
import com.codesdream.ase.component.error.ErrorResponse;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class ASEControllerAdvice {
|
||||||
|
@ExceptionHandler(value = {RuntimeException.class})
|
||||||
|
public final ResponseEntity<Object> handleRuntimeException(RuntimeException e, WebRequest webRequest){
|
||||||
|
List<String> details = new ArrayList<>();
|
||||||
|
details.add(e.getLocalizedMessage());
|
||||||
|
ErrorResponse errorResponse = new ErrorResponse("Runtime Error", details);
|
||||||
|
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,26 +1,44 @@
|
|||||||
package com.codesdream.ase.controller;
|
package com.codesdream.ase.controller;
|
||||||
|
|
||||||
|
import com.codesdream.ase.component.error.ErrorResponse;
|
||||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.context.request.WebRequest;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class ASEErrorController implements ErrorController {
|
public class ASEErrorController implements ErrorController {
|
||||||
|
|
||||||
@RequestMapping("/error")
|
@RequestMapping("/error")
|
||||||
public String handleError(HttpServletRequest request, Model model){
|
public String handleError(HttpServletRequest request, Model model){
|
||||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||||
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
|
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
|
||||||
|
|
||||||
// 页面未找到
|
// 页面未找到
|
||||||
if(statusCode == 404){
|
if(statusCode == HttpStatus.NOT_FOUND.value()){
|
||||||
return "not_found";
|
return "not_found";
|
||||||
}
|
}
|
||||||
|
model.addAttribute("http_status", statusCode);
|
||||||
|
model.addAttribute("exception_name", exception.getClass().getName());
|
||||||
|
model.addAttribute("exception_message", exception.getMessage());
|
||||||
|
List<String> stack_infos = new ArrayList<>();
|
||||||
|
|
||||||
|
for(StackTraceElement element : exception.getStackTrace()){
|
||||||
|
String s = element.toString();
|
||||||
|
stack_infos.add(s);
|
||||||
|
}
|
||||||
|
model.addAttribute("error_stack", stack_infos);
|
||||||
|
|
||||||
|
return "error";
|
||||||
|
|
||||||
return"error";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.codesdream.ase.controller;
|
package com.codesdream.ase.controller;
|
||||||
|
|
||||||
|
import com.codesdream.ase.component.ASESpringUtil;
|
||||||
|
import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher;
|
||||||
|
import com.codesdream.ase.component.datamanager.DataModelSearcher;
|
||||||
import org.apache.catalina.core.ApplicationContext;
|
import org.apache.catalina.core.ApplicationContext;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
@ -10,31 +13,28 @@ import org.w3c.dom.Entity;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/database")
|
@RequestMapping(value = "/database")
|
||||||
public class DataManagerController {
|
public class DataManagerController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ASESpringUtil springUtil;
|
||||||
|
|
||||||
@RequestMapping(value = "{subSystem}/{dataModel}/query")
|
@RequestMapping(value = "{subSystem}/{dataModel}/query")
|
||||||
private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem)
|
private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem) {
|
||||||
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
DataModelRepositorySearcher dataModelRepositorySearcher = springUtil.getBean(DataModelRepositorySearcher.class);
|
||||||
String dataModelFullName = "com.codesdream.ase.model." + subSystem + "." + dataModel;
|
DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class);
|
||||||
String repositoryFullName = "com.codesdream.ase.repository." + subSystem + "." + dataModel + "Repository";
|
|
||||||
try {
|
dataModelSearcher.getDataModelClass(subSystem, dataModel);
|
||||||
Class<?> entityModelClass = Class.forName(dataModelFullName);
|
if(!dataModelSearcher.isPresent()){
|
||||||
Collection<String> paramArgs = new ArrayList<>();
|
throw new RuntimeException("Data Model Not Found");
|
||||||
for(Field field :entityModelClass.getDeclaredFields()){
|
|
||||||
paramArgs.add(field.getName());
|
|
||||||
}
|
}
|
||||||
model.addAttribute("paramArgs", paramArgs);
|
dataModelRepositorySearcher.getDataModelRepositoryClass(subSystem, dataModel);
|
||||||
Object entityModel = entityModelClass.newInstance();
|
if(!dataModelRepositorySearcher.isPresent()){
|
||||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
throw new RuntimeException("Data Model Repository Not Found");
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
return null;
|
return "query";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.codesdream.ase.test;
|
package com.codesdream.ase.test;
|
||||||
|
|
||||||
import com.codesdream.ase.component.ASESpringUtil;
|
import com.codesdream.ase.component.ASESpringUtil;
|
||||||
|
import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher;
|
||||||
import com.codesdream.ase.component.datamanager.DataModelSearcher;
|
import com.codesdream.ase.component.datamanager.DataModelSearcher;
|
||||||
|
import com.codesdream.ase.repository.permission.UserRepository;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -31,6 +33,12 @@ public class DataManagerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dataModelRepositorySearcherTest(){
|
public void dataModelRepositorySearcherTest(){
|
||||||
|
DataModelRepositorySearcher dataModelRepositorySearcher =
|
||||||
|
springUtil.getBean(DataModelRepositorySearcher.class);
|
||||||
|
|
||||||
|
dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User");
|
||||||
|
Assert.assertTrue(dataModelRepositorySearcher.isPresent());
|
||||||
|
UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,78 @@
|
|||||||
<h1><font color="#FFFFFF">Error</font></h1>
|
<h1><font color="#FFFFFF">Error</font></h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row-fluid">
|
||||||
|
<div class="span12">
|
||||||
|
<h2>
|
||||||
|
检测到异常发生
|
||||||
|
</h2>
|
||||||
|
<p>
|
||||||
|
<strong><strong>当服务器端程序内部某个模块抛出异常时,该界面就会被显示。此次异常的简要信息已被异常管理子系统收集并概括如下表:</strong></strong>
|
||||||
|
</p>
|
||||||
|
<table class="table table-bordered">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
编号
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
信息项
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
值
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
0
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
服务器返回HTTP状态
|
||||||
|
</td>
|
||||||
|
<td th:text="${http_status}">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
1
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
抛出的异常类型
|
||||||
|
</td>
|
||||||
|
<td th:text="${exception_name}">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
2
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
异常附带消息
|
||||||
|
</td>
|
||||||
|
<td th:text="${exception_message}">
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<h3>
|
||||||
|
运行栈相关信息补充
|
||||||
|
</h3>
|
||||||
|
<p th:each="line : ${error_stack}">
|
||||||
|
<span th:text="${line}"></span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
11
src/main/resources/templates/query.html
Normal file
11
src/main/resources/templates/query.html
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user