Skip to content

Commit 1a1d6e8

Browse files
Specify error messages (#242)
1 parent 6dd32bc commit 1a1d6e8

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

agent/src/main/java/com/microsoft/hydralab/agent/runner/ActionExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.microsoft.hydralab.common.util.ThreadUtils;
1111
import org.jetbrains.annotations.NotNull;
1212
import org.slf4j.Logger;
13+
import org.springframework.http.HttpStatus;
1314

1415
import java.lang.reflect.InvocationTargetException;
1516
import java.lang.reflect.Method;
@@ -94,7 +95,7 @@ private Object[] convertArgs(@NotNull DeviceInfo deviceInfo, @NotNull Logger log
9495
try {
9596
methodArgs[i + 1] = JSONObject.parseObject(actionArgs.get(i), DeviceAction.class);
9697
} catch (Exception e1) {
97-
throw new HydraLabRuntimeException(500, "Convert arg failed!", e1);
98+
throw new HydraLabRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Convert arg failed!", e1);
9899
}
99100
}
100101
}

center/src/main/java/com/microsoft/hydralab/center/controller/TestTaskController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public Result<Object> runTestTask(@CurrentSecurityContext SysUser requestor,
7272
if (!userTeamManagementService.checkRequestorTeamRelation(requestor, testTaskSpec.teamId)) {
7373
return Result.error(HttpStatus.UNAUTHORIZED.value(), "Unauthorized, the TestFileSet doesn't belong to user's Teams");
7474
}
75-
if (!testTaskService.checkTestTaskTeamConsistency(testTaskSpec)) {
76-
return Result.error(HttpStatus.UNAUTHORIZED.value(), "Unauthorized, the TestTask is requiring deviceIdentifier that doesn't belong to user's Teams");
77-
}
75+
testTaskService.checkTestTaskTeamConsistency(testTaskSpec);
7876
}
7977
//if the queue is not empty, the task will be added to the queue directly
8078
if (testTaskService.isQueueEmpty() || testTaskService.isDeviceFree(testTaskSpec.deviceIdentifier)) {

center/src/main/java/com/microsoft/hydralab/center/service/DeviceAgentManagementService.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313
import com.microsoft.hydralab.common.entity.common.*;
1414
import com.microsoft.hydralab.common.repository.BlobFileInfoRepository;
1515
import com.microsoft.hydralab.common.repository.StatisticDataRepository;
16-
import com.microsoft.hydralab.common.util.AttachmentService;
17-
import com.microsoft.hydralab.common.util.Const;
18-
import com.microsoft.hydralab.common.util.GlobalConstant;
19-
import com.microsoft.hydralab.common.util.SerializeUtil;
16+
import com.microsoft.hydralab.common.util.*;
2017
import com.microsoft.hydralab.common.util.blob.BlobStorageClient;
2118
import com.microsoft.hydralab.t2c.runner.DriverInfo;
2219
import com.microsoft.hydralab.t2c.runner.T2CJsonParser;
2320
import com.microsoft.hydralab.t2c.runner.TestInfo;
2421
import lombok.extern.slf4j.Slf4j;
2522
import org.slf4j.LoggerFactory;
2623
import org.springframework.beans.factory.annotation.Value;
24+
import org.springframework.http.HttpStatus;
2725
import org.springframework.scheduling.annotation.Scheduled;
2826
import org.springframework.stereotype.Component;
2927
import org.springframework.util.Assert;
@@ -305,18 +303,18 @@ public void cancelTestTaskById(String taskId, String reason) {
305303

306304
public void checkAccessInfo(String name, String key) {
307305
if (key == null) {
308-
throw new RuntimeException("access key is required!");
306+
throw new HydraLabRuntimeException(HttpStatus.UNAUTHORIZED.value(), "Access key is required!");
309307
}
310308
AccessInfo accessInfo = accessInfoMap.get(name);
311-
if (accessInfo != null) {
312-
throw new RuntimeException("please generate access key first!");
309+
if (accessInfo == null) {
310+
throw new HydraLabRuntimeException(HttpStatus.UNAUTHORIZED.value(), "Please generate access key first!");
313311
}
314312
if (!key.equals(accessInfo.getKey())) {
315-
throw new RuntimeException("error access key!");
313+
throw new HydraLabRuntimeException(HttpStatus.UNAUTHORIZED.value(), "Error access key!");
316314
}
317315
int hour = (int) ((new Date().getTime() - accessInfo.getIngestTime().getTime()) / 1000 / 60 / 60);
318316
if (hour > accessLimit) {
319-
throw new RuntimeException("access key has expired!");
317+
throw new HydraLabRuntimeException(HttpStatus.UNAUTHORIZED.value(), "Access key has expired!");
320318
}
321319
}
322320

center/src/main/java/com/microsoft/hydralab/center/service/TestTaskService.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
1111
import com.microsoft.hydralab.common.entity.common.TestTask;
1212
import com.microsoft.hydralab.common.util.Const;
13+
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
1314
import lombok.extern.slf4j.Slf4j;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
17+
import org.springframework.http.HttpStatus;
1618
import org.springframework.scheduling.annotation.Scheduled;
1719
import org.springframework.stereotype.Component;
1820

@@ -138,46 +140,46 @@ public TestTaskQueuedInfo getTestQueuedInfo(String testTaskId) {
138140
return taskQueuedInfo;
139141
}
140142

141-
public boolean checkTestTaskTeamConsistency(TestTaskSpec testTaskSpec) {
143+
public void checkTestTaskTeamConsistency(TestTaskSpec testTaskSpec) throws HydraLabRuntimeException {
142144
if (TestTask.TestRunningType.APPIUM_CROSS.equals(testTaskSpec.runningType)
143145
|| TestTask.TestRunningType.T2C_JSON_TEST.equals(testTaskSpec.runningType)) {
144146
AgentUser agent = agentManageService.getAgent(testTaskSpec.deviceIdentifier);
145147
if (agent == null) {
146-
return false;
148+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "Didn't find AgentUser with given deviceIdentifier!");
149+
}
150+
if (!testTaskSpec.teamId.equals(agent.getTeamId())) {
151+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "AgentUser doesn't belong to the given team in spec!");
147152
}
148-
return testTaskSpec.teamId.equals(agent.getTeamId());
149153
} else {
150154
String deviceIdentifier = testTaskSpec.deviceIdentifier;
151155
if (deviceIdentifier.startsWith(Const.DeviceGroup.GROUP_NAME_PREFIX)) {
152156
DeviceGroup deviceGroup = deviceGroupService.getGroupByName(deviceIdentifier);
153157
if (deviceGroup == null) {
154-
return false;
158+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "Didn't find DeviceGroup with given deviceIdentifier!");
155159
}
156160
if (testTaskSpec.teamId.equals(deviceGroup.getTeamId())) {
157-
return true;
161+
return;
158162
}
159163
if (!deviceGroup.getIsPrivate()) {
160-
return true;
164+
return;
161165
}
162166
deviceAgentManagementService.checkAccessInfo(deviceIdentifier, testTaskSpec.accessKey);
163-
return true;
164167
} else {
165168
DeviceInfo device = deviceAgentManagementService.getDevice(deviceIdentifier);
166169
if (device == null) {
167-
return false;
170+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "Didn't find device with given deviceIdentifier!");
168171
}
169172
AgentUser agent = agentManageService.getAgent(device.getAgentId());
170173
if (agent == null) {
171-
return false;
174+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "Didn't find AgentUser with given agent id!");
172175
}
173176
if (testTaskSpec.teamId.equals(agent.getTeamId())) {
174-
return true;
177+
return;
175178
}
176179
if (!device.getIsPrivate()) {
177-
return true;
180+
return;
178181
}
179182
deviceAgentManagementService.checkAccessInfo(deviceIdentifier, testTaskSpec.accessKey);
180-
return true;
181183
}
182184
}
183185
}

common/src/main/java/com/microsoft/hydralab/common/entity/common/BlobFileInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.alibaba.fastjson.JSONObject;
66
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
77
import lombok.Data;
8+
import org.springframework.http.HttpStatus;
89
import org.springframework.util.DigestUtils;
910

1011
import javax.persistence.Column;
@@ -55,9 +56,9 @@ public BlobFileInfo(File file, String relativePath, String fileType) {
5556
this.setMd5(DigestUtils.md5DigestAsHex(inputStream));
5657
inputStream.close();
5758
} catch (FileNotFoundException e) {
58-
throw new HydraLabRuntimeException(500, "Generate temp file failed!", e);
59+
throw new HydraLabRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Generate temp file failed!", e);
5960
} catch (IOException e) {
60-
throw new HydraLabRuntimeException(500, "Get the MD5 of temp file failed!", e);
61+
throw new HydraLabRuntimeException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Get the MD5 of temp file failed!", e);
6162
}
6263
}
6364

common/src/main/java/com/microsoft/hydralab/common/util/AttachmentService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public File verifyAndSaveFile(@NotNull MultipartFile originFile, String parentDi
244244
String fileSuffix = null;
245245
boolean isMatch = false;
246246
if (filename == null) {
247-
throw new HydraLabRuntimeException(405, "error file type: " + filename);
247+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "error file type: " + filename);
248248
}
249249
if (fileTypes != null) {
250250
for (String fileType : fileTypes) {
@@ -255,7 +255,7 @@ public File verifyAndSaveFile(@NotNull MultipartFile originFile, String parentDi
255255
}
256256
}
257257
if (!isMatch) {
258-
throw new HydraLabRuntimeException(405, "error file type: " + filename);
258+
throw new HydraLabRuntimeException(HttpStatus.BAD_REQUEST.value(), "error file type: " + filename);
259259
}
260260
}
261261

0 commit comments

Comments
 (0)