-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionExtensions.cs
More file actions
96 lines (85 loc) · 3.1 KB
/
ExceptionExtensions.cs
File metadata and controls
96 lines (85 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Couchbase.AnalyticsClient.Exceptions;
using Couchbase.Grpc.Protocol.Columnar;
using InvalidCredentialException = Couchbase.AnalyticsClient.Exceptions.InvalidCredentialException;
using QueryException = Couchbase.AnalyticsClient.Exceptions.QueryException;
namespace Couchbase.Analytics.Performer.Internal.Utils;
internal static class ExceptionExtensions
{
public static Error ToProtoError(this Exception exception)
{
Serilog.Log.Information("An error happened: {Message}", exception.ToString());
var errorResponse = new Error();
if (exception.IsColumnarError())
{
var columnarError = new ColumnarError
{
AsString = exception.ToString()
};
if (exception is QueryException queryException)
{
columnarError.SubException = new SubColumnarError
{
QueryException = new Grpc.Protocol.Columnar.QueryException
{
ErrorCode = queryException.Code,
ServerMessage = queryException.Message
}
};
}
if (exception is InvalidCredentialException)
{
columnarError.SubException = new SubColumnarError
{
InvalidCredentialException =
new Grpc.Protocol.Columnar.InvalidCredentialException()
};
}
if (exception is AnalyticsTimeoutException timeoutEx)
{
columnarError.SubException = new SubColumnarError
{
TimeoutException = new Grpc.Protocol.Columnar.TimeoutException()
};
if (timeoutEx.InnerException is not null)
{
columnarError.Cause = timeoutEx.InnerException.ToProtoError();
}
}
if (exception.InnerException != null)
{
columnarError.Cause = ToProtoError(exception.InnerException);
}
errorResponse.Columnar ??= columnarError;
}
else
{
errorResponse.Platform = UnwrapPlatformError(exception);
}
return errorResponse;
}
private static PlatformError UnwrapPlatformError(this Exception exception)
{
return new PlatformError()
{
Type = exception is ArgumentException
? PlatformErrorType.PlatformErrorInvalidArgument
: PlatformErrorType.PlatformErrorUnspecified,
AsString = exception.Message
};
}
private static bool IsColumnarError(this Exception exception)
{
switch (exception)
{
case null:
return false;
case InvalidCredentialException invalidCredentialException:
case QueryException queryException:
case AnalyticsTimeoutException timeoutException:
case AnalyticsException analyticsException:
return true;
default:
return false;
}
}
}