Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FakeError.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.addons.debug.commands;
  18. import com.dmdirc.addons.debug.Debug;
  19. import com.dmdirc.addons.debug.DebugCommand;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.commands.IntelligentCommand;
  22. import com.dmdirc.commandparser.commands.context.CommandContext;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import com.dmdirc.ui.input.AdditionalTabTargets;
  25. import javax.annotation.Nonnull;
  26. import javax.inject.Inject;
  27. import javax.inject.Provider;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.slf4j.Marker;
  31. import static com.dmdirc.util.LogUtils.APP_ERROR;
  32. import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
  33. import static com.dmdirc.util.LogUtils.FATAL_USER_ERROR;
  34. import static com.dmdirc.util.LogUtils.USER_ERROR;
  35. /**
  36. * Creates DMDirc errors with the specified parameters.
  37. */
  38. public class FakeError extends DebugCommand implements IntelligentCommand {
  39. private static final Logger LOG = LoggerFactory.getLogger(FakeError.class);
  40. @Inject
  41. public FakeError(final Provider<Debug> commandProvider) {
  42. super(commandProvider);
  43. }
  44. @Override
  45. public String getName() {
  46. return "error";
  47. }
  48. @Override
  49. public String getUsage() {
  50. return "<user|app> [<low|medium|high|fatal|unknown>] - Creates an error"
  51. + " with the specified parameters, defaults to high priority.";
  52. }
  53. @Override
  54. public void execute(@Nonnull final WindowModel origin,
  55. final CommandArguments args, final CommandContext context) {
  56. if ((args.getArguments().length == 1
  57. || args.getArguments().length == 2)
  58. && "user".equals(args.getArguments()[0])) {
  59. raiseError(getLevel(args.getArguments()), false);
  60. } else if ((args.getArguments().length == 1
  61. || args.getArguments().length == 2)
  62. && "app".equals(args.getArguments()[0])) {
  63. raiseError(getLevel(args.getArguments()), true);
  64. } else {
  65. showUsage(origin, args.isSilent(), getName(), getUsage());
  66. }
  67. }
  68. private void raiseError(final String level, final boolean appError) {
  69. final Marker marker = appError ? APP_ERROR : USER_ERROR;
  70. switch (level.toUpperCase()) {
  71. case "FATAL":
  72. LOG.error(appError ? FATAL_APP_ERROR : FATAL_USER_ERROR, "Debug error message",
  73. new IllegalArgumentException());
  74. break;
  75. case "HIGH":
  76. LOG.error(marker, "Debug error message", new IllegalArgumentException());
  77. break;
  78. case "MEDIUM":
  79. LOG.warn(marker, "Debug error message", new IllegalArgumentException());
  80. break;
  81. case "INFO":
  82. LOG.info(marker, "Debug error message", new IllegalArgumentException());
  83. break;
  84. default:
  85. LOG.info(marker, "Debug error message", new IllegalArgumentException());
  86. }
  87. }
  88. /**
  89. * Returns the error level specified by the provided arguments.
  90. *
  91. * @param args command arguments
  92. *
  93. * @return Error level
  94. */
  95. private String getLevel(final String... args) {
  96. if (args.length >= 2) {
  97. return args[1].toUpperCase();
  98. } else {
  99. return "HIGH";
  100. }
  101. }
  102. @Override
  103. public AdditionalTabTargets getSuggestions(final int arg,
  104. final IntelligentCommandContext context) {
  105. final AdditionalTabTargets res = new AdditionalTabTargets();
  106. res.excludeAll();
  107. if (arg == 1) {
  108. res.add("user");
  109. res.add("app");
  110. } else if (arg == 2) {
  111. res.add("low");
  112. res.add("medium");
  113. res.add("high");
  114. res.add("fatal");
  115. }
  116. return res;
  117. }
  118. }