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.

FakeError.java 4.7KB

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