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.2KB

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