Beispiel #1
0
  @Override
  public void postToFeed(final User user, final Throwable ex) {
    LogHelper.logException(this.getClass(), ex);
    try {
      postToFeed(user, ExceptionUtils.getStackTrace(ex), FeedType.error);

    } catch (NimbitsException e) {
      LogHelper.logException(this.getClass(), e);
    }
  }
  /**
   * Send mailNotications.
   *
   * @param message The message body itself
   * @param eventType Type of notification
   * @param e An exception (can be null)
   */
  private void sendMailNotifications(String message, NotificationType eventType, Throwable e) {
    String subjectPrefix = SUBJECT_PREFIX + "-" + eventType + ": ";

    // Subject is a specified string + first line of error message
    String subject = subjectPrefix + message.split("\n")[0];

    // Body consists of four parts.
    StringBuffer body = new StringBuffer();

    // 1: The host of the message
    body.append("Host: " + SystemUtils.getLocalHostName() + "\n");
    body.append("Date: " + new Date().toString() + "\n");

    // 2: The origin of the message, found by inspecting stack trace
    for (StackTraceElement elm : Thread.currentThread().getStackTrace()) {
      if (!elm.toString().startsWith(getClass().getName())
          && !elm.toString().startsWith(Notifications.class.getName())
          && !elm.toString().startsWith(Thread.class.getName())) {
        body.append(elm.toString() + "\n");
        break;
      }
    }

    // 3: The given message
    body.append(message + "\n");

    // 4: Optionally the exception
    if (e != null) {
      body.append(ExceptionUtils.getStackTrace(e));
    }

    try {
      // Send the mail
      EMailUtils.sendEmail(MAIL_RECEIVER, MAIL_SENDER, subject, body.toString());

      // Log as error

      log.error("Mailing {}{}", subjectPrefix, message, e);
    } catch (Exception e1) {
      // On trouble: Log and print it to system out, it's the best we can
      // do!

      String msg =
          "Could not send email on "
              + eventType.toString().toLowerCase()
              + " notification:\n"
              + body.toString()
              + "\n";
      System.err.println(msg);
      e1.printStackTrace(System.err);
      log.error(msg, e1);
    }
  }
Beispiel #3
0
  public static void handleException(RestRequest request, RestResponse response, Throwable ex) {

    Throwable rootCause = ExceptionUtils.getRootCause(ex);
    rootCause = rootCause == null ? ex : rootCause;

    logger.error("捕获到Rest异常:request={}", request, rootCause);
    RestError restError = new RestError();
    restError.setErrorCode(2);

    if (ex instanceof RestServiceException) {

      RestServiceException rse = (RestServiceException) ex;

      if (rse.getErrorCode() != 0) {

        restError.setErrorCode(rse.getErrorCode());
      }

      restError.setErrorInfo(rse.getMessage());

    } else {

      restError.setErrorInfo(RestApiConstants.DEFAULT_ERROR_INFO);

      if (request.isDebug()) {

        String stackTrace = ExceptionUtils.getStackTrace(rootCause);
        // 截取有用的部分
        stackTrace = StringUtils.substringBefore(stackTrace, RestApiConstants.STACK_TRACE_BEFORE);
        response.setDebugInfo(stackTrace);
      }

      // 统计响应结果
      recordToErrorCounter(request.getCmd());
    }

    response.setStatusCode(500);
    response.setError(restError);
    response.setResponseTime(new Date());
  }
Beispiel #4
0
 private String getTrace() {
   return ExceptionUtils.getStackTrace(this);
 }