Skip to content

Commit aa2ee4c

Browse files
committed
Improved exception handling
1 parent 4470008 commit aa2ee4c

6 files changed

Lines changed: 62 additions & 12 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ plugins {
2727
}
2828

2929
group = 'hu.zza'
30-
project.version = '0.3.0'
30+
project.version = '0.3.1'
3131
sourceCompatibility = '11'
3232

3333
repositories {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* clim // Command Line Interface Menu
4+
* // https://git.zza.hu/clim
5+
*
6+
* Copyright (C) 2020-2021 Szabó László András // hu-zza
7+
*
8+
* This file is part of clim.
9+
*
10+
* clim is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* clim is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
24+
package hu.zza.clim;
25+
26+
public class ClimException extends RuntimeException {
27+
28+
public ClimException() {
29+
super();
30+
}
31+
32+
public ClimException(String message) {
33+
super(message);
34+
}
35+
36+
public ClimException(String message, Throwable cause) {
37+
super(message, cause);
38+
}
39+
40+
public ClimException(Throwable cause) {
41+
super(cause);
42+
}
43+
}

src/main/java/hu/zza/clim/Menu.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private void tryToChooseAnOption(String input) {
155155
inputProcessorService.processInputRelatedToOptions(input, options));
156156
} catch (Exception e) {
157157
positionHistory.pollFirst();
158-
warnAboutInput(input, e);
158+
throw new ClimException(PROCESSING_FAILED.getMessage(input), e);
159159
}
160160
}
161161

@@ -164,14 +164,6 @@ private void chooseOptionByProcessedInput(ProcessedInput processedInput) {
164164
position = menuStructure.get(selected).select(processedInput);
165165
}
166166

167-
private static void warnAboutInput(String input, Exception e) {
168-
if (e.getMessage() != null) {
169-
System.err.print(PROCESSING_FAILED.getMessage(input, e.getMessage()));
170-
} else {
171-
System.err.print(PROCESSING_FAILED.getMessage(input, ""));
172-
e.printStackTrace();
173-
}
174-
}
175167

176168
private Position returnValidatedPositionOrThrow(Position position) {
177169
refreshOptions();

src/main/java/hu/zza/clim/MenuStructureBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
package hu.zza.clim;
2525

26+
import static hu.zza.clim.menu.Message.INITIALIZATION_FAILED;
27+
2628
import com.google.gson.JsonArray;
2729
import com.google.gson.JsonElement;
2830
import com.google.gson.JsonObject;
@@ -100,6 +102,14 @@ private void clearBuilt() {
100102
}
101103

102104
public MenuStructure build() {
105+
try {
106+
return buildMenuStructure();
107+
} catch (Exception e) {
108+
throw new IllegalArgumentException(INITIALIZATION_FAILED.getMessage(e.getMessage()), e);
109+
}
110+
}
111+
112+
private MenuStructure buildMenuStructure() {
103113
clearBuilt();
104114
findAllNodesAndLeaves();
105115
checkBeforeBuild();

src/main/resources/MessageTemplate.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ MENU_OPTION_DECORATOR=%s
2828
MENU_ORDINAL_OPTION_DECORATOR=%d. %s
2929
MENU_HISTORY_SEPARATOR=>
3030

31-
PROCESSING_FAILED=The menu can not process the given input: '%s'%nCause: %s%n
31+
PROCESSING_FAILED=The menu can not process the given input: '%s'%n
3232
INITIALIZATION_FAILED=Object initialization fails because of parameter invalidity:%n%s%n
3333

3434
STOP_BECAUSE_NULL=Execution stopped because of the value of the '%s': It cannot be null.

src/test/java/MenuBuilderTestInteractive.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
2323

24+
import hu.zza.clim.ClimException;
2425
import hu.zza.clim.HeaderStyle;
2526
import hu.zza.clim.Menu;
2627
import hu.zza.clim.MenuBuilder;
@@ -115,7 +116,11 @@ public static void main(String[] args) throws Exception {
115116
while (waitingForUserInput) {
116117
menu.listOptions();
117118
if (scanner.hasNext()) {
118-
menu.chooseOption(scanner.nextLine());
119+
try {
120+
menu.chooseOption(scanner.nextLine());
121+
} catch (ClimException e) {
122+
System.err.println(e.getMessage());
123+
}
119124
} else {
120125
waitingForUserInput = false;
121126
}

0 commit comments

Comments
 (0)