- Improve email notification cases for unexpected exceptions.

This commit is contained in:
Paulo Gustavo Veiga
2012-08-14 23:55:13 -03:00
parent 6d7c12ca60
commit 09ed4f68b7
9 changed files with 113 additions and 50 deletions

View File

@@ -165,7 +165,7 @@ final public class NotificationService {
final String errorReporterEmail = mailer.getErrorReporterEmail();
if (errorReporterEmail != null && !errorReporterEmail.isEmpty()) {
mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Editor error from " + user.getEmail(), model,
"editorErrorReport.vm");
"errorNotification.vm");
}
} catch (Exception e) {
handleException(e);
@@ -183,7 +183,24 @@ final public class NotificationService {
final String errorReporterEmail = mailer.getErrorReporterEmail();
if (errorReporterEmail != null && !errorReporterEmail.isEmpty()) {
mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Export error from " + user.getEmail(), model,
"editorErrorReport.vm");
"errorNotification.vm");
}
} catch (Exception e) {
handleException(e);
}
}
public void reportUnexpectedError(@NotNull Throwable exception, @Nullable User user, @NotNull String userAgent) {
try {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("errorMsg", stackTraceToString(exception));
model.put("userAgent", userAgent);
final String errorReporterEmail = mailer.getErrorReporterEmail();
if (errorReporterEmail != null && !errorReporterEmail.isEmpty()) {
mailer.sendEmail(mailer.getServerSenderEmail(), errorReporterEmail, "[WiseMapping] Unexpected error from " + (user != null ? user.getEmail() : "anonymous"), model,
"errorNotification.vm");
}
} catch (Exception e) {
handleException(e);

View File

@@ -0,0 +1,43 @@
package com.wisemapping.mail;
import com.wisemapping.model.User;
import com.wisemapping.security.Utils;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.Set;
public class NotifyingExceptionResolver extends SimpleMappingExceptionResolver {
final private Logger logger = Logger.getLogger("com.wisemapping");
private Set<String> exclude = new HashSet<String>();
private NotificationService notificationService;
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (!exclude.contains(ex.getClass().getName())) {
logger.error("An Exception has occurred in the application", ex);
sendNotification(ex, request);
}
return super.doResolveException(request, response, handler, ex);
}
private void sendNotification(@NotNull Exception ex, @NotNull HttpServletRequest request) {
final User user = Utils.getUser();
notificationService.reportUnexpectedError(ex, user, request.getHeader("User-Agent"));
}
public void setExclude(final Set<String> exclude) {
this.exclude = exclude;
}
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
}

View File

@@ -18,7 +18,10 @@
package com.wisemapping.rest;
import com.wisemapping.mail.NotificationService;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestErrors;
import com.wisemapping.security.Utils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -34,6 +37,9 @@ public class BaseController {
@Autowired
private ResourceBundleMessageSource messageSource;
@Autowired
private NotificationService notificationService;
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
@@ -46,14 +52,15 @@ public class BaseController {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public String handleServerErrors(@NotNull Exception ex) {
ex.printStackTrace();
final User user = Utils.getUser();
notificationService.reportUnexpectedError(ex, user, "unknown browser");
return ex.getMessage();
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestErrors handleValidationErrors(@NotNull ValidationException ex) {
return new RestErrors(ex.getErrors(),messageSource);
return new RestErrors(ex.getErrors(), messageSource);
}