You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProgramError.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.logger;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.events.ProgramErrorStatusEvent;
  25. import com.google.common.base.MoreObjects;
  26. import com.google.common.base.Throwables;
  27. import java.io.Serializable;
  28. import java.util.Date;
  29. import java.util.Objects;
  30. import java.util.Optional;
  31. import javax.annotation.Nonnull;
  32. import javax.annotation.Nullable;
  33. import static com.google.common.base.Preconditions.checkNotNull;
  34. /**
  35. * Stores a program error.
  36. */
  37. public class ProgramError implements Serializable {
  38. /** A version number for this class. */
  39. private static final long serialVersionUID = 4;
  40. /** Error icon. */
  41. private final ErrorLevel level;
  42. /** Error message. */
  43. private final String message;
  44. /** Underlying exception. */
  45. private final Optional<Throwable> exception;
  46. /** Date/time error first occurred. */
  47. private final Date date;
  48. /** The eventbus to post status changes to. */
  49. private final Optional<DMDircMBassador> eventBus;
  50. /** Is this an application error? */
  51. private final boolean appError;
  52. /** Error report Status. */
  53. private ErrorReportStatus reportStatus;
  54. /** Has the error been output. */
  55. private boolean handled;
  56. /**
  57. * Creates a new instance of ProgramError.
  58. *
  59. * @param level Error level
  60. * @param message Error message
  61. * @param exception The exception that caused the error, if any.
  62. * @param date Error time and date
  63. * @param eventBus The event bus to post status changes to
  64. */
  65. public ProgramError(
  66. @Nonnull final ErrorLevel level,
  67. @Nonnull final String message,
  68. @Nullable final Throwable exception,
  69. @Nonnull final Date date,
  70. @Nullable final DMDircMBassador eventBus,
  71. final boolean appError) {
  72. checkNotNull(level);
  73. checkNotNull(message);
  74. checkNotNull(level);
  75. checkNotNull(date);
  76. this.level = level;
  77. this.message = message;
  78. this.exception = Optional.ofNullable(exception);
  79. this.date = (Date) date.clone();
  80. this.reportStatus = ErrorReportStatus.WAITING;
  81. this.eventBus = Optional.ofNullable(eventBus);
  82. this.appError = appError;
  83. }
  84. /**
  85. * Returns this errors level.
  86. *
  87. * @return Error level
  88. */
  89. public ErrorLevel getLevel() {
  90. return level;
  91. }
  92. /**
  93. * Returns this errors message.
  94. *
  95. * @return Error message
  96. */
  97. public String getMessage() {
  98. return message;
  99. }
  100. public Optional<Throwable> getThrowable() {
  101. return exception;
  102. }
  103. public Optional<String> getThrowableAsString() {
  104. return exception.map(Throwables::getStackTraceAsString);
  105. }
  106. /**
  107. * Returns this errors time.
  108. *
  109. * @return Error time
  110. */
  111. public Date getDate() {
  112. return (Date) date.clone();
  113. }
  114. /**
  115. * Returns the reportStatus of this error.
  116. *
  117. * @return Error reportStatus
  118. */
  119. public ErrorReportStatus getReportStatus() {
  120. return reportStatus;
  121. }
  122. /**
  123. * Sets the report Status of this error.
  124. *
  125. * @param newStatus new ErrorReportStatus for the error
  126. */
  127. public void setReportStatus(final ErrorReportStatus newStatus) {
  128. if (newStatus != null && reportStatus != newStatus) {
  129. reportStatus = newStatus;
  130. eventBus.ifPresent(e -> e.publishAsync(new ProgramErrorStatusEvent(this)));
  131. }
  132. }
  133. /**
  134. * Set this error as handled.
  135. */
  136. public void setHandled() {
  137. handled = true;
  138. }
  139. /**
  140. * Has this error been handled?
  141. *
  142. * @return Handled state
  143. */
  144. public boolean isHandled() {
  145. return handled;
  146. }
  147. /**
  148. * Is this an application error?
  149. *
  150. * @return true iif this is an application error
  151. */
  152. public boolean isAppError() {
  153. return appError;
  154. }
  155. @Override
  156. public String toString() {
  157. return MoreObjects.toStringHelper(this)
  158. .add("Level", getLevel())
  159. .add("Status", getReportStatus())
  160. .add("Message", getMessage())
  161. .toString();
  162. }
  163. @Override
  164. public boolean equals(final Object obj) {
  165. if (obj instanceof ProgramError) {
  166. final ProgramError other = (ProgramError) obj;
  167. return Objects.equals(getLevel(), other.getLevel()) &&
  168. Objects.equals(getMessage(), other.getMessage()) &&
  169. Objects.equals(getThrowable(), other.getThrowable());
  170. }
  171. return false;
  172. }
  173. @Override
  174. public int hashCode() {
  175. return Objects.hash(level, message, exception);
  176. }
  177. }