Skip to content

Commit

Permalink
Adding error code to query rejection (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Feb 3, 2023
1 parent de3b89f commit a1f1131
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 47 deletions.
61 changes: 44 additions & 17 deletions src/Agent/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@
- [#ctor(message)](#M-EdjCase-ICP-Agent-InvalidCertificateException-#ctor-System-String- 'EdjCase.ICP.Agent.InvalidCertificateException.#ctor(System.String)')
- [InvalidPublicKey](#T-EdjCase-ICP-Agent-InvalidPublicKey 'EdjCase.ICP.Agent.InvalidPublicKey')
- [#ctor()](#M-EdjCase-ICP-Agent-InvalidPublicKey-#ctor 'EdjCase.ICP.Agent.InvalidPublicKey.#ctor')
- [QueryRejectInfo](#T-EdjCase-ICP-Agent-Responses-QueryRejectInfo 'EdjCase.ICP.Agent.Responses.QueryRejectInfo')
- [Code](#P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-Code 'EdjCase.ICP.Agent.Responses.QueryRejectInfo.Code')
- [ErrorCode](#P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-ErrorCode 'EdjCase.ICP.Agent.Responses.QueryRejectInfo.ErrorCode')
- [Message](#P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-Message 'EdjCase.ICP.Agent.Responses.QueryRejectInfo.Message')
- [QueryRejectedException](#T-EdjCase-ICP-Agent-QueryRejectedException 'EdjCase.ICP.Agent.QueryRejectedException')
- [#ctor(code,message)](#M-EdjCase-ICP-Agent-QueryRejectedException-#ctor-EdjCase-ICP-Agent-Responses-RejectCode,System-String- 'EdjCase.ICP.Agent.QueryRejectedException.#ctor(EdjCase.ICP.Agent.Responses.RejectCode,System.String)')
- [Code](#P-EdjCase-ICP-Agent-QueryRejectedException-Code 'EdjCase.ICP.Agent.QueryRejectedException.Code')
- [#ctor(info)](#M-EdjCase-ICP-Agent-QueryRejectedException-#ctor-EdjCase-ICP-Agent-Responses-QueryRejectInfo- 'EdjCase.ICP.Agent.QueryRejectedException.#ctor(EdjCase.ICP.Agent.Responses.QueryRejectInfo)')
- [Info](#P-EdjCase-ICP-Agent-QueryRejectedException-Info 'EdjCase.ICP.Agent.QueryRejectedException.Info')
- [Message](#P-EdjCase-ICP-Agent-QueryRejectedException-Message 'EdjCase.ICP.Agent.QueryRejectedException.Message')
- [RejectionMessage](#P-EdjCase-ICP-Agent-QueryRejectedException-RejectionMessage 'EdjCase.ICP.Agent.QueryRejectedException.RejectionMessage')
- [QueryReply](#T-EdjCase-ICP-Agent-Responses-QueryReply 'EdjCase.ICP.Agent.Responses.QueryReply')
- [Arg](#P-EdjCase-ICP-Agent-Responses-QueryReply-Arg 'EdjCase.ICP.Agent.Responses.QueryReply.Arg')
- [QueryRequest](#T-EdjCase-ICP-Agent-Requests-QueryRequest 'EdjCase.ICP.Agent.Requests.QueryRequest')
Expand Down Expand Up @@ -1480,6 +1483,38 @@ Exception to indicate that the specified BLS public key is invalid

This constructor has no parameters.

<a name='T-EdjCase-ICP-Agent-Responses-QueryRejectInfo'></a>
## QueryRejectInfo `type`

##### Namespace

EdjCase.ICP.Agent.Responses

##### Summary

Data from a query response that has been rejected

<a name='P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-Code'></a>
### Code `property`

##### Summary

The type of query reject

<a name='P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-ErrorCode'></a>
### ErrorCode `property`

##### Summary

Optional. A specific error id for the reject

<a name='P-EdjCase-ICP-Agent-Responses-QueryRejectInfo-Message'></a>
### Message `property`

##### Summary

Optional. A human readable message about the rejection

<a name='T-EdjCase-ICP-Agent-QueryRejectedException'></a>
## QueryRejectedException `type`

Expand All @@ -1491,22 +1526,21 @@ EdjCase.ICP.Agent

Exception for when a query to a canister is rejected/has an error

<a name='M-EdjCase-ICP-Agent-QueryRejectedException-#ctor-EdjCase-ICP-Agent-Responses-RejectCode,System-String-'></a>
### #ctor(code,message) `constructor`
<a name='M-EdjCase-ICP-Agent-QueryRejectedException-#ctor-EdjCase-ICP-Agent-Responses-QueryRejectInfo-'></a>
### #ctor(info) `constructor`

##### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| code | [EdjCase.ICP.Agent.Responses.RejectCode](#T-EdjCase-ICP-Agent-Responses-RejectCode 'EdjCase.ICP.Agent.Responses.RejectCode') | The type of rejection that occurred |
| message | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The human readable message of the rejection error |
| info | [EdjCase.ICP.Agent.Responses.QueryRejectInfo](#T-EdjCase-ICP-Agent-Responses-QueryRejectInfo 'EdjCase.ICP.Agent.Responses.QueryRejectInfo') | The type of rejection that occurred |

<a name='P-EdjCase-ICP-Agent-QueryRejectedException-Code'></a>
### Code `property`
<a name='P-EdjCase-ICP-Agent-QueryRejectedException-Info'></a>
### Info `property`

##### Summary

The type of rejection that occurred
The details of the rejection

<a name='P-EdjCase-ICP-Agent-QueryRejectedException-Message'></a>
### Message `property`
Expand All @@ -1515,13 +1549,6 @@ The type of rejection that occurred

*Inherit from parent.*

<a name='P-EdjCase-ICP-Agent-QueryRejectedException-RejectionMessage'></a>
### RejectionMessage `property`

##### Summary

The human readable message of the rejection error

<a name='T-EdjCase-ICP-Agent-Responses-QueryReply'></a>
## QueryReply `type`

Expand Down
34 changes: 24 additions & 10 deletions src/Agent/API.xml

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

6 changes: 5 additions & 1 deletion src/Agent/Cbor/Converters/QueryResponseCborConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override QueryResponse Read(ref CborReader reader)
return QueryResponse.Replied(reply);
case "rejected":
RejectCode code = (RejectCode)(ulong)context.RejectCode!;
return QueryResponse.Rejected(code, context.RejectMessage);
return QueryResponse.Rejected(code, context.RejectMessage, context.ErrorCode);
default:
throw new NotImplementedException($"Cannot deserialize query response with status '{context.Status}'");
}
Expand Down Expand Up @@ -63,6 +63,9 @@ private void SetQueryResponseField(string name, ref CborReader reader, ref Query
case "reject_message":
context.RejectMessage = reader.ReadString();
break;
case "error_code":
context.ErrorCode = reader.ReadString();
break;
default:
throw new NotImplementedException($"Cannot deserialize query response. Unknown field '{name}'");
}
Expand Down Expand Up @@ -93,5 +96,6 @@ internal class QueryReponseContext
public byte[]? ReplyArg { get; set; }
public UnboundedUInt? RejectCode { get; set; }
public string? RejectMessage { get; set; }
public string? ErrorCode { get; set; }
}
}
19 changes: 6 additions & 13 deletions src/Agent/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,17 @@ public CallRejectedException(RejectCode rejectCode, string rejectMessage, string
public class QueryRejectedException : Exception
{
/// <summary>
/// The type of rejection that occurred
/// </summary>
public RejectCode Code { get; }

/// <summary>
/// The human readable message of the rejection error
/// The details of the rejection
/// </summary>
public string? RejectionMessage { get; }
public QueryRejectInfo Info { get; }

/// <param name="code">The type of rejection that occurred</param>
/// <param name="message">The human readable message of the rejection error</param>
public QueryRejectedException(RejectCode code, string? message)
/// <param name="info">The type of rejection that occurred</param>
internal QueryRejectedException(QueryRejectInfo info)
{
this.Code = code;
this.RejectionMessage = message;
this.Info = info;
}

/// <inheritdoc />
public override string Message => $"Query was rejected. Code: {this.Code}, Message: {this.Message}";
public override string Message => $"Query was rejected. Code: {this.Info.Code}, Message: {this.Info.Message}, ErrorCode: {this.Info.ErrorCode}";
}
}
39 changes: 33 additions & 6 deletions src/Agent/Responses/QueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public QueryReply AsReplied()
/// </summary>
/// <exception cref="InvalidOperationException">Will throw if not of type 'rejected'</exception>
/// <returns>Reject error information</returns>
public (RejectCode Code, string? Message) AsRejected()
public QueryRejectInfo AsRejected()
{
this.ThrowIfWrongType(QueryResponseType.Rejected);
return (ValueTuple<RejectCode, string>)this.value;
return (QueryRejectInfo)this.value;
}

private void ThrowIfWrongType(QueryResponseType type)
Expand All @@ -61,15 +61,15 @@ public QueryReply ThrowOrGetReply()
{
if (this.Type == QueryResponseType.Rejected)
{
(RejectCode code, string? message) = this.AsRejected();
throw new QueryRejectedException(code, message);
QueryRejectInfo rejectInfo = this.AsRejected();
throw new QueryRejectedException(rejectInfo);
}
return this.AsReplied();
}

internal static QueryResponse Rejected(RejectCode code, string? message)
internal static QueryResponse Rejected(RejectCode code, string? message, string? errorCode)
{
return new QueryResponse(QueryResponseType.Rejected, (code, message));
return new QueryResponse(QueryResponseType.Rejected, new QueryRejectInfo(code, message, errorCode));
}

internal static QueryResponse Replied(QueryReply reply)
Expand Down Expand Up @@ -109,4 +109,31 @@ internal QueryReply(CandidArg arg)
}
}

/// <summary>
/// Data from a query response that has been rejected
/// </summary>
public class QueryRejectInfo
{
/// <summary>
/// The type of query reject
/// </summary>
public RejectCode Code { get; }

/// <summary>
/// Optional. A human readable message about the rejection
/// </summary>
public string? Message { get; }

/// <summary>
/// Optional. A specific error id for the reject
/// </summary>
public string? ErrorCode { get; }

internal QueryRejectInfo(RejectCode code, string? message, string? errorCode)
{
this.Code = code;
this.Message = message;
this.ErrorCode = errorCode;
}
}
}

0 comments on commit a1f1131

Please sign in to comment.