Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit c235ac4

Browse files
author
Aleksi Salmela
committed
Print errors into System.err .
1 parent a8ee84d commit c235ac4

27 files changed

Lines changed: 114 additions & 85 deletions

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Application(CliContext context) {
6262
private boolean runCommand(String name, String[] args) {
6363
AbstractCommand command = CommandFactory.createCommand(name);
6464
if (command == null) {
65-
io.println("Command " + name + " doesn't exist.");
65+
io.errorln("Command " + name + " doesn't exist.");
6666
return false;
6767
}
6868

@@ -87,7 +87,7 @@ private String[] parseArgs(String[] args) {
8787
}
8888

8989
if (commandName.startsWith("-")) {
90-
io.println("Unrecognized option: " + commandName);
90+
io.errorln("Unrecognized option: " + commandName);
9191
return null;
9292
}
9393

@@ -153,7 +153,7 @@ private boolean versionCheck() {
153153
try {
154154
time = Long.parseLong(previousTimestamp);
155155
} catch (NumberFormatException ex) {
156-
io.println("The previous update date isn't number.");
156+
io.errorln("The previous update date isn't number.");
157157
logger.warn("The previous update date isn't number.", ex);
158158
return false;
159159
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static Boolean save(CourseInfo course, Path courseInfoFile) {
3131
Files.createDirectories(courseInfoFile.getParent());
3232
Files.write(courseInfoFile, json);
3333
} catch (IOException e) {
34+
//TODO print to user
3435
logger.error("Could not create course file", e);
3536
return false;
3637
}
@@ -47,6 +48,7 @@ public static CourseInfo load(Path courseInfoFile) {
4748
try {
4849
reader = Files.newBufferedReader(courseInfoFile, Charset.forName("UTF-8"));
4950
} catch (IOException e) {
51+
//TODO print to user
5052
logger.error("Course file located, but failed to read from it", e);
5153
return null;
5254
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/SettingsIo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ private static boolean requireConfigDirectory(Path configRoot) {
143143
try {
144144
Files.createDirectories(configRoot);
145145
} catch (Exception e) {
146+
//TODO print error to user
146147
logger.error("Could not create config directory", e);
147148
return false;
148149
}
@@ -156,6 +157,7 @@ private static AccountList getHolderFromJson(Path file) {
156157
try {
157158
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
158159
} catch (IOException e) {
160+
//TODO print error to user
159161
logger.error("Accounts file located, but failed to read from it", e);
160162
return null;
161163
}
@@ -168,6 +170,7 @@ private static boolean saveHolderToJson(AccountList holder, Path file) {
168170
try {
169171
Files.write(file, json);
170172
} catch (IOException e) {
173+
//TODO print error to user
171174
logger.error("Could not write account to accounts file", e);
172175
return false;
173176
}
@@ -180,6 +183,7 @@ private static HashMap<String, String> getPropertiesFromJson(Path file) {
180183
try {
181184
reader = Files.newBufferedReader(file, Charset.forName("UTF-8"));
182185
} catch (IOException e) {
186+
//TODO print error to user
183187
logger.error("Properties file located, but failed to read from it", e);
184188
return null;
185189
}
@@ -194,6 +198,7 @@ private static boolean savePropertiesToJson(HashMap<String, String> properties,
194198
try {
195199
Files.write(file, json);
196200
} catch (IOException e) {
201+
//TODO print error to user
197202
logger.error("Could not write properties to file", e);
198203
return false;
199204
}

src/main/java/fi/helsinki/cs/tmc/cli/backend/TmcUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,27 +218,27 @@ protected static void handleTmcExceptions(CliContext ctx, Exception exception) {
218218
Throwable cause = exception.getCause();
219219

220220
if (isAuthenticationError(exception)) {
221-
io.println("Your username or password is not valid anymore.");
221+
io.errorln("Your username or password is not valid anymore.");
222222
return;
223223
}
224224
if (cause instanceof FailedHttpResponseException) {
225225
logger.error("Unable to connect to server", exception);
226-
io.println("Unable to connect to server.");
226+
io.errorln("Unable to connect to server.");
227227
return;
228228
}
229229
if (cause instanceof ObsoleteClientException) {
230230
logger.error("Outdated tmc client");
231-
io.println("Your tmc-cli is outdated. Please update it.");
231+
io.errorln("Your tmc-cli is outdated. Please update it.");
232232
ctx.getApp().runAutoUpdate();
233233
return;
234234
}
235235
if (cause != null && cause.getCause() instanceof UnknownHostException) {
236236
logger.error("No internet connection");
237-
io.println("You have no internet connection.");
237+
io.errorln("You have no internet connection.");
238238
return;
239239
}
240240
logger.error("Command failed in tmc-core", exception);
241-
io.println("Command failed in tmc-core, check tmc-cli.log file for more info");
241+
io.errorln("Command failed in tmc-core, check tmc-cli.log file for more info");
242242
}
243243

244244
private static boolean isAuthenticationError(Exception exception) {

src/main/java/fi/helsinki/cs/tmc/cli/command/DocumentCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ public void run(CliContext context, CommandLine args) {
9292
try {
9393
this.height = Integer.parseInt(args.getOptionValue('h'));
9494
} catch (NumberFormatException e) {
95-
io.println("Height must be integer");
95+
io.errorln("Height must be integer");
9696
return;
9797
}
9898
}
9999
if (args.hasOption('s')) {
100100
try {
101101
this.speed = Integer.parseInt(args.getOptionValue('s'));
102102
} catch (NumberFormatException e) {
103-
io.println("Speed must be integer");
103+
io.errorln("Speed must be integer");
104104
return;
105105
}
106106
}
107107
if (EnvironmentUtil.isWindows()) {
108-
io.println("Command document doesn't exist. ;)");
108+
io.errorln("Command document doesn't exist. ;)");
109109
return;
110110
}
111111

@@ -117,7 +117,7 @@ public void run(CliContext context, CommandLine args) {
117117
if (cursorY == height / 2) {
118118
io.println("Just kidding :)");
119119
}
120-
io.println("");
120+
io.println();
121121
wait(50);
122122
}
123123
fadeOut();
@@ -233,7 +233,7 @@ private String loadFile(String filename) {
233233
try {
234234
return IOUtils.toString(stream, StandardCharsets.UTF_8);
235235
} catch (IOException e) {
236-
io.println("Encoding failure... REALLY???");
236+
io.errorln("Encoding failure... REALLY???");
237237
return null;
238238
}
239239
}

src/main/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void run(CliContext context, CommandLine args) {
4343

4444
String[] stringArgs = args.getArgs();
4545
if (stringArgs.length == 0 || stringArgs.length > 1) {
46-
io.println("You must give a course name as an argument.");
47-
io.println("Usage: tmc download COURSE");
46+
io.errorln("You must give a course name as an argument.");
47+
io.errorln("Usage: tmc download COURSE");
4848
return;
4949
}
5050

@@ -57,7 +57,7 @@ public void run(CliContext context, CommandLine args) {
5757

5858
WorkDir workDir = ctx.getWorkDir();
5959
if (workDir.getConfigFile() != null) {
60-
io.println("Can't download a course inside a course directory.");
60+
io.errorln("Can't download a course inside a course directory.");
6161
return;
6262
}
6363

@@ -78,7 +78,7 @@ public void run(CliContext context, CommandLine args) {
7878
ctx.useAccount(finder.getAccount());
7979
List<Exercise> exercises = TmcUtil.downloadExercises(ctx, filtered, progobs);
8080
if (exercises == null) {
81-
io.println("Failed to download exercises");
81+
io.errorln("Failed to download exercises");
8282
return;
8383
}
8484

src/main/java/fi/helsinki/cs/tmc/cli/command/InfoCommand.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void run(CliContext context, CommandLine args) {
5454

5555
if (fetchFromInternet) {
5656
if (useWorkingDirectory) {
57-
io.println("You must give a course as an argument.");
57+
io.errorln("You must give a course as an argument.");
5858
return;
5959
}
6060
String courseName = stringArgs[0];
@@ -66,7 +66,7 @@ public void run(CliContext context, CommandLine args) {
6666

6767
private void printInfoFromInternet(String courseName) {
6868
if (!ctx.hasLogin()) {
69-
io.println("Loading a course from a server requires login.");
69+
io.errorln("Loading a course from a server requires login.");
7070
return;
7171
}
7272

@@ -80,7 +80,7 @@ private void printInfoFromInternet(String courseName) {
8080
private void printLocalInfo(String[] stringArgs) {
8181
info = ctx.getCourseInfo();
8282
if (info == null) {
83-
io.println("You have to be in a course directory"
83+
io.errorln("You have to be in a course directory"
8484
+ " or use the -i option with the course name "
8585
+ "to get the information from the server.");
8686
return;
@@ -96,7 +96,7 @@ private void printLocalInfo(String[] stringArgs) {
9696
}
9797
} else {
9898
if (stringArgs.length != 1) {
99-
io.println("You can only give one path for this command.");
99+
io.errorln("You can only give one path for this command.");
100100
return;
101101
}
102102
String path = stringArgs[0];
@@ -117,8 +117,8 @@ private void printInfoFromParameters(String pathName) {
117117
if (course != null) {
118118
printCourse(course);
119119
} else {
120-
io.println("Not a course directory. ");
121-
io.println("Use the -i option to get course from "
120+
io.errorln("Not a course directory. ");
121+
io.errorln("Use the -i option to get course from "
122122
+ "server.");
123123
}
124124
}
@@ -243,6 +243,6 @@ private void printExerciseFull(Exercise exercise) {
243243
io.println(" Download URL: " + exercise.getDownloadUrl());
244244
io.println(" Solution download URL: " + exercise.getSolutionDownloadUrl());
245245
io.println(" Checksum: " + exercise.getChecksum());
246-
io.println("");
246+
io.println();
247247
}
248248
}

src/main/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public void run(CliContext context, CommandLine args) {
4141
}
4242

4343
if (!TmcUtil.hasConnection(ctx)) {
44-
io.println("You don't have internet connection currently.");
45-
io.println("Check the tmc-cli logs to get exact problem.");
44+
io.errorln("You don't have internet connection currently.");
45+
io.errorln("Check the tmc-cli logs if you disagree.");
4646
return;
4747
}
4848

4949
AccountList accountsList = SettingsIo.loadAccountList();
5050
boolean isFirst = true;
5151

5252
if (accountsList.getAccountCount() == 0) {
53-
io.println("You haven't logged in on any tmc server.");
53+
io.errorln("You haven't logged in on any tmc server.");
5454
return;
5555
}
5656

5757
for (Account settings : accountsList) {
5858
if (!isFirst) {
59-
io.println("");
59+
io.println();
6060
}
6161
if (accountsList.getAccountCount() > 1) {
6262
io.println(ColorUtil.colorString("Server " + settings.getServerAddress(),
@@ -71,7 +71,7 @@ private void printCourseList(Account account) {
7171
ctx.useAccount(account);
7272
List<Course> courses = TmcUtil.listCourses(ctx);
7373
if (courses.isEmpty()) {
74-
io.println("No courses found from the server.");
74+
io.errorln("No courses found from the server.");
7575
return;
7676
}
7777
for (Course course : courses) {

src/main/java/fi/helsinki/cs/tmc/cli/command/ListExercisesCommand.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private List<Exercise> getExercises(CommandLine args, String courseName) {
6969
private String getCourseNameFromCurrentDirectory() {
7070
CourseInfo info = getCourseInfoFromCurrentDirectory();
7171
if (info == null) {
72-
this.io.println("No course specified. Either run the command in a course"
72+
this.io.errorln("No course specified. Either run the command in a course"
7373
+ " directory or enter the course as a parameter.");
7474
return null;
7575
}
@@ -89,13 +89,13 @@ private List<Exercise> getExercisesFromServer(String courseName) {
8989

9090
Course course = TmcUtil.findCourse(ctx, courseName);
9191
if (course == null) {
92-
this.io.println("Course '" + courseName + "' doesn't exist on the server.");
92+
this.io.errorln("Course '" + courseName + "' doesn't exist on the server.");
9393
return null;
9494
}
9595

9696
List<Exercise> exercises = course.getExercises();
9797
if (exercises == null || exercises.isEmpty()) {
98-
this.io.println("Course '" + courseName + "' doesn't have any exercises.");
98+
this.io.errorln("Course '" + courseName + "' doesn't have any exercises.");
9999
return null;
100100
}
101101
return exercises;
@@ -104,14 +104,14 @@ private List<Exercise> getExercisesFromServer(String courseName) {
104104
private List<Exercise> getLocalExercises(String courseName) {
105105
CourseInfo info = getCourseInfoFromCurrentDirectory();
106106
if (info == null || !info.getCourseName().equals(courseName)) {
107-
this.io.println("You have to be in a course directory or use the -i option "
107+
this.io.errorln("You have to be in a course directory or use the -i option "
108108
+ "to get the exercises from the server.");
109109
return null;
110110
}
111111

112112
List<Exercise> exercises = info.getExercises();
113113
if (exercises == null || exercises.isEmpty()) {
114-
this.io.println("Course '" + courseName + "' doesn't have any exercises.");
114+
this.io.errorln("Course '" + courseName + "' doesn't have any exercises.");
115115
return null;
116116
}
117117
return exercises;

src/main/java/fi/helsinki/cs/tmc/cli/command/LoginCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public void run(CliContext context, CommandLine args) {
4040
}
4141

4242
if (!TmcUtil.hasConnection(ctx)) {
43-
io.println("You don't have internet connection currently.");
44-
io.println("Check the tmc-cli logs to get exact problem.");
43+
io.errorln("You don't have internet connection currently.");
44+
io.errorln("Check the tmc-cli logs if you disagree.");
4545
return;
4646
}
4747

@@ -63,7 +63,7 @@ public void run(CliContext context, CommandLine args) {
6363
AccountList list = SettingsIo.loadAccountList();
6464
list.addAccount(account);
6565
if (!SettingsIo.saveAccountList(list)) {
66-
io.println("Failed to write the accounts file.");
66+
io.errorln("Failed to write the accounts file.");
6767
return;
6868
}
6969

0 commit comments

Comments
 (0)