• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 12833136576

17 Jan 2025 04:37PM UTC coverage: 68.489% (+0.3%) from 68.152%
12833136576

push

github

web-flow
prevent stacktrace of CliException even with debug logging (#933)

2723 of 4349 branches covered (62.61%)

Branch coverage included in aggregate %.

7062 of 9938 relevant lines covered (71.06%)

3.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.88
cli/src/main/java/com/devonfw/tools/ide/cli/Ideasy.java
1
package com.devonfw.tools.ide.cli;
2

3
import java.net.URLEncoder;
4
import java.nio.charset.StandardCharsets;
5

6
import com.devonfw.tools.ide.commandlet.ContextCommandlet;
7
import com.devonfw.tools.ide.context.AbstractIdeContext;
8
import com.devonfw.tools.ide.context.IdeContext;
9
import com.devonfw.tools.ide.context.IdeContextConsole;
10
import com.devonfw.tools.ide.context.IdeStartContextImpl;
11
import com.devonfw.tools.ide.log.IdeLogLevel;
12
import com.devonfw.tools.ide.property.FlagProperty;
13
import com.devonfw.tools.ide.property.Property;
14

15
/**
16
 * The main program of the CLI (command-line-interface).
17
 */
18
public final class Ideasy {
19

20
  private AbstractIdeContext context;
21

22
  /**
23
   * The default constructor.
24
   */
25
  public Ideasy() {
26
    super();
×
27
  }
×
28

29
  /**
30
   * The constructor.
31
   *
32
   * @param context the predefined {@link IdeContext} for testing.
33
   */
34
  Ideasy(AbstractIdeContext context) {
35

36
    super();
2✔
37
    this.context = context;
3✔
38
  }
1✔
39

40
  private IdeContext context() {
41

42
    if (this.context == null) {
3!
43
      // fallback in case of exception before initialization
44
      return new IdeContextConsole(IdeLogLevel.INFO, null, false);
×
45
    }
46
    return this.context;
3✔
47
  }
48

49
  /**
50
   * Non-static variant of {@link #main(String...) main method} without invoking {@link System#exit(int)} so it can be tested.
51
   *
52
   * @param args the command-line arguments.
53
   * @return the exit code.
54
   */
55
  public int run(String... args) {
56

57
    int exitStatus;
58
    try {
59
      exitStatus = runOrThrow(args);
×
60
    } catch (CliException error) {
1✔
61
      exitStatus = error.getExitCode();
3✔
62
      String errorMessage = error.getMessage();
3✔
63
      if ((errorMessage != null) && !errorMessage.isBlank()) {
5!
64
        context().error(errorMessage);
4✔
65
      }
66
    } catch (Throwable error) {
×
67
      exitStatus = 255;
×
68
      String title = error.getMessage();
×
69
      if (title == null) {
×
70
        title = error.getClass().getName();
×
71
      } else {
72
        title = error.getClass().getSimpleName() + ": " + title;
×
73
      }
74
      String message = "An unexpected error occurred!\n" //
×
75
          + "We are sorry for the inconvenience.\n" //
76
          + "Please check the error below, resolve it and try again.\n" //
77
          + "If the error is not on your end (network connectivity, lack of permissions, etc.) please file a bug:\n" //
78
          + "https://github.com/devonfw/IDEasy/issues/new?assignees=&labels=bug&projects=&template=bug.md&title="
79
          + URLEncoder.encode(title, StandardCharsets.UTF_8);
×
80
      context().error(error, message);
×
81
    }
1✔
82
    return exitStatus;
2✔
83
  }
84

85
  /**
86
   * Like {@link #run(String...)} but does not catch {@link Throwable}s so you can handle them yourself.
87
   *
88
   * @param args the command-line arguments.
89
   * @return the exit code.
90
   */
91
  public int runOrThrow(String... args) {
92

93
    CliArguments arguments = new CliArguments(args);
5✔
94
    initContext(arguments);
3✔
95
    return this.context.run(arguments);
×
96
  }
97

98
  private void initContext(CliArguments arguments) {
99

100
    ContextCommandlet contextCommandlet = new ContextCommandlet();
4✔
101
    while (arguments.hasNext()) {
3!
102
      CliArgument current = arguments.next();
3✔
103
      String key = current.getKey();
3✔
104
      Property<?> property = contextCommandlet.getOption(key);
4✔
105
      if (property == null) {
2✔
106
        break;
1✔
107
      }
108
      String value = current.getValue();
3✔
109
      if (value == null) {
2!
110
        if (property instanceof FlagProperty) {
3!
111
          ((FlagProperty) property).setValue(Boolean.TRUE);
5✔
112
        } else {
113
          System.err.println("Missing value for option " + key);
×
114
        }
115
      } else {
116
        property.setValueAsString(value, this.context);
×
117
      }
118
    }
1✔
119
    contextCommandlet.run();
2✔
120
    if (this.context == null) {
3!
121
      IdeStartContextImpl startContext = contextCommandlet.getStartContext();
×
122
      this.context = new IdeContextConsole(startContext);
×
123
    }
124
  }
1✔
125

126
  /**
127
   * The actual main method of the CLI program.
128
   *
129
   * @param args the command-line arguments.
130
   */
131
  public static void main(String... args) {
132

133
    int exitStatus = new Ideasy().run(args);
×
134
    System.exit(exitStatus);
×
135
  }
×
136

137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc