add more error reporting/relax header requirements

This commit is contained in:
Emil Lerch 2025-10-15 22:08:58 -07:00
parent 7e703c4556
commit 7b6a959086
Signed by: lobo
GPG key ID: A7B62D657EF764F8
2 changed files with 23 additions and 16 deletions

View file

@ -124,6 +124,7 @@ pub const Db = struct {
if (nm_query == null) return error.CouldNotCreateQuery; if (nm_query == null) return error.CouldNotCreateQuery;
const handle = nm_query.?; const handle = nm_query.?;
errdefer c.notmuch_query_destroy(handle); errdefer c.notmuch_query_destroy(handle);
// SAFETY: out paramter in notmuch_query_search_threads
var threads: ?*c.notmuch_threads_t = undefined; var threads: ?*c.notmuch_threads_t = undefined;
const status = c.notmuch_query_search_threads(handle, &threads); const status = c.notmuch_query_search_threads(handle, &threads);
if (status != c.NOTMUCH_STATUS_SUCCESS) return error.CouldNotSearchThreads; if (status != c.NOTMUCH_STATUS_SUCCESS) return error.CouldNotSearchThreads;
@ -155,10 +156,13 @@ pub const Db = struct {
pub const Message = struct { pub const Message = struct {
message_handle: *c.notmuch_message_t, message_handle: *c.notmuch_message_t,
pub fn getHeader(self: Message, header: [:0]const u8) !?[]const u8 { pub fn getHeader(self: Message, header: [:0]const u8) ?[]const u8 {
const val = c.notmuch_message_get_header(self.message_handle, header) orelse return error.ErrorGettingHeader; // null is an error const val = c.notmuch_message_get_header(self.message_handle, header) orelse {
std.log.err("notmuch returned null for header '{s}' on message {s} (file: {s})", .{ header, self.getMessageId(), self.getFilename() });
return null;
};
const ziggy_val = std.mem.span(val); const ziggy_val = std.mem.span(val);
if (ziggy_val.len == 0) return null; // empty string indicates message does not contain the header if (ziggy_val.len == 0) return null;
return ziggy_val; return ziggy_val;
} }
pub fn getMessageId(self: Message) []const u8 { pub fn getMessageId(self: Message) []const u8 {

View file

@ -35,21 +35,24 @@ pub const Thread = struct {
// } // }
//] //]
try jws.beginArray(); try jws.beginArray();
var mi = self.thread.getMessages() catch return error.WriteFailed; var mi = self.thread.getMessages() catch |err| {
std.log.err("Failed to get messages for thread {s}: {s}", .{ self.thread.getThreadId(), @errorName(err) });
return error.WriteFailed;
};
while (mi.next()) |m| { while (mi.next()) |m| {
try jws.beginObject(); try jws.beginObject();
try jws.objectField("from"); try jws.objectField("from");
try jws.write(m.getHeader("from") catch return error.WriteFailed); try jws.write(m.getHeader("from"));
try jws.objectField("to"); try jws.objectField("to");
try jws.write(m.getHeader("to") catch return error.WriteFailed); try jws.write(m.getHeader("to"));
try jws.objectField("cc"); try jws.objectField("cc");
try jws.write(m.getHeader("cc") catch return error.WriteFailed); try jws.write(m.getHeader("cc"));
try jws.objectField("bcc"); try jws.objectField("bcc");
try jws.write(m.getHeader("bcc") catch return error.WriteFailed); try jws.write(m.getHeader("bcc"));
try jws.objectField("date"); try jws.objectField("date");
try jws.write(m.getHeader("date") catch return error.WriteFailed); try jws.write(m.getHeader("date"));
try jws.objectField("subject"); try jws.objectField("subject");
try jws.write(m.getHeader("subject") catch return error.WriteFailed); try jws.write(m.getHeader("subject"));
try jws.objectField("message_id"); try jws.objectField("message_id");
try jws.write(m.getMessageId()); try jws.write(m.getMessageId());
try jws.endObject(); try jws.endObject();
@ -250,12 +253,12 @@ pub const NotmuchDb = struct {
const content_info = try email_msg.getTextAndHtmlBodyVersions(self.allocator); const content_info = try email_msg.getTextAndHtmlBodyVersions(self.allocator);
const attachments = try email_msg.getAttachments(self.allocator); const attachments = try email_msg.getAttachments(self.allocator);
const from = if (notmuch_msg.getHeader("from") catch null) |h| try self.allocator.dupe(u8, h) else null; const from = if (notmuch_msg.getHeader("from")) |h| try self.allocator.dupe(u8, h) else null;
const to = if (notmuch_msg.getHeader("to") catch null) |h| try self.allocator.dupe(u8, h) else null; const to = if (notmuch_msg.getHeader("to")) |h| try self.allocator.dupe(u8, h) else null;
const cc = if (notmuch_msg.getHeader("cc") catch null) |h| try self.allocator.dupe(u8, h) else null; const cc = if (notmuch_msg.getHeader("cc")) |h| try self.allocator.dupe(u8, h) else null;
const bcc = if (notmuch_msg.getHeader("bcc") catch null) |h| try self.allocator.dupe(u8, h) else null; const bcc = if (notmuch_msg.getHeader("bcc")) |h| try self.allocator.dupe(u8, h) else null;
const date = if (notmuch_msg.getHeader("date") catch null) |h| try self.allocator.dupe(u8, h) else null; const date = if (notmuch_msg.getHeader("date")) |h| try self.allocator.dupe(u8, h) else null;
const subject = if (notmuch_msg.getHeader("subject") catch null) |h| try self.allocator.dupe(u8, h) else null; const subject = if (notmuch_msg.getHeader("subject")) |h| try self.allocator.dupe(u8, h) else null;
const msg_id = try self.allocator.dupe(u8, notmuch_msg.getMessageId()); const msg_id = try self.allocator.dupe(u8, notmuch_msg.getMessageId());
return .{ return .{