Skip to content

Commit 6acc470

Browse files
committed
fix: bad response handling of put/post requests #61
1 parent 666fb4a commit 6acc470

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

Diff for: src/api/util.ts

+44-24
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export async function doGetRequest<T>(reqOptions: RequestOptionsBase, pathname:
2424
export type QueryParameters = Record<string, string | number | boolean | null | undefined>
2525
export type BodyParameters = Record<string, any>
2626

27-
export async function doPutRequest<T>(
27+
export async function doPutRequest(
2828
reqOptions: RequestOptionsBase,
2929
pathname: string,
3030
queryParams: QueryParameters,
3131
body: BodyParameters,
32-
): Promise<Response<T>> {
33-
return doRequest<T>(
34-
got.put<T>(SpotifyBaseUrl + pathname, {
32+
): Promise<Response<void>> {
33+
return doRequestNoResponse(
34+
got.put<void>(SpotifyBaseUrl + pathname, {
3535
headers: {
3636
Authorization: `Bearer ${reqOptions.accessToken}`,
3737
'Content-Type': 'application/json',
@@ -47,13 +47,13 @@ export async function doPutRequest<T>(
4747
)
4848
}
4949

50-
export async function doPostRequest<T>(
50+
export async function doPostRequest(
5151
reqOptions: RequestOptionsBase,
5252
pathname: string,
5353
queryParams: QueryParameters,
54-
): Promise<Response<T>> {
55-
return doRequest<T>(
56-
got.post<T>(SpotifyBaseUrl + pathname, {
54+
): Promise<Response<void>> {
55+
return doRequestNoResponse(
56+
got.post<void>(SpotifyBaseUrl + pathname, {
5757
headers: {
5858
Authorization: `Bearer ${reqOptions.accessToken}`,
5959
'Content-Type': 'application/json',
@@ -68,32 +68,52 @@ export async function doPostRequest<T>(
6868
)
6969
}
7070

71+
export async function doRequestNoResponse(req: CancelableRequest<Response<void>>): Promise<Response<void>> {
72+
try {
73+
// console.log('json', await req.json(), (await req.buffer()).length)
74+
const res = await req
75+
76+
return {
77+
headers: res.headers,
78+
statusCode: res.statusCode,
79+
body: null,
80+
}
81+
} catch (e: unknown) {
82+
return wrapHttpError(e)
83+
}
84+
}
85+
7186
export async function doRequest<T>(req: CancelableRequest<Response<T>>): Promise<Response<T>> {
7287
try {
7388
// console.log('json', await req.json(), (await req.buffer()).length)
7489
const res = await req
75-
// console.log(res.body)
90+
91+
console.log('body', res.body)
7692
return {
7793
headers: res.headers,
7894
statusCode: res.statusCode,
7995
body: await req.json(),
8096
}
8197
} catch (e: unknown) {
82-
if (e instanceof HTTPError) {
83-
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
84-
return Promise.reject({
85-
headers: e.response.headers,
86-
statusCode: e.response.statusCode,
87-
error: e,
88-
})
89-
} else {
90-
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
91-
return Promise.reject({
92-
headers: {},
93-
statusCode: 500,
94-
error: e,
95-
})
96-
}
98+
return wrapHttpError(e)
99+
}
100+
}
101+
102+
async function wrapHttpError(e: unknown): Promise<never> {
103+
if (e instanceof HTTPError) {
104+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
105+
return Promise.reject({
106+
headers: e.response.headers,
107+
statusCode: e.response.statusCode,
108+
error: e,
109+
})
110+
} else {
111+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
112+
return Promise.reject({
113+
headers: {},
114+
statusCode: 500,
115+
error: e,
116+
})
97117
}
98118
}
99119

Diff for: src/main.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ class SpotifyInstance extends InstanceBase<DeviceConfig> implements SpotifyInsta
229229
// Verify the api client is configured
230230
if (this.canPollOrPost()) {
231231
await fcn(this, this.config.deviceId || null)
232+
.catch((e) => {
233+
console.log(e)
234+
this.log('error', `Execute action failed: ${e.toString()}`)
235+
})
232236
.then(() => {
233237
// Do a poll asap, to catch the changes
234238
this.queuePoll()
235239
})
236-
.catch((e) => {
237-
this.log('error', `Execute action failed: ${e.toString()}`)
238-
})
239240
}
240241
}
241242

@@ -337,6 +338,7 @@ class SpotifyInstance extends InstanceBase<DeviceConfig> implements SpotifyInsta
337338
if (this.pollQueue.size > 1) {
338339
this.log('debug', `Poll queue overflow`)
339340
} else {
341+
this.log('debug', `queue poll`)
340342
this.pollQueue
341343
.add(async () => {
342344
// If everything is populated we can do the poll

0 commit comments

Comments
 (0)