Skip to content

Commit

Permalink
Add MediaWikiRemoteException.
Browse files Browse the repository at this point in the history
You may retrieve remote stack trace of internal_api_error_ from this exception now.
  • Loading branch information
CXuesong committed Jul 11, 2019
1 parent 1fbf874 commit 842e563
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
11 changes: 10 additions & 1 deletion WikiClientLibrary/Client/MediaWikiJsonResponseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public override async Task<JToken> ParseResponseAsync(HttpResponseMessage respon
/// <description><see cref="OperationConflictException"/></description>
/// </item>
/// <item>
/// <item>
/// <term><c>internal_api_error*</c></term>
/// <description><see cref="MediaWikiRemoteException"/></description>
/// </item>
/// <term>others</term>
/// <description><see cref="OperationFailedException"/></description>
/// </item>
Expand Down Expand Up @@ -160,7 +164,7 @@ public override async Task<JToken> ParseResponseAsync(HttpResponseMessage respon
case "prev_revision":
throw new OperationConflictException(errorCode, errorMessage);
default:
if (errorCode.EndsWith("conflict"))
if (errorCode.EndsWith("conflict", StringComparison.OrdinalIgnoreCase))
throw new OperationConflictException(errorCode, errorMessage);
// "messages": [
// {
Expand All @@ -171,6 +175,11 @@ public override async Task<JToken> ParseResponseAsync(HttpResponseMessage respon
// }
// },
// ...]
if (errorCode.StartsWith("internal_api_error", StringComparison.OrdinalIgnoreCase))
{
throw new MediaWikiRemoteException(errorCode, errorMessage,
(string)errorNode["errorclass"], (string)errorNode["*"]);
}
var messages = (JArray)errorNode["messages"];
if (messages != null && messages.Count > 1 && messages[0]["html"] != null)
{
Expand Down
76 changes: 71 additions & 5 deletions WikiClientLibrary/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public WikiClientException(string message) : base(message)

public WikiClientException(string message, Exception innerException) : base(message, innerException)
{

}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ public OperationFailedException(string errorCode, string errorMessage)
string.Format(
string.IsNullOrEmpty(errorMessage)
? "{0}"
: "{0}:{1}",
: "{0}: {1}",
errorCode, errorMessage))
{
ErrorCode = errorCode;
Expand Down Expand Up @@ -103,7 +103,27 @@ public AccountAssertionFailureException(string errorCode, string message)
/// <summary>
/// Raises when user has no rights for certain operations.
/// </summary>
/// <remarks>This corresponds to <c>*conflict</c> MW API error.</remarks>
/// <remarks>
/// This exception corresponds to the following MW API errors:
/// <list type="bullet">
/// <item>
/// <term>readapidenied</term>
/// <description>You need read permission to use this module.</description>
/// </item>
/// <item><term>permissiondenied</term></item>
/// <item>
/// <term>mustbeloggedin</term>
/// <description>You must be logged in to upload this file.</description>
/// </item>
/// <item><term>readapidenied</term></item>
/// <item><term>permissions</term></item>
/// <item><term>cantpurge</term></item>
/// <item>
/// <term>(empty ErrorCode)</term>
/// <description>Other cases where the user does not have the rights to perform the operation.</description>
/// </item>
/// </list>
/// </remarks>
public class UnauthorizedOperationException : OperationFailedException
{
public UnauthorizedOperationException(string errorCode, string message)
Expand All @@ -124,27 +144,72 @@ public UnauthorizedOperationException(OperationFailedException ex)
}

/// <summary>
/// Raises when conflict detected performing the operation.
/// Raises when conflict detected performing the operation. (<c>*conflict</c>)
/// </summary>
/// <remarks>This corresponds to <c>*conflict</c> MW API error.</remarks>
public class OperationConflictException : OperationFailedException
{

public OperationConflictException(string errorCode, string message)
: base(errorCode, message)
{
}

}

/// <summary>
/// Raises when the token used to invoke MediaWiki API is invalid.
/// Raises when the token used to invoke MediaWiki API is invalid. (<c>badtoken</c>)
/// </summary>
/// <remarks>This corresponds to <c>badtoken</c> MW API error.</remarks>
public class BadTokenException : OperationFailedException
{

public BadTokenException(string errorCode, string message)
: base(errorCode, message)
{
}

}

/// <summary>
/// Raises when the remote MediaWiki site has encountered an internal error. (<c>internal_api_error_*</c>)
/// </summary>
/// <remarks>This corresponds to <c>internal_api_error_*</c> MW API error.</remarks>
public class MediaWikiRemoteException : OperationFailedException
{

public virtual string ErrorClass { get; }

public virtual string RemoteStackTrace { get; }

public MediaWikiRemoteException(string errorCode, string message)
: this(errorCode, message, null, null)
{
}

public MediaWikiRemoteException(string errorCode, string message, string errorClass, string remoteStackTrace)
: base(errorCode, message)
{
ErrorClass = errorClass;
RemoteStackTrace = remoteStackTrace;
}

/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(GetType());
sb.Append(": ");
sb.Append(Message);
sb.AppendLine();
if (!string.IsNullOrEmpty(RemoteStackTrace))
{
sb.AppendLine(RemoteStackTrace);
sb.AppendLine(Prompts.MediaWikiRemoteStackTraceEnd);
}
sb.Append(StackTrace);
return sb.ToString();
}
}

/// <summary>
Expand All @@ -165,4 +230,5 @@ public UnexpectedDataException(string message, Exception inner)
: base(message, inner)
{ }
}

}
9 changes: 9 additions & 0 deletions WikiClientLibrary/Prompts.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions WikiClientLibrary/Prompts.resx
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,7 @@
<data name="ExceptionPatrolledByRevisionNotSupported" xml:space="preserve">
<value>Current version of site does not support patrol by RevisionId.</value>
</data>
<data name="MediaWikiRemoteStackTraceEnd" xml:space="preserve">
<value>--- End of MediaWiki remote stack trace ---</value>
</data>
</root>
3 changes: 3 additions & 0 deletions WikiClientLibrary/Prompts.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,7 @@
<data name="ExceptionWikiPageResolveCircularRedirect1" xml:space="preserve">
<value>无法解析循环重定向:{0}。</value>
</data>
<data name="MediaWikiRemoteStackTraceEnd" xml:space="preserve">
<value>--- MediaWiki远程调用堆栈结束 ---</value>
</data>
</root>

0 comments on commit 842e563

Please sign in to comment.