File tree 1 file changed +27
-2
lines changed
1 file changed +27
-2
lines changed Original file line number Diff line number Diff line change 2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import fetch from "node-fetch" ;
5
- import type { RequestInfo , RequestInit , Response } from "node-fetch" ;
5
+ import type { RequestInfo , RequestInit , Response , Headers } from "node-fetch" ;
6
6
import { buildQueryString } from "./utils" ;
7
7
import {
8
8
AuthClient ,
@@ -52,6 +52,25 @@ async function fetchWithRetries(
52
52
return res ;
53
53
}
54
54
55
+ class TwitterResponseError extends Error {
56
+ status : number ;
57
+ statusText : string ;
58
+ headers : Record < string , any > ;
59
+ error : Record < string , any > ;
60
+ constructor (
61
+ status : number ,
62
+ statusText : string ,
63
+ headers : Headers ,
64
+ error : Record < string , any >
65
+ ) {
66
+ super ( ) ;
67
+ this . status = status ;
68
+ this . statusText = statusText ;
69
+ this . headers = Object . fromEntries ( headers ) ;
70
+ this . error = error ;
71
+ }
72
+ }
73
+
55
74
export async function request ( {
56
75
auth,
57
76
endpoint,
@@ -90,7 +109,13 @@ export async function request({
90
109
max_retries
91
110
) ;
92
111
if ( ! response . ok ) {
93
- throw new Error ( `${ response . status } , ${ response . statusText } ` ) ;
112
+ const error = await response . json ( ) ;
113
+ throw new TwitterResponseError (
114
+ response . status ,
115
+ response . statusText ,
116
+ response . headers ,
117
+ error
118
+ ) ;
94
119
}
95
120
return response ;
96
121
}
You can’t perform that action at this time.
0 commit comments