Skip to content

Commit 8ab419f

Browse files
committed
Better error handling for missing preferences.
Fixes #1471
1 parent c24b3f6 commit 8ab419f

File tree

6 files changed

+57
-29
lines changed

6 files changed

+57
-29
lines changed

app/src/processing/app/Editor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package processing.app;
2424

2525
import processing.app.debug.*;
26+
import processing.app.helpers.PreferencesMapException;
2627
import processing.app.syntax.*;
2728
import processing.app.tools.*;
2829
import processing.core.*;
@@ -2543,9 +2544,10 @@ public void run() {
25432544
statusError(_("Error while burning bootloader."));
25442545
// error message will already be visible
25452546
}
2546-
} catch (RunnerException e) {
2547-
statusError(_("Error while burning bootloader."));
2548-
e.printStackTrace();
2547+
} catch (PreferencesMapException e) {
2548+
statusError(I18n.format(
2549+
_("Error while burning bootloader: missing '{0}' configuration parameter"),
2550+
e.getMessage()));
25492551
//statusError(e);
25502552
} catch (Exception e) {
25512553
statusError(_("Error while burning bootloader."));

app/src/processing/app/Sketch.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ protected boolean exportApplet(boolean usingProgrammer) throws Exception {
15811581
* Handle export to applet.
15821582
*/
15831583
public boolean exportApplet(String appletPath, boolean usingProgrammer)
1584-
throws RunnerException, IOException, SerialException {
1584+
throws Exception {
15851585

15861586
prepare();
15871587

@@ -1660,7 +1660,7 @@ protected void size(PreferencesMap prefs) throws RunnerException {
16601660
}
16611661

16621662
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer)
1663-
throws RunnerException, SerialException {
1663+
throws Exception {
16641664

16651665
Uploader uploader;
16661666

app/src/processing/app/debug/BasicUploader.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import processing.app.Serial;
3838
import processing.app.SerialException;
3939
import processing.app.helpers.PreferencesMap;
40+
import processing.app.helpers.PreferencesMapException;
4041
import processing.app.helpers.StringReplacer;
4142

4243
import static processing.app.I18n._;
@@ -45,7 +46,7 @@ public class BasicUploader extends Uploader {
4546

4647
public boolean uploadUsingPreferences(String buildPath, String className,
4748
boolean usingProgrammer)
48-
throws RunnerException, SerialException {
49+
throws Exception {
4950
// FIXME: Preferences should be reorganized
5051
TargetPlatform targetPlatform = Base.getTargetPlatform();
5152
PreferencesMap prefs = Preferences.getMap();
@@ -71,7 +72,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
7172
boolean waitForUploadPort = (t != null) && t.equals("true");
7273

7374
if (doTouch) {
74-
String uploadPort = prefs.get("serial.port");
75+
String uploadPort = prefs.getOrExcept("serial.port");
7576
try {
7677
// Toggle 1200 bps on selected serial port to force board reset.
7778
List<String> before = Serial.list();
@@ -109,9 +110,9 @@ public boolean uploadUsingPreferences(String buildPath, String className,
109110
prefs.put("build.path", buildPath);
110111
prefs.put("build.project_name", className);
111112
if (verbose)
112-
prefs.put("upload.verbose", prefs.get("upload.params.verbose"));
113+
prefs.put("upload.verbose", prefs.getOrExcept("upload.params.verbose"));
113114
else
114-
prefs.put("upload.verbose", prefs.get("upload.params.quiet"));
115+
prefs.put("upload.verbose", prefs.getOrExcept("upload.params.quiet"));
115116

116117
boolean uploadResult;
117118
try {
@@ -120,7 +121,7 @@ public boolean uploadUsingPreferences(String buildPath, String className,
120121
// flushSerialBuffer();
121122
// }
122123

123-
String pattern = prefs.get("upload.pattern");
124+
String pattern = prefs.getOrExcept("upload.pattern");
124125
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
125126
uploadResult = executeUploadCommand(cmd);
126127
} catch (Exception e) {
@@ -211,7 +212,7 @@ private String waitForUploadPort(String uploadPort, List<String> before)
211212
}
212213

213214
public boolean uploadUsingProgrammer(String buildPath, String className)
214-
throws RunnerException {
215+
throws Exception {
215216

216217
TargetPlatform targetPlatform = Base.getTargetPlatform();
217218
String programmer = Preferences.get("programmer");
@@ -224,15 +225,15 @@ public boolean uploadUsingProgrammer(String buildPath, String className)
224225
PreferencesMap prefs = Preferences.getMap();
225226
prefs.putAll(Base.getBoardPreferences());
226227
prefs.putAll(targetPlatform.getProgrammer(programmer));
227-
prefs.putAll(targetPlatform.getTool(prefs.get("program.tool")));
228+
prefs.putAll(targetPlatform.getTool(prefs.getOrExcept("program.tool")));
228229

229230
prefs.put("build.path", buildPath);
230231
prefs.put("build.project_name", className);
231232

232233
if (verbose)
233-
prefs.put("program.verbose", prefs.get("program.params.verbose"));
234+
prefs.put("program.verbose", prefs.getOrExcept("program.params.verbose"));
234235
else
235-
prefs.put("program.verbose", prefs.get("program.params.quiet"));
236+
prefs.put("program.verbose", prefs.getOrExcept("program.params.quiet"));
236237

237238
try {
238239
// if (prefs.get("program.disable_flushing") == null
@@ -241,15 +242,15 @@ public boolean uploadUsingProgrammer(String buildPath, String className)
241242
// flushSerialBuffer();
242243
// }
243244

244-
String pattern = prefs.get("program.pattern");
245+
String pattern = prefs.getOrExcept("program.pattern");
245246
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
246247
return executeUploadCommand(cmd);
247248
} catch (Exception e) {
248249
throw new RunnerException(e);
249250
}
250251
}
251252

252-
public boolean burnBootloader() throws RunnerException {
253+
public boolean burnBootloader() throws RunnerException, PreferencesMapException {
253254
TargetPlatform targetPlatform = Base.getTargetPlatform();
254255

255256
// Find preferences for the selected programmer
@@ -272,7 +273,7 @@ public boolean burnBootloader() throws RunnerException {
272273

273274
// Create configuration for bootloader tool
274275
PreferencesMap toolPrefs = new PreferencesMap();
275-
String tool = prefs.get("bootloader.tool");
276+
String tool = prefs.getOrExcept("bootloader.tool");
276277
if (tool.contains(":")) {
277278
String[] split = tool.split(":", 2);
278279
TargetPlatform platform = Base.getCurrentTargetPlatformFromPackage(split[0]);
@@ -291,20 +292,20 @@ public boolean burnBootloader() throws RunnerException {
291292
// Merge tool with global configuration
292293
prefs.putAll(toolPrefs);
293294
if (verbose) {
294-
prefs.put("erase.verbose", prefs.get("erase.params.verbose"));
295-
prefs.put("bootloader.verbose", prefs.get("bootloader.params.verbose"));
295+
prefs.put("erase.verbose", prefs.getOrExcept("erase.params.verbose"));
296+
prefs.put("bootloader.verbose", prefs.getOrExcept("bootloader.params.verbose"));
296297
} else {
297-
prefs.put("erase.verbose", prefs.get("erase.params.quiet"));
298-
prefs.put("bootloader.verbose", prefs.get("bootloader.params.quiet"));
298+
prefs.put("erase.verbose", prefs.getOrExcept("erase.params.quiet"));
299+
prefs.put("bootloader.verbose", prefs.getOrExcept("bootloader.params.quiet"));
299300
}
300301

301302
try {
302-
String pattern = prefs.get("erase.pattern");
303+
String pattern = prefs.getOrExcept("erase.pattern");
303304
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
304305
if (!executeUploadCommand(cmd))
305306
return false;
306307

307-
pattern = prefs.get("bootloader.pattern");
308+
pattern = prefs.getOrExcept("bootloader.pattern");
308309
cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
309310
return executeUploadCommand(cmd);
310311
} catch (Exception e) {

app/src/processing/app/debug/Uploader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import processing.app.I18n;
3636
import processing.app.Preferences;
3737
import processing.app.Serial;
38-
import processing.app.SerialException;
3938
import processing.app.SerialNotFoundException;
4039

4140
public abstract class Uploader implements MessageConsumer {
@@ -52,11 +51,11 @@ public abstract class Uploader implements MessageConsumer {
5251
boolean verbose;
5352

5453
public abstract boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer)
55-
throws RunnerException, SerialException;
54+
throws Exception;
5655

57-
public abstract boolean burnBootloader() throws RunnerException;
56+
public abstract boolean burnBootloader() throws Exception;
5857

59-
protected void flushSerialBuffer() throws RunnerException, SerialException {
58+
protected void flushSerialBuffer() throws Exception {
6059
// Cleanup the serial buffer
6160
try {
6261
Serial serialPort = new Serial();
@@ -87,14 +86,14 @@ protected void flushSerialBuffer() throws RunnerException, SerialException {
8786
}
8887

8988
protected boolean executeUploadCommand(Collection<String> commandDownloader)
90-
throws RunnerException {
89+
throws Exception {
9190
String[] commandArray = new String[commandDownloader.size()];
9291
commandDownloader.toArray(commandArray);
9392
return executeUploadCommand(commandArray);
9493
}
9594

9695
protected boolean executeUploadCommand(String commandArray[])
97-
throws RunnerException
96+
throws Exception
9897
{
9998
firstErrorFound = false; // haven't found any errors yet
10099
secondErrorFound = false;

app/src/processing/app/helpers/PreferencesMap.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,22 @@ public String toString(String indent) {
245245
return res;
246246
}
247247

248+
/**
249+
* Returns the value to which the specified key is mapped, or throws a
250+
* PreferencesMapException if not found
251+
*
252+
* @param k
253+
* the key whose associated value is to be returned
254+
* @return the value to which the specified key is mapped
255+
* @throws PreferencesMapException
256+
*/
257+
public String getOrExcept(String k) throws PreferencesMapException {
258+
String r = get(k);
259+
if (r == null)
260+
throw new PreferencesMapException(k);
261+
return r;
262+
}
263+
248264
@Override
249265
public String toString() {
250266
return toString("");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package processing.app.helpers;
2+
3+
@SuppressWarnings("serial")
4+
public class PreferencesMapException extends Exception {
5+
6+
public PreferencesMapException(String message) {
7+
super(message);
8+
}
9+
10+
}

0 commit comments

Comments
 (0)