Skip to content

Commit ca6ed80

Browse files
Route _checkWritable and connect-time read-only popup through the writeProtected dialog
The previous PR handled HTTP 409/500 on PUT /fs/* by tagging the ProtocolError with .writeProtected and showing an actionable dialog in saveFileContents. But there are two earlier paths users hit FIRST that were still using the old generic wording: 1. At connect, checkReadOnly() in script.js queries /fs/ for the writable flag. If false, it popped a one-line warning that said 'Disable the USB drive' without telling users how, what 'eject may not be enough' means, or where to read more. 2. On save, writeFile() calls _checkWritable() FIRST. If the cached _writable flag is false, _checkWritable threw a plain Error (not a ProtocolError, no .writeProtected tag), so the catch block in saveFileContents never recognized it as the write-protected case and fell through to 'Saving file failed after multiple attempts.' This commit: - Factors the writeProtected ProtocolError shape into a new _writeProtectedError() helper on FileTransferClient, so both _checkWritable and the _fetch 409/500 handler return identical tagged errors (same .hint, .helpUrl, .helpLabel). - _checkWritable now invalidates the cached _writable flag before re-checking. Means users who release the drive can save again without disconnecting/reconnecting the workflow. - Updates checkReadOnly() in script.js to use the same wording + clickable Learn-guide link as the per-save dialog. Connect-time popup is now genuinely informative instead of cryptic. - Removes the debug console.warn lines from the previous commit now that we've confirmed the path (the read-only branch was bypassing the tagged ProtocolError, not a bug in _fetch). After this change, users see the same actionable message at all three trip points (connect, pre-save check, server-side response). The message names USB MSC, points at boot.py, warns that eject alone is unreliable on macOS, and links to the Learn guide section that walks them through disabling it.
1 parent 5d9b159 commit ca6ed80

2 files changed

Lines changed: 48 additions & 28 deletions

File tree

‎js/common/web-file-transfer.js‎

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,36 @@ class FileTransferClient {
5050
}
5151
}
5252

53+
// Build a ProtocolError-shaped error that callers can recognize as
54+
// "the device's filesystem is currently held by something else"
55+
// (typically USB MSC). Tagged identically to the runtime PUT 409/500
56+
// path so `saveFileContents()` can show the same actionable dialog
57+
// whether the check trips on the cached writable flag or on the
58+
// actual response from the device.
59+
_writeProtectedError() {
60+
const err = new ProtocolError("File System is Read Only.");
61+
err.status = 409;
62+
err.writeProtected = true;
63+
err.hint = "The board's filesystem is currently locked, " +
64+
"usually because CIRCUITPY is mounted on a " +
65+
"computer over USB. Disconnect the USB cable, " +
66+
"or disable USB Mass Storage in boot.py, then " +
67+
"reset the board and try saving again. " +
68+
"(Ejecting the drive in your OS may not be " +
69+
"enough on its own.)";
70+
err.helpUrl = "https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/device-setup#disabling-usb-mass-storage-3125964";
71+
err.helpLabel = "Disabling USB Mass Storage (Adafruit Learn)";
72+
return err;
73+
}
74+
5375
async _checkWritable() {
76+
// Force a re-read of the writable flag so the user can recover
77+
// without disconnecting: if they just released the drive (or
78+
// disabled USB MSC and reset), the next save attempt should
79+
// succeed, not bounce off a stale `false` cache.
80+
this._writable = null;
5481
if (await this.readOnly()) {
55-
throw new Error("File System is Read Only. Try disabling the USB Drive.");
82+
throw this._writeProtectedError();
5683
}
5784
}
5885

@@ -128,15 +155,6 @@ class FileTransferClient {
128155
err.status = response.status;
129156
err.method = (fetchOptions.method || "GET").toUpperCase();
130157
err.path = location;
131-
// DEBUG: leave this in until we're sure the writeProtected
132-
// detection is firing in the field. Cheap, runs only on
133-
// non-OK responses.
134-
console.warn("[web-file-transfer] non-OK response", {
135-
method: err.method,
136-
path: err.path,
137-
status: err.status,
138-
statusText: response.statusText,
139-
});
140158
// /fs/ PUT against a write-protected filesystem currently returns
141159
// 500 on shipped CircuitPython firmware. A fix is pending to
142160
// return 409 Conflict (matching DELETE / MOVE / mkdir-PUT in
@@ -146,16 +164,14 @@ class FileTransferClient {
146164
typeof location === "string" &&
147165
location.startsWith("/fs/");
148166
if (isFsWrite && (response.status === 409 || response.status === 500)) {
167+
// Reuse the same wording/hint as the cached-flag
168+
// _checkWritable() path, so users see one consistent
169+
// message regardless of which layer caught the lock.
170+
const wp = this._writeProtectedError();
149171
err.writeProtected = true;
150-
err.hint = "The board's filesystem is currently locked, " +
151-
"usually because CIRCUITPY is mounted on a " +
152-
"computer over USB. Disconnect the USB cable, " +
153-
"or disable USB Mass Storage in boot.py, then " +
154-
"reset the board and try saving again. " +
155-
"(Ejecting the drive in your OS may not be " +
156-
"enough on its own.)";
157-
err.helpUrl = "https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/device-setup#disabling-usb-mass-storage-3125964";
158-
err.helpLabel = "Disabling USB Mass Storage (Adafruit Learn)";
172+
err.hint = wp.hint;
173+
err.helpUrl = wp.helpUrl;
174+
err.helpLabel = wp.helpLabel;
159175
}
160176
throw err;
161177
}

‎js/script.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,19 @@ async function checkReadOnly() {
331331
await showMessage(readOnly);
332332
return false;
333333
} else if (readOnly) {
334-
await showMessage("Warning: File System is in read only mode. Disable the USB drive to allow write access.");
334+
// Same wording as the per-save dialog, with the Learn-guide
335+
// link, so the very first popup users see at connect already
336+
// points them at the right fix.
337+
const learnUrl = "https://learn.adafruit.com/getting-started-with-web-workflow-using-the-code-editor/device-setup#disabling-usb-mass-storage-3125964";
338+
const learnLabel = "Disabling USB Mass Storage (Adafruit Learn)";
339+
await showMessage(
340+
"Warning: the board's filesystem is read-only, usually because " +
341+
"CIRCUITPY is mounted on a computer over USB. You can browse and " +
342+
"open files, but saving will fail until the lock is released. " +
343+
"Disconnect the USB cable, or disable USB Mass Storage in boot.py, " +
344+
"then reset the board. (Ejecting the drive in your OS may not be " +
345+
`enough on its own.) <a href="${learnUrl}" target="_blank" rel="noopener noreferrer">${learnLabel}</a>.`
346+
);
335347
}
336348
return true;
337349
}
@@ -594,14 +606,6 @@ async function saveFileContents(path) {
594606
return false;
595607
} catch (e) {
596608
console.error(`write failed (attempt ${attempt} of ${MAX_SAVE_RETRIES})`, e, e.stack);
597-
console.warn("[saveFileContents] caught error fields", {
598-
name: e && e.name,
599-
message: e && e.message,
600-
status: e && e.status,
601-
method: e && e.method,
602-
path: e && e.path,
603-
writeProtected: e && e.writeProtected,
604-
});
605609
unchanged = Math.min(baseUnchanged, unchanged);
606610
// If the device cleanly told us the filesystem is held by
607611
// someone else (most commonly USB-MSC: the host has

0 commit comments

Comments
 (0)