Merge pull request #11843 from thingsboard/fix/hide-email-failures
Hide mail sender error details
This commit is contained in:
		
						commit
						ee8d81a7be
					
				@ -235,7 +235,15 @@ public class AdminController extends BaseController {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            String email = getCurrentUser().getEmail();
 | 
			
		||||
            mailService.sendTestMail(adminSettings.getJsonValue(), email);
 | 
			
		||||
            try {
 | 
			
		||||
                mailService.sendTestMail(adminSettings.getJsonValue(), email);
 | 
			
		||||
            } catch (ThingsboardException e) {
 | 
			
		||||
                String error = e.getMessage();
 | 
			
		||||
                if (e.getCause() != null) {
 | 
			
		||||
                    error += ": " + e.getCause().getMessage(); // showing actual underlying error for testing purposes
 | 
			
		||||
                }
 | 
			
		||||
                throw new ThingsboardException(error, e.getErrorCode());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -217,7 +217,7 @@ public class AuthController extends BaseController {
 | 
			
		||||
            try {
 | 
			
		||||
                mailService.sendAccountActivatedEmail(loginUrl, email);
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                log.info("Unable to send account activation email [{}]", e.getMessage());
 | 
			
		||||
                log.warn("Unable to send account activation email [{}]", e.getMessage());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -256,7 +256,11 @@ public class AuthController extends BaseController {
 | 
			
		||||
            String baseUrl = systemSecurityService.getBaseUrl(user.getTenantId(), user.getCustomerId(), request);
 | 
			
		||||
            String loginUrl = String.format("%s/login", baseUrl);
 | 
			
		||||
            String email = user.getEmail();
 | 
			
		||||
            mailService.sendPasswordWasResetEmail(loginUrl, email);
 | 
			
		||||
            try {
 | 
			
		||||
                mailService.sendPasswordWasResetEmail(loginUrl, email);
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                log.warn("Couldn't send password was reset email: {}", e.getMessage());
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            eventPublisher.publishEvent(new UserCredentialsInvalidationEvent(securityUser.getId()));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -402,7 +402,7 @@ public abstract class BaseController {
 | 
			
		||||
                || exception instanceof DataValidationException || cause instanceof 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);
 | 
			
		||||
            return new ThingsboardException("Unable to send mail", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        } else if (exception instanceof AsyncRequestTimeoutException) {
 | 
			
		||||
            return new ThingsboardException("Request timeout", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        } else if (exception instanceof DataAccessException) {
 | 
			
		||||
 | 
			
		||||
@ -219,7 +219,11 @@ public class UserController extends BaseController {
 | 
			
		||||
        accessControlService.checkPermission(securityUser, Resource.USER, Operation.READ, user.getId(), user);
 | 
			
		||||
 | 
			
		||||
        UserActivationLink activationLink = tbUserService.getActivationLink(securityUser.getTenantId(), securityUser.getCustomerId(), user.getId(), request);
 | 
			
		||||
        mailService.sendActivationEmail(activationLink.value(), activationLink.ttlMs(), email);
 | 
			
		||||
        try {
 | 
			
		||||
            mailService.sendActivationEmail(activationLink.value(), activationLink.ttlMs(), email);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new ThingsboardException("Couldn't send user activation email", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ApiOperation(value = "Get activation link (getActivationLink)",
 | 
			
		||||
 | 
			
		||||
@ -60,7 +60,7 @@ public class DefaultUserService extends AbstractTbEntityService implements TbUse
 | 
			
		||||
                    mailService.sendActivationEmail(activationLink.value(), activationLink.ttlMs(), savedUser.getEmail());
 | 
			
		||||
                } catch (ThingsboardException e) {
 | 
			
		||||
                    userService.deleteUser(tenantId, savedUser);
 | 
			
		||||
                    throw e;
 | 
			
		||||
                    throw new ThingsboardException("Couldn't send user activation email", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            logEntityActionService.logEntityAction(tenantId, savedUser.getId(), savedUser, customerId, actionType, user);
 | 
			
		||||
 | 
			
		||||
@ -443,17 +443,16 @@ public class DefaultMailService implements MailService {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void sendMailWithTimeout(JavaMailSender mailSender, MimeMessage msg, long timeout) {
 | 
			
		||||
    private void sendMailWithTimeout(JavaMailSender mailSender, MimeMessage msg, long timeout) throws ThingsboardException {
 | 
			
		||||
        var submittedMail = Futures.withTimeout(
 | 
			
		||||
                mailExecutorService.submit(() -> mailSender.send(msg)),
 | 
			
		||||
                timeout, TimeUnit.MILLISECONDS, timeoutScheduler);
 | 
			
		||||
        try {
 | 
			
		||||
            submittedMail.get(timeout, TimeUnit.MILLISECONDS);
 | 
			
		||||
        } catch (TimeoutException e) {
 | 
			
		||||
            log.debug("Error during mail submission", e);
 | 
			
		||||
            throw new RuntimeException("Timeout!");
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new RuntimeException(ExceptionUtils.getRootCause(e));
 | 
			
		||||
            throw new ThingsboardException("Unable to send mail", ExceptionUtils.getRootCause(e), ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -463,20 +462,20 @@ public class DefaultMailService implements MailService {
 | 
			
		||||
            Template template = freemarkerConfig.getTemplate(templateLocation);
 | 
			
		||||
            return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
            log.warn("Failed to process mail template: {}", ExceptionUtils.getRootCauseMessage(e));
 | 
			
		||||
            throw new ThingsboardException("Failed to process mail template: " + e.getMessage(), e, ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected ThingsboardException handleException(Exception exception) {
 | 
			
		||||
        String message;
 | 
			
		||||
        if (exception instanceof NestedRuntimeException) {
 | 
			
		||||
            message = ((NestedRuntimeException) exception).getMostSpecificCause().getMessage();
 | 
			
		||||
        } else {
 | 
			
		||||
            message = exception.getMessage();
 | 
			
		||||
    protected ThingsboardException handleException(Throwable exception) {
 | 
			
		||||
        if (exception instanceof ThingsboardException thingsboardException) {
 | 
			
		||||
            return thingsboardException;
 | 
			
		||||
        }
 | 
			
		||||
        log.warn("Unable to send mail: {}", message);
 | 
			
		||||
        return new ThingsboardException(String.format("Unable to send mail: %s", message),
 | 
			
		||||
                ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        if (exception instanceof NestedRuntimeException) {
 | 
			
		||||
            exception = ((NestedRuntimeException) exception).getMostSpecificCause();
 | 
			
		||||
        }
 | 
			
		||||
        log.warn("Unable to send mail: {}", exception.getMessage());
 | 
			
		||||
        return new ThingsboardException("Unable to send mail: " + exception.getMessage(), ThingsboardErrorCode.GENERAL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -57,7 +57,11 @@ public class EmailTwoFaProvider extends OtpBasedTwoFaProvider<EmailTwoFaProvider
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void sendVerificationCode(SecurityUser user, String verificationCode, EmailTwoFaProviderConfig providerConfig, EmailTwoFaAccountConfig accountConfig) throws ThingsboardException {
 | 
			
		||||
        mailService.sendTwoFaVerificationEmail(accountConfig.getEmail(), verificationCode, providerConfig.getVerificationCodeLifetime());
 | 
			
		||||
        try {
 | 
			
		||||
            mailService.sendTwoFaVerificationEmail(accountConfig.getEmail(), verificationCode, providerConfig.getVerificationCodeLifetime());
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new ThingsboardException("Couldn't send 2FA verification email", ThingsboardErrorCode.GENERAL);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user