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

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