@@ -54,16 +54,26 @@ public async Task<T> GetAsync<T>(
54
54
55
55
var response = await SendAsync ( requestUri , HttpMethod . Get , headers , cancellationToken : cancellationToken ) ;
56
56
57
- if ( response . IsSuccessStatusCode )
57
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
58
+ }
59
+
60
+ private static async Task < Exception > BuildException ( HttpResponseMessage response )
61
+ {
62
+ var errorBody = await response . Content . ReadAsStringAsync ( ) ;
63
+
64
+ NotionApiErrorResponse errorResponse = null ;
65
+ if ( ! string . IsNullOrWhiteSpace ( errorBody ) )
58
66
{
59
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
67
+ try
68
+ {
69
+ errorResponse = JsonConvert . DeserializeObject < NotionApiErrorResponse > ( errorBody ) ;
70
+ }
71
+ catch
72
+ {
73
+ }
60
74
}
61
75
62
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
63
- ? response . ReasonPhrase
64
- : await response . Content . ReadAsStringAsync ( ) ;
65
-
66
- throw new NotionApiException ( response . StatusCode , message ) ;
76
+ return new NotionApiException ( response . StatusCode , errorResponse ? . ErrorCode , errorResponse . Message ) ;
67
77
}
68
78
69
79
private async Task < HttpResponseMessage > SendAsync (
@@ -84,7 +94,14 @@ private async Task<HttpResponseMessage> SendAsync(
84
94
85
95
attachContent ? . Invoke ( httpRequest ) ;
86
96
87
- return await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
97
+ var response = await _httpClient . SendAsync ( httpRequest , cancellationToken ) ;
98
+
99
+ if ( ! response . IsSuccessStatusCode )
100
+ {
101
+ throw await BuildException ( response ) ;
102
+ }
103
+
104
+ return response ;
88
105
}
89
106
90
107
private static void AddHeaders ( HttpRequestMessage request , IDictionary < string , string > headers )
@@ -114,16 +131,7 @@ void AttachContent(HttpRequestMessage httpRequest)
114
131
115
132
var response = await SendAsync ( requestUri , HttpMethod . Post , headers , AttachContent , cancellationToken : cancellationToken ) ;
116
133
117
- if ( response . IsSuccessStatusCode )
118
- {
119
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
120
- }
121
-
122
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
123
- ? response . ReasonPhrase
124
- : await response . Content . ReadAsStringAsync ( ) ;
125
-
126
- throw new NotionApiException ( response . StatusCode , message ) ;
134
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
127
135
}
128
136
129
137
public async Task < T > PatchAsync < T > ( string uri , object body , IDictionary < string , string > queryParams = null , IDictionary < string , string > headers = null , JsonSerializerSettings serializerSettings = null , CancellationToken cancellationToken = default )
@@ -140,18 +148,7 @@ void AttachContent(HttpRequestMessage httpRequest)
140
148
141
149
var response = await SendAsync ( requestUri , new HttpMethod ( "PATCH" ) , headers , AttachContent , cancellationToken : cancellationToken ) ;
142
150
143
- if ( response . IsSuccessStatusCode )
144
- {
145
- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
146
- }
147
-
148
- var message = ! string . IsNullOrWhiteSpace ( response . ReasonPhrase )
149
- ? response . ReasonPhrase
150
- : await response . Content . ReadAsStringAsync ( ) ;
151
-
152
- var errorMessage = await response . Content . ReadAsStringAsync ( ) ;
153
-
154
- throw new NotionApiException ( response . StatusCode , message ) ;
151
+ return await response . ParseStreamAsync < T > ( serializerSettings ) ;
155
152
}
156
153
157
154
private HttpClient EnsureHttpClient ( )
0 commit comments