|
1 | 1 | using JetBrains.Annotations;
|
2 | 2 |
|
3 | 3 | // We cannot rely on generating ApiException as soon as we are generating multiple clients, see https://github.com/RicoSuter/NSwag/issues/2839#issuecomment-776647377.
|
4 |
| -// Instead, we take the generated code as is and use it for the various clients. |
5 |
| -#nullable disable |
6 |
| -// @formatter:off |
7 |
| -// ReSharper disable All |
8 |
| -namespace JsonApiDotNetCore.OpenApi.Client.Exceptions |
| 4 | +// Instead, we configure NSwag to point to the exception below in the generated code. |
| 5 | + |
| 6 | +// ReSharper disable once CheckNamespace |
| 7 | +namespace JsonApiDotNetCore.OpenApi.Client.Exceptions; |
| 8 | + |
| 9 | +[UsedImplicitly(ImplicitUseTargetFlags.Members)] |
| 10 | +internal class ApiException : Exception |
9 | 11 | {
|
10 |
| - [UsedImplicitly(ImplicitUseTargetFlags.Members)] |
11 |
| - internal class ApiException : Exception |
12 |
| - { |
13 |
| - public int StatusCode { get; } |
| 12 | + public int StatusCode { get; } |
| 13 | + |
| 14 | + public string? Response { get; } |
14 | 15 |
|
15 |
| - public string Response { get; } |
| 16 | + public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; } |
16 | 17 |
|
17 |
| - public IReadOnlyDictionary<string, IEnumerable<string>> Headers { get; } |
| 18 | + public ApiException(string message, int statusCode, string? response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException) |
| 19 | + : base( |
| 20 | + message + "\n\nStatus: " + statusCode + "\nResponse: \n" + |
| 21 | + (response == null ? "(null)" : response[..(response.Length >= 512 ? 512 : response.Length)]), innerException) |
| 22 | + { |
| 23 | + StatusCode = statusCode; |
| 24 | + Response = response; |
| 25 | + Headers = headers; |
| 26 | + } |
18 | 27 |
|
19 |
| - public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, Exception innerException) |
20 |
| - : base( |
21 |
| - message + "\n\nStatus: " + statusCode + "\nResponse: \n" + |
22 |
| - (response == null ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException) |
23 |
| - { |
24 |
| - StatusCode = statusCode; |
25 |
| - Response = response; |
26 |
| - Headers = headers; |
27 |
| - } |
| 28 | + public override string ToString() |
| 29 | + { |
| 30 | + return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}"; |
| 31 | + } |
| 32 | +} |
28 | 33 |
|
29 |
| - public override string ToString() |
30 |
| - { |
31 |
| - return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}"; |
32 |
| - }} |
| 34 | +[UsedImplicitly(ImplicitUseTargetFlags.Members)] |
| 35 | +internal sealed class ApiException<TResult> : ApiException |
| 36 | +{ |
| 37 | + public TResult Result { get; } |
33 | 38 |
|
34 |
| - internal sealed class ApiException<TResult> : ApiException |
| 39 | + public ApiException(string message, int statusCode, string? response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result, |
| 40 | + Exception innerException) |
| 41 | + : base(message, statusCode, response, headers, innerException) |
35 | 42 | {
|
36 |
| - public TResult Result { get; } |
37 |
| - |
38 |
| - public ApiException(string message, int statusCode, string response, IReadOnlyDictionary<string, IEnumerable<string>> headers, TResult result, |
39 |
| - Exception innerException) |
40 |
| - : base(message, statusCode, response, headers, innerException) |
41 |
| - { |
42 |
| - Result = result; |
43 |
| - }} |
| 43 | + Result = result; |
| 44 | + } |
44 | 45 | }
|
0 commit comments