{"record":{"id":"d2303dbae9900870","repo":"jstedfast/MailKit","slug":"cannot-search-for-an-empty-string","errorCode":null,"errorMessage":"Cannot search for an empty string.","messagePattern":"Cannot search for an empty string\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/SearchQuery.cs","lineNumber":1179,"sourceCode":"\t\t/// for use with IMAP servers that support the <c>X-GM-EXT1</c> extension.</note>\n\t\t/// <seealso cref=\"MailKit.Net.Imap.ImapCapabilities.GMailExt1\"/>\n\t\t/// <seealso cref=\"MailKit.Net.Imap.ImapClient.Capabilities\"/>\n\t\t/// </remarks>\n\t\t/// <returns>A <see cref=\"TextSearchQuery\"/>.</returns>\n\t\t/// <param name=\"label\">The GMail label.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"label\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"label\"/> is empty.\n\t\t/// </exception>\n\t\tpublic static TextSearchQuery HasGMailLabel (string label)\n\t\t{\n\t\t\tif (label == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (label));\n\n\t\t\tif (label.Length == 0)\n\t\t\t\tthrow new ArgumentException (\"Cannot search for an empty string.\", nameof (label));\n\n\t\t\treturn new TextSearchQuery (SearchTerm.GMailLabels, label);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Match messages using the GMail search expression.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Matches messages using Google's custom message search syntax.</para>\n\t\t/// <note type=\"note\">This is equivalent to the <c>X-GM-RAW</c> search key as defined in <a href=\"https://developers.google.com/gmail/imap_extensions\">Google's IMAP extensions</a> and is therefor only available\n\t\t/// for use with IMAP servers that support the <c>X-GM-EXT1</c> extension.</note>\n\t\t/// <seealso cref=\"MailKit.Net.Imap.ImapCapabilities.GMailExt1\"/>\n\t\t/// <seealso cref=\"MailKit.Net.Imap.ImapClient.Capabilities\"/>\n\t\t/// </remarks>\n\t\t/// <returns>A <see cref=\"TextSearchQuery\"/>.</returns>\n\t\t/// <param name=\"expression\">The raw GMail search text.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"expression\"/> is <see langword=\"null\" />.","sourceCodeStart":1161,"sourceCodeEnd":1197,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/SearchQuery.cs#L1161-L1197","documentation":"SearchQuery.HasGMailLabel(string label) rejects an empty-string label with ArgumentException(\"Cannot search for an empty string.\") because IMAP/GMail label searches require non-empty text; an empty term would produce an invalid search key.","triggerScenarios":"Calling SearchQuery.HasGMailLabel(\"\") — usually from a config value that is present but blank, or string.Split/trimming results yielding \"\".","commonSituations":"Blank label entries in settings files; user leaving a label field empty in UI; parsing label lists where an empty token appears.","solutions":["Check string.IsNullOrEmpty(label) before calling HasGMailLabel and skip the query when empty.","Trim and validate user/config-supplied label values at input time.","Filter out empty entries when building labels from a list."],"exampleFix":"// before\nvar query = SearchQuery.HasGMailLabel(label); // label may be \"\"\n// after\nvar trimmed = label?.Trim();\nvar query = string.IsNullOrEmpty(trimmed) ? null : SearchQuery.HasGMailLabel(trimmed);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(label)) throw new ArgumentException(\"GMail label cannot be empty.\", nameof(label));\nvar query = SearchQuery.HasGMailLabel(label.Trim());","typeGuard":"static bool IsValidLabel(string label) => !string.IsNullOrWhiteSpace(label);","tryCatchPattern":"try { var q = SearchQuery.HasGMailLabel(label); } catch (ArgumentException) { /* skip label filter */ }","preventionTips":["Trim and reject blank labels at input time.","Skip empty label entries when building queries from lists.","Require non-empty label fields in configuration schemas."],"tags":["empty-string","gmail-label","argument"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}