Skip to content

Commit

Permalink
Added ImapCommand.ThrowIfNotOk() to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Dec 26, 2023
1 parent 393f055 commit 740ff90
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 98 deletions.
27 changes: 9 additions & 18 deletions MailKit/Net/Imap/ImapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ bool TryQueueEnableQuickResyncCommand (CancellationToken cancellationToken, out

static void ProcessEnableResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("ENABLE", ic);
ic.ThrowIfNotOk ("ENABLE");
}

/// <summary>
Expand Down Expand Up @@ -547,8 +546,7 @@ ImapCommand QueueIdentifyCommand (ImapImplementation clientImplementation, Cance

static ImapImplementation ProcessIdentifyResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("ID", ic);
ic.ThrowIfNotOk ("ID");

return (ImapImplementation) ic.UserData;
}
Expand Down Expand Up @@ -1807,8 +1805,7 @@ ImapCommand QueueNoOpCommand (CancellationToken cancellationToken)

static void ProcessNoOpResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("NOOP", ic);
ic.ThrowIfNotOk ("NOOP");
}

/// <summary>
Expand Down Expand Up @@ -1890,8 +1887,7 @@ ImapCommand QueueIdleCommand (ImapIdleContext context, CancellationToken cancell

static void ProcessIdleResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("IDLE", ic);
ic.ThrowIfNotOk ("IDLE");
}

/// <summary>
Expand Down Expand Up @@ -2004,8 +2000,7 @@ ImapCommand QueueNotifyCommand (bool status, IList<ImapEventGroup> eventGroups,

void ProcessNotifyResponse (ImapCommand ic, bool notifySelectedNewExpunge)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("NOTIFY", ic);
ic.ThrowIfNotOk ("NOTIFY");

engine.NotifySelectedNewExpunge = notifySelectedNewExpunge;
}
Expand Down Expand Up @@ -2085,8 +2080,7 @@ ImapCommand QueueDisableNotifyCommand (CancellationToken cancellationToken)

void ProcessDisableNotifyResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("NOTIFY", ic);
ic.ThrowIfNotOk ("NOTIFY");

engine.NotifySelectedNewExpunge = false;
}
Expand Down Expand Up @@ -2430,8 +2424,7 @@ ImapCommand QueueGetMetadataCommand (MetadataTag tag, CancellationToken cancella

string ProcessGetMetadataResponse (ImapCommand ic, MetadataTag tag)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("GETMETADATA", ic);
ic.ThrowIfNotOk ("GETMETADATA");

var metadata = (MetadataCollection) ic.UserData;
string value = null;
Expand Down Expand Up @@ -2557,8 +2550,7 @@ bool TryQueueGetMetadataCommand (MetadataOptions options, IEnumerable<MetadataTa

MetadataCollection ProcessGetMetadataResponse (ImapCommand ic, MetadataOptions options)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("GETMETADATA", ic);
ic.ThrowIfNotOk ("GETMETADATA");

if (ic.RespCodes.Count > 0 && ic.RespCodes[ic.RespCodes.Count - 1].Type == ImapResponseCodeType.Metadata) {
var metadata = (MetadataResponseCode) ic.RespCodes[ic.RespCodes.Count - 1];
Expand Down Expand Up @@ -2663,8 +2655,7 @@ bool TryQueueSetMetadataCommand (MetadataCollection metadata, CancellationToken

static void ProcessSetMetadataResponse (ImapCommand ic)
{
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("SETMETADATA", ic);
ic.ThrowIfNotOk ("SETMETADATA");
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions MailKit/Net/Imap/ImapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,17 @@ public ImapResponseCode GetResponseCode (ImapResponseCodeType type)

return null;
}

/// <summary>
/// Throw an <see cref="ImapCommandException"/> if the response was not OK.
/// </summary>
/// <remarks>
/// Throws an <see cref="ImapCommandException"/> if the response was not OK.
/// </remarks>
public void ThrowIfNotOk (string command)
{
if (Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create (command, this);
}
}
}
12 changes: 4 additions & 8 deletions MailKit/Net/Imap/ImapEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3302,8 +3302,7 @@ ImapFolder ProcessGetQuotaRootResponse (ImapCommand ic, string quotaRoot, out Li

list = (List<ImapFolder>) ic.UserData;

if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("LIST", ic);
ic.ThrowIfNotOk ("LIST");

if ((folder = GetFolder (list, quotaRoot)) == null) {
folder = CreateImapFolder (quotaRoot, FolderAttributes.NonExistent, '.');
Expand Down Expand Up @@ -3386,8 +3385,7 @@ static ImapFolder ProcessGetFolderResponse (ImapCommand ic, string path, string

list = (List<ImapFolder>) ic.UserData;

if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("LIST", ic);
ic.ThrowIfNotOk ("LIST");

if ((folder = GetFolder (list, encodedName)) == null)
throw new FolderNotFoundException (path);
Expand Down Expand Up @@ -3578,8 +3576,7 @@ public IList<IMailFolder> GetFolders (FolderNamespace @namespace, StatusItems it

Run (ic);

if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create (ic.Lsub ? "LSUB" : "LIST", ic);
ic.ThrowIfNotOk (ic.Lsub ? "LSUB" : "LIST");

LookupParentFolders (list, cancellationToken);

Expand Down Expand Up @@ -3611,8 +3608,7 @@ public async Task<IList<IMailFolder>> GetFoldersAsync (FolderNamespace @namespac

await RunAsync (ic).ConfigureAwait (false);

if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create (ic.Lsub ? "LSUB" : "LIST", ic);
ic.ThrowIfNotOk (ic.Lsub ? "LSUB" : "LIST");

await LookupParentFoldersAsync (list, cancellationToken).ConfigureAwait (false);

Expand Down
Loading

0 comments on commit 740ff90

Please sign in to comment.