Skip to content

Commit 1a8776c

Browse files
committed
Debugger logging for invalid json response
1 parent 3db6648 commit 1a8776c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/request.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import colorize from "./util/colorize"
22
import { MiddlewareStack } from "./middleware-stack"
3-
import { ILogger, logger as defaultLogger } from "./logger"
3+
import { ILogger } from "./logger"
44
import { JsonapiResponseDoc, JsonapiRequestDoc } from "./jsonapi-spec"
55

66
export type RequestVerbs = keyof Request
@@ -60,6 +60,10 @@ export class Request {
6060
this.logger.debug(colorize("bold", JSON.stringify(responseJSON, null, 4)))
6161
}
6262

63+
private _logInvalidJSON(response : Response) : void {
64+
this.logger.debug(`Invalid Response JSON: ${response.clone().text()}`)
65+
}
66+
6367
private async _fetchWithLogging(
6468
url: string,
6569
options: RequestInit
@@ -111,9 +115,7 @@ export class Request {
111115
try {
112116
json = await response.clone().json()
113117
} catch (e) {
114-
if (response.body) {
115-
this.logger.debug(`Invalid Response JSON: ${response.text()}`)
116-
}
118+
this._logInvalidJSON(response)
117119

118120
throw new ResponseError(response, `invalid json: ${json}`, e)
119121
}
@@ -137,6 +139,7 @@ export class Request {
137139
throw new ResponseError(response, "record not found")
138140
} else {
139141
// Bad JSON, for instance an errors payload
142+
this._logInvalidJSON(response)
140143
throw new ResponseError(response, "invalid json")
141144
}
142145
}

0 commit comments

Comments
 (0)