Import Notes: an out-of-memory import keeps its own message (#13915)

The io error stays typed until the notes import decides: a pixel
allocation failure (IoError::OutOfMemory) and the PSD memory refusals go
through io_open_error as File > Open reports them; only other failures
become "No notes were imported.".
This commit is contained in:
RickiNano committed 2026-11-15 14:39:16 +01:00
1 parent 4da228b43d
commit 3a76e500de
2 files changed
+38 -10

No files matched your search

+14 -7
View File
@@ -194,15 +194,22 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
/// notes source (#13915).
pub const NO_NOTES_IMPORTED: &str = "No notes were imported.";
/// The error for a source the import could not read. A memory refusal says what the user can do
/// about it and keeps its own (typed) message; anything else (not an image, damaged, truncated)
/// gave no notes (#13915).
fn source_error(name: &str, error: photocraft_io::IoError) -> EngineError {
use photocraft_io::IoError;
match error {
IoError::OutOfMemory(_) | IoError::PsdApprovalRequired { .. } | IoError::PsdImportBudget { .. } => crate::error::io_open_error(name, error),
_ => EngineError::Other(NO_NOTES_IMPORTED.into()),
}
}
/// File › Import › Notes… from file bytes (what a file picker returns).
pub fn import_notes_from(s: &mut Session, name: &str, bytes: &[u8]) -> Result<Value> {
let src = match crate::file_cmds::import(name, bytes) {
Ok(src) => src,
// A memory refusal says what the user can do about it; keep it.
Err(e @ (EngineError::PsdApprovalRequired { .. } | EngineError::PsdHardBudget { .. })) => return Err(e),
// Not an image, damaged or truncated: no notes came out of it (#13915).
Err(_) => return Err(EngineError::Other(NO_NOTES_IMPORTED.into())),
};
// The steps of `file_cmds::import`, with the io error still typed for `source_error`.
crate::allocation::checkpoint("importing document")?;
let src = photocraft_io::import_for_open(name, bytes, &photocraft_io::ImportOptions::default()).map_err(|e| source_error(name, e))?.0.document;
if src.notes.is_empty() {
return Err(EngineError::Other(NO_NOTES_IMPORTED.into()));
}
@@ -1,13 +1,13 @@
//! #13915: File › Import › Notes… from a source that gives no notes fails as Photoshop does, with
//! "No notes were imported.", no history step and the document unmodified. A file that cannot be
//! read at all keeps its "<file>: <why>" error.
//! read at all, or one this machine has no memory for, keeps its own error.
use photocraft_color::{ColorMode, SampleType};
use photocraft_doc::{Document, Note, Size};
use serde_json::json;
use super::{NO_NOTES_IMPORTED, import_notes_from};
use crate::Session;
use super::{NO_NOTES_IMPORTED, import_notes_from, source_error};
use crate::{EngineError, Session};
fn session() -> Session {
let mut s = Session::new();
@@ -78,6 +78,27 @@ fn a_file_with_notes_still_imports() {
}
}
#[test]
fn a_memory_refusal_keeps_its_own_message() {
// A pixel buffer the decoder could not allocate: the file may be fine.
let e = source_error("big.psd", photocraft_io::IoError::OutOfMemory("not enough memory to interleave PSD pixels".into())).to_string();
assert_eq!(e, "big.psd: not enough memory: not enough memory to interleave PSD pixels");
let e = source_error("big.psd", photocraft_io::IoError::PsdImportBudget { required: 9000, limit: 4000, free_memory: None });
assert!(matches!(e, EngineError::PsdHardBudget { .. }), "{e:?}");
let e = source_error("big.psd", photocraft_io::IoError::PsdApprovalRequired { required: 9000, soft_limit: 2000, hard_limit: 4000, available_ram: None });
assert!(matches!(e, EngineError::PsdApprovalRequired { .. }), "{e:?}");
// Not a memory problem: no notes.
assert_eq!(source_error("x.txt", photocraft_io::IoError::UnknownFormat("x.txt".into())).to_string(), NO_NOTES_IMPORTED);
// The allocation checkpoint before decoding keeps its message too, and changes nothing.
let (mut s, bytes) = (session(), pcraft(2));
let past = s.active().unwrap().history.past_len();
crate::allocation::fail_next_for_test();
let e = import_notes_from(&mut s, "a.pcraft", &bytes).unwrap_err().to_string();
assert!(e.starts_with("not enough memory for importing document"), "{e}");
assert_eq!(s.active().unwrap().history.past_len(), past);
assert!(s.active().unwrap().doc.notes.is_empty());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_file_that_cannot_be_read_keeps_its_own_error() {