Fauna v10 .NET/C# Driver 1.0.0
 
Loading...
Searching...
No Matches
ExceptionHandler.cs
Go to the documentation of this file.
1using System.Net;
2using Fauna.Core;
3using Fauna.Mapping;
4
5namespace Fauna.Exceptions;
6
10public static class ExceptionHandler
11{
18 public static Exception FromQueryFailure(MappingContext ctx, QueryFailure f)
19 {
20 var msg =
21 $"{f.StatusCode} ({f.ErrorCode}): {f.Message}{(f.Summary is { Length: > 0 } ? "\n---\n" + f.Summary : "")}";
22
23 return f.ErrorCode switch
24 {
25 "abort" => new AbortException(msg, f, ctx),
26 "bad_gateway" => new BadGatewayException(msg, f),
27 "contended_transaction" => new ContendedTransactionException(msg, f),
28 "forbidden" => new AuthorizationException(msg, f),
29 "internal_error" => new ServiceException(msg, f),
30 "invalid_query" => new QueryCheckException(msg, f),
31 "invalid_request" => new InvalidRequestException(msg, f),
32 "limit_exceeded" => new ThrottlingException(msg, f),
33 "time_out" => new QueryTimeoutException(msg, f),
34 "gateway_timeout" => new NetworkException(msg, f.StatusCode, f.Message),
35 "unauthorized" => new AuthenticationException(msg, f),
36 "constraint_failure" => new ConstraintFailureException(msg, f),
37
38 _ => new QueryRuntimeException(msg, f)
39 };
40 }
41
42
49 public static Exception FromRawResponse(string body, HttpResponseMessage r)
50 {
51 if (r.StatusCode is >= HttpStatusCode.OK and <= (HttpStatusCode)299)
52 {
53 // We should never get here, but if we do it's outside of the expected wire protocol.
54 return new ProtocolException("Malformed response.", r.StatusCode, body);
55 }
56
57 return r.StatusCode switch
58 {
59 HttpStatusCode.TooManyRequests => new ThrottlingException(
60 $"{r.StatusCode}: {r.ReasonPhrase ?? "Too many requests."}"),
61 _ => new FaunaException($"{r.StatusCode}: {r.ReasonPhrase}")
62 };
63 }
64}
Represents a failed query response.
string Message
The query failure message.
HttpStatusCode StatusCode
The HTTP status code.
A class representing the mapping context to be used during serialization and deserialization.