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

devonfw / IDEasy / 9904178931

12 Jul 2024 07:32AM UTC coverage: 61.387% (-0.5%) from 61.842%
9904178931

push

github

web-flow
#400: fix error handling for undefined IDE_ROOT and IDE_HOME (#476)

1997 of 3575 branches covered (55.86%)

Branch coverage included in aggregate %.

5297 of 8307 relevant lines covered (63.77%)

2.8 hits per line

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

0.0
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.log.IdeLogLevel;
11
import com.devonfw.tools.ide.property.FlagProperty;
12
import com.devonfw.tools.ide.property.Property;
13

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

19
  private AbstractIdeContext context;
20

21
  /**
22
   * The actual main method of the CLI program.
23
   *
24
   * @param args the command-line arguments.
25
   */
26
  public static void main(String... args) {
27

28
    int exitStatus = new Ideasy().run(args);
×
29
    System.exit(exitStatus);
×
30
  }
×
31

32
  private IdeContext context() {
33

34
    if (this.context == null) {
×
35
      // fallback in case of exception before initialization
36
      this.context = new IdeContextConsole(IdeLogLevel.INFO, null, false);
×
37
    }
38
    return this.context;
×
39
  }
40

41
  /**
42
   * Non-static variant of {@link #main(String...) main method} without invoking {@link System#exit(int)} so it can be
43
   * tested.
44
   *
45
   * @param args the command-line arguments.
46
   * @return the exit code.
47
   */
48
  public int run(String... args) {
49

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

79
  /**
80
   * Like {@link #run(String...)} but does not catch {@link Throwable}s so you can handle them yourself.
81
   *
82
   * @param args the command-line arguments.
83
   * @return the exit code.
84
   */
85
  public int runOrThrow(String... args) {
86

87
    CliArguments arguments = new CliArguments(args);
×
88
    this.context = initContext(arguments);
×
89
    return this.context.run(arguments);
×
90
  }
91

92
  private AbstractIdeContext initContext(CliArguments arguments) {
93

94
    ContextCommandlet contextCommandlet = new ContextCommandlet();
×
95
    while (arguments.hasNext()) {
×
96
      CliArgument current = arguments.next();
×
97
      String key = current.getKey();
×
98
      Property<?> property = contextCommandlet.getOption(key);
×
99
      if (property == null) {
×
100
        break;
×
101
      }
102
      String value = current.getValue();
×
103
      if (value == null) {
×
104
        if (property instanceof FlagProperty) {
×
105
          ((FlagProperty) property).setValue(Boolean.TRUE);
×
106
        } else {
107
          System.err.println("Missing value for option " + key);
×
108
        }
109
      } else {
110
        property.setValueAsString(value, this.context);
×
111
      }
112
    }
×
113
    contextCommandlet.run();
×
114
    return contextCommandlet.getIdeContext();
×
115
  }
116

117
}
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