Commit 3fc72e3d6f49b91091fae958da0f6fba6743540a

Authored by Swoq
1 parent 827da31d

Fix logging exceptions of controllers

... ... @@ -138,7 +138,11 @@ public class TelemetryController extends BaseController {
138 138 @ResponseBody
139 139 public DeferredResult<ResponseEntity> getAttributeKeys(
140 140 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr) throws ThingsboardException {
141   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr, this::getAttributeKeysCallback);
  141 + try {
  142 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr, this::getAttributeKeysCallback);
  143 + } catch (Exception e) {
  144 + throw handleException(e);
  145 + }
142 146 }
143 147
144 148 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -147,8 +151,12 @@ public class TelemetryController extends BaseController {
147 151 public DeferredResult<ResponseEntity> getAttributeKeysByScope(
148 152 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr
149 153 , @PathVariable("scope") String scope) throws ThingsboardException {
150   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
151   - (result, tenantId, entityId) -> getAttributeKeysCallback(result, tenantId, entityId, scope));
  154 + try {
  155 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
  156 + (result, tenantId, entityId) -> getAttributeKeysCallback(result, tenantId, entityId, scope));
  157 + } catch (Exception e) {
  158 + throw handleException(e);
  159 + }
152 160 }
153 161
154 162 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -157,9 +165,13 @@ public class TelemetryController extends BaseController {
157 165 public DeferredResult<ResponseEntity> getAttributes(
158 166 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
159 167 @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException {
160   - SecurityUser user = getCurrentUser();
161   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
162   - (result, tenantId, entityId) -> getAttributeValuesCallback(result, user, entityId, null, keysStr));
  168 + try {
  169 + SecurityUser user = getCurrentUser();
  170 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
  171 + (result, tenantId, entityId) -> getAttributeValuesCallback(result, user, entityId, null, keysStr));
  172 + } catch (Exception e) {
  173 + throw handleException(e);
  174 + }
163 175 }
164 176
165 177 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -169,9 +181,13 @@ public class TelemetryController extends BaseController {
169 181 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
170 182 @PathVariable("scope") String scope,
171 183 @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException {
172   - SecurityUser user = getCurrentUser();
173   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
174   - (result, tenantId, entityId) -> getAttributeValuesCallback(result, user, entityId, scope, keysStr));
  184 + try {
  185 + SecurityUser user = getCurrentUser();
  186 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_ATTRIBUTES, entityType, entityIdStr,
  187 + (result, tenantId, entityId) -> getAttributeValuesCallback(result, user, entityId, scope, keysStr));
  188 + } catch (Exception e) {
  189 + throw handleException(e);
  190 + }
175 191 }
176 192
177 193 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -179,8 +195,12 @@ public class TelemetryController extends BaseController {
179 195 @ResponseBody
180 196 public DeferredResult<ResponseEntity> getTimeseriesKeys(
181 197 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr) throws ThingsboardException {
182   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
183   - (result, tenantId, entityId) -> Futures.addCallback(tsService.findAllLatest(tenantId, entityId), getTsKeysToResponseCallback(result), MoreExecutors.directExecutor()));
  198 + try {
  199 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
  200 + (result, tenantId, entityId) -> Futures.addCallback(tsService.findAllLatest(tenantId, entityId), getTsKeysToResponseCallback(result), MoreExecutors.directExecutor()));
  201 + } catch (Exception e) {
  202 + throw handleException(e);
  203 + }
184 204 }
185 205
186 206 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -190,10 +210,14 @@ public class TelemetryController extends BaseController {
190 210 @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
191 211 @RequestParam(name = "keys", required = false) String keysStr,
192 212 @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
193   - SecurityUser user = getCurrentUser();
  213 + try {
  214 + SecurityUser user = getCurrentUser();
194 215
195   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
196   - (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr, useStrictDataTypes));
  216 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
  217 + (result, tenantId, entityId) -> getLatestTimeseriesValuesCallback(result, user, entityId, keysStr, useStrictDataTypes));
  218 + } catch (Exception e) {
  219 + throw handleException(e);
  220 + }
197 221 }
198 222
199 223
... ... @@ -211,15 +235,19 @@ public class TelemetryController extends BaseController {
211 235 @RequestParam(name = "agg", defaultValue = "NONE") String aggStr,
212 236 @RequestParam(name = "orderBy", defaultValue = "DESC") String orderBy,
213 237 @RequestParam(name = "useStrictDataTypes", required = false, defaultValue = "false") Boolean useStrictDataTypes) throws ThingsboardException {
214   - return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
215   - (result, tenantId, entityId) -> {
216   - // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
217   - Aggregation agg = interval == 0L ? Aggregation.valueOf(Aggregation.NONE.name()) : Aggregation.valueOf(aggStr);
218   - List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg, orderBy))
219   - .collect(Collectors.toList());
220   -
221   - Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictDataTypes), MoreExecutors.directExecutor());
222   - });
  238 + try {
  239 + return accessValidator.validateEntityAndCallback(getCurrentUser(), Operation.READ_TELEMETRY, entityType, entityIdStr,
  240 + (result, tenantId, entityId) -> {
  241 + // If interval is 0, convert this to a NONE aggregation, which is probably what the user really wanted
  242 + Aggregation agg = interval == 0L ? Aggregation.valueOf(Aggregation.NONE.name()) : Aggregation.valueOf(aggStr);
  243 + List<ReadTsKvQuery> queries = toKeysList(keys).stream().map(key -> new BaseReadTsKvQuery(key, startTs, endTs, interval, limit, agg, orderBy))
  244 + .collect(Collectors.toList());
  245 +
  246 + Futures.addCallback(tsService.findAll(tenantId, entityId, queries), getTsKvListCallback(result, useStrictDataTypes), MoreExecutors.directExecutor());
  247 + });
  248 + } catch (Exception e) {
  249 + throw handleException(e);
  250 + }
223 251 }
224 252
225 253 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -227,8 +255,12 @@ public class TelemetryController extends BaseController {
227 255 @ResponseBody
228 256 public DeferredResult<ResponseEntity> saveDeviceAttributes(@PathVariable("deviceId") String deviceIdStr, @PathVariable("scope") String scope,
229 257 @RequestBody JsonNode request) throws ThingsboardException {
230   - EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE, deviceIdStr);
231   - return saveAttributes(getTenantId(), entityId, scope, request);
  258 + try {
  259 + EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE, deviceIdStr);
  260 + return saveAttributes(getTenantId(), entityId, scope, request);
  261 + } catch (Exception e) {
  262 + throw handleException(e);
  263 + }
232 264 }
233 265
234 266 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -237,8 +269,12 @@ public class TelemetryController extends BaseController {
237 269 public DeferredResult<ResponseEntity> saveEntityAttributesV1(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
238 270 @PathVariable("scope") String scope,
239 271 @RequestBody JsonNode request) throws ThingsboardException {
240   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
241   - return saveAttributes(getTenantId(), entityId, scope, request);
  272 + try {
  273 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  274 + return saveAttributes(getTenantId(), entityId, scope, request);
  275 + } catch (Exception e) {
  276 + throw handleException(e);
  277 + }
242 278 }
243 279
244 280 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -247,8 +283,12 @@ public class TelemetryController extends BaseController {
247 283 public DeferredResult<ResponseEntity> saveEntityAttributesV2(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
248 284 @PathVariable("scope") String scope,
249 285 @RequestBody JsonNode request) throws ThingsboardException {
250   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
251   - return saveAttributes(getTenantId(), entityId, scope, request);
  286 + try {
  287 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  288 + return saveAttributes(getTenantId(), entityId, scope, request);
  289 + } catch (Exception e) {
  290 + throw handleException(e);
  291 + }
252 292 }
253 293
254 294 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -257,8 +297,12 @@ public class TelemetryController extends BaseController {
257 297 public DeferredResult<ResponseEntity> saveEntityTelemetry(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
258 298 @PathVariable("scope") String scope,
259 299 @RequestBody String requestBody) throws ThingsboardException {
260   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
261   - return saveTelemetry(getTenantId(), entityId, requestBody, 0L);
  300 + try {
  301 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  302 + return saveTelemetry(getTenantId(), entityId, requestBody, 0L);
  303 + } catch (Exception e) {
  304 + throw handleException(e);
  305 + }
262 306 }
263 307
264 308 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -267,8 +311,12 @@ public class TelemetryController extends BaseController {
267 311 public DeferredResult<ResponseEntity> saveEntityTelemetryWithTTL(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
268 312 @PathVariable("scope") String scope, @PathVariable("ttl") Long ttl,
269 313 @RequestBody String requestBody) throws ThingsboardException {
270   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
271   - return saveTelemetry(getTenantId(), entityId, requestBody, ttl);
  314 + try {
  315 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  316 + return saveTelemetry(getTenantId(), entityId, requestBody, ttl);
  317 + } catch (Exception e) {
  318 + throw handleException(e);
  319 + }
272 320 }
273 321
274 322 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -280,8 +328,12 @@ public class TelemetryController extends BaseController {
280 328 @RequestParam(name = "startTs", required = false) Long startTs,
281 329 @RequestParam(name = "endTs", required = false) Long endTs,
282 330 @RequestParam(name = "rewriteLatestIfDeleted", defaultValue = "false") boolean rewriteLatestIfDeleted) throws ThingsboardException {
283   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
284   - return deleteTimeseries(entityId, keysStr, deleteAllDataForKeys, startTs, endTs, rewriteLatestIfDeleted);
  331 + try {
  332 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  333 + return deleteTimeseries(entityId, keysStr, deleteAllDataForKeys, startTs, endTs, rewriteLatestIfDeleted);
  334 + } catch (Exception e) {
  335 + throw handleException(e);
  336 + }
285 337 }
286 338
287 339 private DeferredResult<ResponseEntity> deleteTimeseries(EntityId entityIdStr, String keysStr, boolean deleteAllDataForKeys,
... ... @@ -335,8 +387,12 @@ public class TelemetryController extends BaseController {
335 387 public DeferredResult<ResponseEntity> deleteEntityAttributes(@PathVariable("deviceId") String deviceIdStr,
336 388 @PathVariable("scope") String scope,
337 389 @RequestParam(name = "keys") String keysStr) throws ThingsboardException {
338   - EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE, deviceIdStr);
339   - return deleteAttributes(entityId, scope, keysStr);
  390 + try {
  391 + EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE, deviceIdStr);
  392 + return deleteAttributes(entityId, scope, keysStr);
  393 + } catch (Exception e) {
  394 + throw handleException(e);
  395 + }
340 396 }
341 397
342 398 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
... ... @@ -345,8 +401,12 @@ public class TelemetryController extends BaseController {
345 401 public DeferredResult<ResponseEntity> deleteEntityAttributes(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
346 402 @PathVariable("scope") String scope,
347 403 @RequestParam(name = "keys") String keysStr) throws ThingsboardException {
348   - EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
349   - return deleteAttributes(entityId, scope, keysStr);
  404 + try {
  405 + EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
  406 + return deleteAttributes(entityId, scope, keysStr);
  407 + } catch (Exception e) {
  408 + throw handleException(e);
  409 + }
350 410 }
351 411
352 412 private DeferredResult<ResponseEntity> deleteAttributes(EntityId entityIdSrc, String scope, String keysStr) throws ThingsboardException {
... ...
... ... @@ -34,13 +34,10 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
34 34 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
35 35 import org.thingsboard.server.common.data.exception.ThingsboardException;
36 36 import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
37   -import org.thingsboard.server.dao.exception.DataValidationException;
38   -import org.thingsboard.server.dao.exception.IncorrectParameterException;
39 37 import org.thingsboard.server.service.security.exception.AuthMethodNotSupportedException;
40 38 import org.thingsboard.server.service.security.exception.JwtExpiredTokenException;
41 39 import org.thingsboard.server.service.security.exception.UserPasswordExpiredException;
42 40
43   -import javax.mail.MessagingException;
44 41 import javax.servlet.ServletException;
45 42 import javax.servlet.http.HttpServletRequest;
46 43 import javax.servlet.http.HttpServletResponse;
... ... @@ -74,17 +71,6 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
74 71 try {
75 72 response.setContentType(MediaType.APPLICATION_JSON_VALUE);
76 73
77   - String cause = "";
78   - if (exception.getCause() != null) {
79   - cause = exception.getCause().getClass().getCanonicalName();
80   - }
81   - if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException
82   - || exception instanceof DataValidationException || cause.contains("IncorrectParameterException")) {
83   - exception = new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS);
84   - } else if (exception instanceof MessagingException) {
85   - exception = new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL);
86   - }
87   -
88 74 if (exception instanceof ThingsboardException) {
89 75 ThingsboardException thingsboardException = (ThingsboardException) exception;
90 76 if (thingsboardException.getErrorCode() == ThingsboardErrorCode.SUBSCRIPTION_VIOLATION) {
... ...
... ... @@ -35,6 +35,7 @@ public class WidgetsBundle extends SearchTextBased<WidgetsBundleId> implements H
35 35 private String title;
36 36 private String image;
37 37 @NoXss
  38 + @Length(fieldName = "description")
38 39 private String description;
39 40
40 41 public WidgetsBundle() {
... ...