Commit 37f1305f by 钱炳权

inhance exception tips

parent 0745a33d
package com.example.fuzzControll.controller; package com.example.fuzzControll.controller;
import com.example.fuzzControll.exception.ServerException;
import com.example.fuzzControll.pojo.vo.AjaxResult; import com.example.fuzzControll.pojo.vo.AjaxResult;
import com.example.fuzzControll.service.GetServerMessageService; import com.example.fuzzControll.service.GetServerMessageService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -26,8 +27,8 @@ public class KittyServerMessageController { ...@@ -26,8 +27,8 @@ public class KittyServerMessageController {
public AjaxResult getStats( ) { public AjaxResult getStats( ) {
try { try {
return AjaxResult.success(getServerMessageService.getStats()); return AjaxResult.success(getServerMessageService.getStats());
} catch (Exception e) { } catch (ServerException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("stats信息获取失败!"); return AjaxResult.error("stats信息获取失败!");
} }
} }
...@@ -38,8 +39,8 @@ public class KittyServerMessageController { ...@@ -38,8 +39,8 @@ public class KittyServerMessageController {
public AjaxResult getTemplateInfo( ) { public AjaxResult getTemplateInfo( ) {
try { try {
return AjaxResult.success(getServerMessageService.getTemplateInfo()); return AjaxResult.success(getServerMessageService.getTemplateInfo());
} catch (Exception e) { } catch (ServerException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("templateInfo信息获取失败!"); return AjaxResult.error("templateInfo信息获取失败!");
} }
} }
...@@ -50,8 +51,8 @@ public class KittyServerMessageController { ...@@ -50,8 +51,8 @@ public class KittyServerMessageController {
public AjaxResult getStages( ) { public AjaxResult getStages( ) {
try { try {
return AjaxResult.success(getServerMessageService.getStages()); return AjaxResult.success(getServerMessageService.getStages());
} catch (Exception e) { } catch (ServerException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("stages信息获取失败!"); return AjaxResult.error("stages信息获取失败!");
} }
} }
...@@ -62,8 +63,8 @@ public class KittyServerMessageController { ...@@ -62,8 +63,8 @@ public class KittyServerMessageController {
public AjaxResult getReport( ) { public AjaxResult getReport( ) {
try { try {
return AjaxResult.success(getServerMessageService.getReport()); return AjaxResult.success(getServerMessageService.getReport());
} catch (Exception e) { } catch (ServerException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("report信息获取失败!"); return AjaxResult.error("report信息获取失败!");
} }
} }
......
package com.example.fuzzControll.controller; package com.example.fuzzControll.controller;
import com.example.fuzzControll.exception.CmdException;
import com.example.fuzzControll.exception.FileException;
import com.example.fuzzControll.pojo.vo.AjaxResult; import com.example.fuzzControll.pojo.vo.AjaxResult;
import com.example.fuzzControll.service.SeedFileService; import com.example.fuzzControll.service.SeedFileService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -32,8 +34,8 @@ public class SeedFileController { ...@@ -32,8 +34,8 @@ public class SeedFileController {
List<String> files = null; List<String> files = null;
try { try {
files = service.getSeedFiles(); files = service.getSeedFiles();
} catch (Exception e) { } catch (CmdException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("种子文件获取失败!"); return AjaxResult.error("种子文件获取失败!");
} }
return AjaxResult.success(files); return AjaxResult.success(files);
...@@ -46,8 +48,8 @@ public class SeedFileController { ...@@ -46,8 +48,8 @@ public class SeedFileController {
public AjaxResult delete(@PathVariable("fileName")String fileName) { public AjaxResult delete(@PathVariable("fileName")String fileName) {
try { try {
service.delFile(fileName); service.delFile(fileName);
} catch (Exception e) { } catch (CmdException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("种子文件删除失败!"); return AjaxResult.error("种子文件删除失败!");
} }
return AjaxResult.success("种子文件删除成功!"); return AjaxResult.success("种子文件删除成功!");
...@@ -59,8 +61,8 @@ public class SeedFileController { ...@@ -59,8 +61,8 @@ public class SeedFileController {
public AjaxResult upload(MultipartFile file) { public AjaxResult upload(MultipartFile file) {
try { try {
service.upload(file); service.upload(file);
} catch (Exception e) { } catch (FileException | CmdException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("种子文件upload失败!"); return AjaxResult.error("种子文件upload失败!");
} }
return AjaxResult.success("种子文件upload成功!"); return AjaxResult.success("种子文件upload成功!");
......
...@@ -22,4 +22,8 @@ public class BaseException extends RuntimeException{ ...@@ -22,4 +22,8 @@ public class BaseException extends RuntimeException{
this.defaultMessage = defaultMessage; this.defaultMessage = defaultMessage;
this.module = module; this.module = module;
} }
public String getDefaultMessage() {
return defaultMessage;
}
} }
...@@ -8,4 +8,5 @@ public class FileException extends BaseException{ ...@@ -8,4 +8,5 @@ public class FileException extends BaseException{
public FileException(String defaultMessage) { public FileException(String defaultMessage) {
super(defaultMessage, "file"); super(defaultMessage, "file");
} }
} }
...@@ -26,6 +26,7 @@ public class GetServerMessageImpl implements GetServerMessageService { ...@@ -26,6 +26,7 @@ public class GetServerMessageImpl implements GetServerMessageService {
CloseableHttpResponse templateInfoResponse = httpClient.execute(httpGetTemplateInfo);) { CloseableHttpResponse templateInfoResponse = httpClient.execute(httpGetTemplateInfo);) {
return EntityUtils.toString(templateInfoResponse.getEntity(), "utf-8"); return EntityUtils.toString(templateInfoResponse.getEntity(), "utf-8");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new ServerException("get server templateInfo error !"); throw new ServerException("get server templateInfo error !");
} }
} }
...@@ -35,6 +36,7 @@ public class GetServerMessageImpl implements GetServerMessageService { ...@@ -35,6 +36,7 @@ public class GetServerMessageImpl implements GetServerMessageService {
CloseableHttpResponse statsResponse = httpClient.execute(httpGetStats);) { CloseableHttpResponse statsResponse = httpClient.execute(httpGetStats);) {
return EntityUtils.toString(statsResponse.getEntity(), "utf-8"); return EntityUtils.toString(statsResponse.getEntity(), "utf-8");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new ServerException("get server stats error !"); throw new ServerException("get server stats error !");
} }
} }
...@@ -44,6 +46,7 @@ public class GetServerMessageImpl implements GetServerMessageService { ...@@ -44,6 +46,7 @@ public class GetServerMessageImpl implements GetServerMessageService {
CloseableHttpResponse reportResponse = httpClient.execute(httpGetStats);) { CloseableHttpResponse reportResponse = httpClient.execute(httpGetStats);) {
return EntityUtils.toString(reportResponse.getEntity(), "utf-8"); return EntityUtils.toString(reportResponse.getEntity(), "utf-8");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new ServerException("get server report error !"); throw new ServerException("get server report error !");
} }
...@@ -54,6 +57,7 @@ public class GetServerMessageImpl implements GetServerMessageService { ...@@ -54,6 +57,7 @@ public class GetServerMessageImpl implements GetServerMessageService {
CloseableHttpResponse stagesResponse = httpClient.execute(httpGetStats);) { CloseableHttpResponse stagesResponse = httpClient.execute(httpGetStats);) {
return EntityUtils.toString(stagesResponse.getEntity(), "utf-8"); return EntityUtils.toString(stagesResponse.getEntity(), "utf-8");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new ServerException("get server stages error !"); throw new ServerException("get server stages error !");
} }
} }
......
...@@ -22,8 +22,8 @@ public class SeedFileServiceImpl implements SeedFileService { ...@@ -22,8 +22,8 @@ public class SeedFileServiceImpl implements SeedFileService {
SeedProperties properties; SeedProperties properties;
@Override @Override
public List<String> getSeedFiles() throws CmdException{ public List<String> getSeedFiles() throws CmdException {
return cmdTools.runCmd(CmdConstent.GET_FILE_NAME + properties.getSeedPath(),"getSeedFiles"); return cmdTools.runCmd(CmdConstent.GET_FILE_NAME + properties.getSeedPath(), "getSeedFiles");
} }
//todo 同步修改可能会出现问题 //todo 同步修改可能会出现问题
...@@ -31,49 +31,38 @@ public class SeedFileServiceImpl implements SeedFileService { ...@@ -31,49 +31,38 @@ public class SeedFileServiceImpl implements SeedFileService {
public void delFile(String fileName) throws CmdException { public void delFile(String fileName) throws CmdException {
int fileCountBefore = 0; int fileCountBefore = 0;
int fileCountAfter = 0; int fileCountAfter = 0;
try {
fileCountBefore = getSeedFileCount("delFile before."); fileCountBefore = getSeedFileCount("delFile before.");
cmdTools.runCmd(CmdConstent.DELETE_FILE + properties.getSeedPath() + "/" + fileName,"delFile"); cmdTools.runCmd(CmdConstent.DELETE_FILE + properties.getSeedPath() + "/" + fileName, "delFile");
fileCountAfter = getSeedFileCount("delFile after."); fileCountAfter = getSeedFileCount("delFile after.");
} catch (CmdException e) { if (fileCountAfter == fileCountBefore) {
throw new CmdException(e.getMessage());
}
if(fileCountAfter==fileCountBefore){
throw new CmdException("Delete unsuccess ! The file has not changed .Attempt to change permissions."); throw new CmdException("Delete unsuccess ! The file has not changed .Attempt to change permissions.");
} }
} }
@Override @Override
public void upload(MultipartFile file) throws FileException,CmdException { public void upload(MultipartFile file) throws FileException, CmdException {
int fileCountBefore = 0; int fileCountBefore = 0;
int fileCountAfter = 0; int fileCountAfter = 0;
try {
fileCountBefore = getSeedFileCount("upload before."); fileCountBefore = getSeedFileCount("upload before.");
fileTools.load(file); fileTools.load(file);
fileCountAfter = getSeedFileCount("upload after."); fileCountAfter = getSeedFileCount("upload after.");
} catch (CmdException e) { if (fileCountAfter == fileCountBefore) {
throw new CmdException(e.getMessage());
} catch (FileException e){
throw new FileException(e.getMessage());
}
if(fileCountAfter==fileCountBefore){
throw new FileException("upload file error !The file failed to be submitted.Attempt to change permissions."); throw new FileException("upload file error !The file failed to be submitted.Attempt to change permissions.");
} }
} }
/** /**
*
* 获取种子文件目录下文件数量 * 获取种子文件目录下文件数量
*/ */
@Override @Override
public int getSeedFileCount(String msg) throws CmdException { public int getSeedFileCount(String msg) throws CmdException {
int count = 0; int count = 0;
try { try {
List<String> files = cmdTools.runCmd(CmdConstent.GET_FILE_NAME+ properties.getSeedPath(),"getSeedFileCount"); List<String> files = cmdTools.runCmd(CmdConstent.GET_FILE_NAME + properties.getSeedPath(), "getSeedFileCount");
count = files.size(); count = files.size();
} catch (CmdException e) { } catch (CmdException e) {
throw new CmdException(e.getMessage()+" when "+msg); throw new CmdException(e.getMessage() + " when " + msg);
} }
return count; return count;
} }
......
...@@ -32,6 +32,7 @@ public class CmdTools { ...@@ -32,6 +32,7 @@ public class CmdTools {
printMessage(process.getErrorStream(), new ArrayList<String>()); printMessage(process.getErrorStream(), new ArrayList<String>());
process.waitFor(); process.waitFor();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new CmdException(caller+" run cmd failed!"); throw new CmdException(caller+" run cmd failed!");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment