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.

ParserError.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2013 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.parser.common;
  23. /**
  24. * IRC Parser Error.
  25. *
  26. * @author Shane Mc Cormack
  27. */
  28. public final class ParserError {
  29. /** Error is potentially Fatal, Desync 99% Guaranteed! */
  30. public static final int ERROR_FATAL = 1;
  31. /** Error is not fatal, but is more severe than a warning. */
  32. public static final int ERROR_ERROR = 2;
  33. /** Error was unexpected, but shouldn't be anything to worry about. */
  34. public static final int ERROR_WARNING = 4;
  35. /** Error is a user-error rather than a server error. */
  36. public static final int ERROR_USER = 8;
  37. /** Error was an exception from elsewhere. */
  38. public static final int ERROR_EXCEPTION = 16;
  39. /** Store the Error level. */
  40. private int errorLevel;
  41. /** Store the Error Information. */
  42. private String errorData;
  43. /** Store the Exception object. */
  44. private Exception exceptionInfo;
  45. /** Last line of server input before this exception was triggered. */
  46. private final String lastLine;
  47. /**
  48. * Create a new Error.
  49. *
  50. * @param level Set the error level.
  51. * @param data String containing information about the error.
  52. * @param line The last line of data recieved from the server before this
  53. * exception.
  54. */
  55. public ParserError(final int level, final String data, final String line) {
  56. errorData = data;
  57. errorLevel = level;
  58. lastLine = line;
  59. }
  60. /**
  61. * Check if this error is considered Fatal.
  62. *
  63. * @return Returns true for a fatal error, false for a non-fatal error
  64. */
  65. public boolean isFatal() {
  66. return (errorLevel & ERROR_FATAL) == ERROR_FATAL;
  67. }
  68. /**
  69. * Check if this error is considered an error (less severe than fatal, worse
  70. * than warning).
  71. *
  72. * @return Returns true for an "Error" level error, else false.
  73. */
  74. public boolean isError() {
  75. return (errorLevel & ERROR_ERROR) == ERROR_ERROR;
  76. }
  77. /**
  78. * Check if this error is considered a warning.
  79. *
  80. * @return Returns true for a warning, else false.
  81. */
  82. public boolean isWarning() {
  83. return (errorLevel & ERROR_WARNING) == ERROR_WARNING;
  84. }
  85. /**
  86. * Check if this error is considered a user-error rather than a server error.
  87. * For DMDirc this will cause the error not to be reported to the developers
  88. *
  89. * @return Returns true for a user error, else false.
  90. */
  91. public boolean isUserError() {
  92. return (errorLevel & ERROR_USER) == ERROR_USER;
  93. }
  94. /**
  95. * Check if this error was generated from an exception.
  96. *
  97. * @return Returns true if getException will return an exception.
  98. */
  99. public boolean isException() {
  100. return (errorLevel & ERROR_EXCEPTION) == ERROR_EXCEPTION;
  101. }
  102. /**
  103. * Check if this error has a lastLine parameter.
  104. *
  105. * @return Returns true if getLastLine returns anything non null, non empty.
  106. */
  107. public boolean hasLastLine() {
  108. return lastLine != null && !lastLine.isEmpty();
  109. }
  110. /**
  111. * Set the Exception object.
  112. *
  113. * @param newException The exception object to store
  114. */
  115. public void setException(final Exception newException) {
  116. exceptionInfo = newException;
  117. if (!isException()) {
  118. errorLevel += ERROR_EXCEPTION;
  119. }
  120. }
  121. /**
  122. * Get the Exception object.
  123. *
  124. * @return Returns the exception object
  125. */
  126. public Exception getException() {
  127. return exceptionInfo;
  128. }
  129. /**
  130. * Get the full ErrorLevel.
  131. *
  132. * @return Returns the error level
  133. */
  134. public int getLevel() {
  135. return errorLevel;
  136. }
  137. /**
  138. * Get the Error information.
  139. *
  140. * @return Returns the error data
  141. */
  142. public String getData() {
  143. return errorData;
  144. }
  145. /**
  146. * Add to the error information.
  147. *
  148. * @param data Information to add to the end of the existing Data
  149. */
  150. public void appendData(final String data) {
  151. errorData += '[' + data + ']';
  152. }
  153. /**
  154. * Get the last line recieved from the server before this exception.
  155. *
  156. * @return Returns the error data
  157. */
  158. public String getLastLine() {
  159. return lastLine;
  160. }
  161. }