Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds PermissionDenied exception #20

Merged
merged 1 commit into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions FaunaDB.Client.Test/ErrorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Threading.Tasks;
using Newtonsoft.Json;

using FaunaDB.Types;
using FaunaDB.Client;
using static FaunaDB.Query.Language;

namespace Test
Expand Down Expand Up @@ -36,6 +38,15 @@ namespace Test
var client = MockClient("{\"errors\": [{\"code\": \"unavailable\", \"description\": \"on vacation\"}]}", HttpStatusCode.ServiceUnavailable);
Assert.ThrowsAsync<UnavailableError>(async() => await client.Query(Get(Ref(""))), "unavailable");
}

[Test] public async Task TestPermissionDenied()
{
var key = await rootClient.Query(Create(Ref("keys"), Obj("database", DbRef, "role", "client")));

var client = GetClient(secret: key.Get(Field.At("secret").To(Codec.STRING)));

AssertQueryException<PermissionDenied>(client, Paginate(Ref("databases")), "permission denied", "Insufficient privileges to perform the action.");
}
#endregion

#region ErrorData
Expand Down Expand Up @@ -116,6 +127,12 @@ void AssertException(FaunaException exception, string code, string description,
}

void AssertQueryException<TException>(Expr query, string code, string description, IReadOnlyList<string> position = null)
where TException : FaunaException
{
AssertQueryException<TException>(client, query, code, description, position);
}

void AssertQueryException<TException>(FaunaClient client, Expr query, string code, string description, IReadOnlyList<string> position = null)
where TException : FaunaException
{
var exception = Assert.ThrowsAsync<TException>(async() => await client.Query(query));
Expand Down
2 changes: 1 addition & 1 deletion FaunaDB.Client.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Test
{
public class TestCase
{
FaunaClient rootClient;
protected FaunaClient rootClient;
protected RefV DbRef;
protected FaunaClient client;

Expand Down
2 changes: 2 additions & 0 deletions FaunaDB.Client/Client/FaunaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ internal static void RaiseForStatusCode(RequestResult resultRequest)
throw new BadRequest(response);
case 401:
throw new Unauthorized(response);
case 403:
throw new PermissionDenied(response);
case 404:
throw new NotFound(response);
case 500:
Expand Down
10 changes: 10 additions & 0 deletions FaunaDB.Client/Errors/FaunaException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public class Unauthorized : FaunaException
internal Unauthorized(QueryErrorResponse response) : base(response) {}
}

/// <summary>
/// HTTP 403 error.
/// An exception thrown if FaunaDB responds with an HTTP 403 (Permission Denied).
/// <para>HTTP 403 error.</para>
/// </summary>
public class PermissionDenied : FaunaException
{
internal PermissionDenied(QueryErrorResponse response) : base(response) {}
}

/// <summary>
/// HTTP 404 error.
/// An exception thrown if a HTTP 404 (Not Found) is returned from FaunaDB.
Expand Down