Commit 37f1305f by 钱炳权

inhance exception tips

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