@@ -54,16 +54,26 @@ public async Task<T> GetAsync<T>(
5454
5555 var response = await SendAsync ( requestUri , HttpMethod . Get , headers , cancellationToken : cancellationToken ) ;
5656
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 ) )
5866 {
59- return await response . ParseStreamAsync < T > ( serializerSettings ) ;
67+ try
68+ {
69+ errorResponse = JsonConvert . DeserializeObject < NotionApiErrorResponse > ( errorBody ) ;
70+ }
71+ catch
72+ {
73+ }
6074 }
6175
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 ) ;
6777 }
6878
6979 private async Task < HttpResponseMessage > SendAsync (
@@ -84,7 +94,14 @@ private async Task<HttpResponseMessage> SendAsync(
8494
8595 attachContent ? . Invoke ( httpRequest ) ;
8696
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 ;
88105 }
89106
90107 private static void AddHeaders ( HttpRequestMessage request , IDictionary < string , string > headers )
@@ -114,16 +131,7 @@ void AttachContent(HttpRequestMessage httpRequest)
114131
115132 var response = await SendAsync ( requestUri , HttpMethod . Post , headers , AttachContent , cancellationToken : cancellationToken ) ;
116133
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 ) ;
127135 }
128136
129137 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)
140148
141149 var response = await SendAsync ( requestUri , new HttpMethod ( "PATCH" ) , headers , AttachContent , cancellationToken : cancellationToken ) ;
142150
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 ) ;
155152 }
156153
157154 private HttpClient EnsureHttpClient ( )
0 commit comments