Browse Source

Delegation of exception handling logic to the ThingsboardErrorResponseHandler

Any thrown Exception casts to the ThingsBoardException.
pull/5166/head
Swoq 5 years ago
parent
commit
1e3dfc33b2
  1. 23
      application/src/main/java/org/thingsboard/server/controller/BaseController.java
  2. 8
      application/src/main/java/org/thingsboard/server/controller/TelemetryController.java
  3. 27
      application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java

23
application/src/main/java/org/thingsboard/server/controller/BaseController.java

@ -281,6 +281,9 @@ public abstract class BaseController {
@Autowired @Autowired
protected RuleEngineEntityActionService ruleEngineEntityActionService; protected RuleEngineEntityActionService ruleEngineEntityActionService;
@Autowired
protected ThingsboardErrorResponseHandler thingsboardErrorResponseHandler;
@Value("${server.log_controller_error_stack_trace}") @Value("${server.log_controller_error_stack_trace}")
@Getter @Getter
private boolean logControllerErrorStackTrace; private boolean logControllerErrorStackTrace;
@ -299,25 +302,7 @@ public abstract class BaseController {
} }
private ThingsboardException handleException(Exception exception, boolean logException) { private ThingsboardException handleException(Exception exception, boolean logException) {
if (logException && logControllerErrorStackTrace) { return thingsboardErrorResponseHandler.castToThingsboardException(exception, logException);
log.error("Error [{}]", exception.getMessage(), exception);
}
String cause = "";
if (exception.getCause() != null) {
cause = exception.getCause().getClass().getCanonicalName();
}
if (exception instanceof ThingsboardException) {
return (ThingsboardException) exception;
} else if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException
|| exception instanceof DataValidationException || cause.contains("IncorrectParameterException")) {
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS);
} else if (exception instanceof MessagingException) {
return new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL);
} else {
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.GENERAL);
}
} }
<T> T checkNotNull(T reference) throws ThingsboardException { <T> T checkNotNull(T reference) throws ThingsboardException {

8
application/src/main/java/org/thingsboard/server/controller/TelemetryController.java

@ -269,12 +269,8 @@ public class TelemetryController extends BaseController {
public DeferredResult<ResponseEntity> saveEntityAttributesV1(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr, public DeferredResult<ResponseEntity> saveEntityAttributesV1(@PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr,
@PathVariable("scope") String scope, @PathVariable("scope") String scope,
@RequestBody JsonNode request) throws ThingsboardException { @RequestBody JsonNode request) throws ThingsboardException {
try { EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr);
EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr); return saveAttributes(getTenantId(), entityId, scope, request);
return saveAttributes(getTenantId(), entityId, scope, request);
} catch (Exception e) {
throw handleException(e);
}
} }
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")

27
application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java

@ -34,10 +34,13 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.msg.tools.TbRateLimitsException; import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.exception.IncorrectParameterException;
import org.thingsboard.server.service.security.exception.AuthMethodNotSupportedException; import org.thingsboard.server.service.security.exception.AuthMethodNotSupportedException;
import org.thingsboard.server.service.security.exception.JwtExpiredTokenException; import org.thingsboard.server.service.security.exception.JwtExpiredTokenException;
import org.thingsboard.server.service.security.exception.UserPasswordExpiredException; import org.thingsboard.server.service.security.exception.UserPasswordExpiredException;
import javax.mail.MessagingException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -85,9 +88,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
} else if (exception instanceof AuthenticationException) { } else if (exception instanceof AuthenticationException) {
handleAuthenticationException((AuthenticationException) exception, response); handleAuthenticationException((AuthenticationException) exception, response);
} else { } else {
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); handle(castToThingsboardException(exception, true), response);
mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(exception.getMessage(),
ThingsboardErrorCode.GENERAL, HttpStatus.INTERNAL_SERVER_ERROR));
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Can't handle exception", e); log.error("Can't handle exception", e);
@ -95,6 +96,26 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
} }
} }
public ThingsboardException castToThingsboardException(Exception exception, boolean logException) {
if (logException) {
log.error("Error [{}]", exception.getMessage(), exception);
}
String cause = "";
if (exception.getCause() != null) {
cause = exception.getCause().getClass().getCanonicalName();
}
if (exception instanceof IllegalArgumentException || exception instanceof IncorrectParameterException
|| exception instanceof DataValidationException || cause.contains("IncorrectParameterException")) {
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.BAD_REQUEST_PARAMS);
} else if (exception instanceof MessagingException) {
return new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL);
} else {
return new ThingsboardException(exception.getMessage(), ThingsboardErrorCode.GENERAL);
}
}
private void handleThingsboardException(ThingsboardException thingsboardException, HttpServletResponse response) throws IOException { private void handleThingsboardException(ThingsboardException thingsboardException, HttpServletResponse response) throws IOException {

Loading…
Cancel
Save