初步完成简单异常处理

This commit is contained in:
Saturneric 2020-02-17 19:13:33 +08:00
parent acbdf63875
commit ebc965fa21
10 changed files with 196 additions and 24 deletions

View File

@ -18,4 +18,5 @@ public class ASESpringUtil {
public <T> T getBean(Class<T> tClass){
return applicationContext.getBean(tClass);
}
}

View File

@ -24,6 +24,7 @@ public class DataModelRepositorySearcher {
private boolean present;
public void getDataModelRepositoryClass(String subSystem, String dataModel){
dataModel = doCheckName(dataModel);
try {
this.repositoryClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
this.setPresent(true);
@ -32,13 +33,21 @@ public class DataModelRepositorySearcher {
}
}
public <T> T getDataModelRepositoryInstance(Class<T> dataModeRepositoryClass) {
public <T> T getDataModelRepositoryInstance() {
if(isPresent()) {
return springUtil.getBean(dataModeRepositoryClass);
return (T) springUtil.getBean(repositoryClass);
}
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){
return new String(repositoryPath + subSystem + "." + dataModel + "Repository");
}

View File

@ -22,6 +22,7 @@ public class DataModelSearcher {
private boolean present;
public void getDataModelClass(String subSystem, String dataModel) {
dataModel = doCheckName(dataModel);
try {
dataModelClass = Class.forName(dataModelFullNameGenerator(subSystem, dataModel));
this.setPresent(true);
@ -56,6 +57,13 @@ public class DataModelSearcher {
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){
return new String(modelPath + subSystem + "." + dataModel);
}

View File

@ -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
}

View File

@ -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);
}
}

View File

@ -1,26 +1,44 @@
package com.codesdream.ase.controller;
import com.codesdream.ase.component.error.ErrorResponse;
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.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.context.request.WebRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ASEErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request, Model model){
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
// 页面未找到
if(statusCode == 404){
if(statusCode == HttpStatus.NOT_FOUND.value()){
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

View File

@ -1,5 +1,8 @@
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.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -10,31 +13,28 @@ import org.w3c.dom.Entity;
import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
@Controller
@RequestMapping(value = "/database")
public class DataManagerController {
@Resource
ASESpringUtil springUtil;
@RequestMapping(value = "{subSystem}/{dataModel}/query")
private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String dataModelFullName = "com.codesdream.ase.model." + subSystem + "." + dataModel;
String repositoryFullName = "com.codesdream.ase.repository." + subSystem + "." + dataModel + "Repository";
try {
Class<?> entityModelClass = Class.forName(dataModelFullName);
Collection<String> paramArgs = new ArrayList<>();
for(Field field :entityModelClass.getDeclaredFields()){
paramArgs.add(field.getName());
}
model.addAttribute("paramArgs", paramArgs);
Object entityModel = entityModelClass.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw e;
private String queryView(Model model, @PathVariable String dataModel, @PathVariable String subSystem) {
DataModelRepositorySearcher dataModelRepositorySearcher = springUtil.getBean(DataModelRepositorySearcher.class);
DataModelSearcher dataModelSearcher = springUtil.getBean(DataModelSearcher.class);
dataModelSearcher.getDataModelClass(subSystem, dataModel);
if(!dataModelSearcher.isPresent()){
throw new RuntimeException("Data Model Not Found");
}
return null;
dataModelRepositorySearcher.getDataModelRepositoryClass(subSystem, dataModel);
if(!dataModelRepositorySearcher.isPresent()){
throw new RuntimeException("Data Model Repository Not Found");
}
return "query";
}
}

View File

@ -1,7 +1,9 @@
package com.codesdream.ase.test;
import com.codesdream.ase.component.ASESpringUtil;
import com.codesdream.ase.component.datamanager.DataModelRepositorySearcher;
import com.codesdream.ase.component.datamanager.DataModelSearcher;
import com.codesdream.ase.repository.permission.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -31,6 +33,12 @@ public class DataManagerTest {
@Test
public void dataModelRepositorySearcherTest(){
DataModelRepositorySearcher dataModelRepositorySearcher =
springUtil.getBean(DataModelRepositorySearcher.class);
dataModelRepositorySearcher.getDataModelRepositoryClass("permission", "User");
Assert.assertTrue(dataModelRepositorySearcher.isPresent());
UserRepository userRepository = dataModelRepositorySearcher.getDataModelRepositoryInstance();
}

View File

@ -12,6 +12,78 @@
<h1><font color="#FFFFFF">Error</font></h1>
</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>

View 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>