Skip to content

Commit

Permalink
Debugger logging for invalid json response
Browse files Browse the repository at this point in the history
  • Loading branch information
wadetandy committed Aug 21, 2018
1 parent 3db6648 commit 1a8776c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import colorize from "./util/colorize"
import { MiddlewareStack } from "./middleware-stack"
import { ILogger, logger as defaultLogger } from "./logger"
import { ILogger } from "./logger"
import { JsonapiResponseDoc, JsonapiRequestDoc } from "./jsonapi-spec"

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

private _logInvalidJSON(response : Response) : void {
this.logger.debug(`Invalid Response JSON: ${response.clone().text()}`)
}

private async _fetchWithLogging(
url: string,
options: RequestInit
Expand Down Expand Up @@ -111,9 +115,7 @@ export class Request {
try {
json = await response.clone().json()
} catch (e) {
if (response.body) {
this.logger.debug(`Invalid Response JSON: ${response.text()}`)
}
this._logInvalidJSON(response)

throw new ResponseError(response, `invalid json: ${json}`, e)
}
Expand All @@ -137,6 +139,7 @@ export class Request {
throw new ResponseError(response, "record not found")
} else {
// Bad JSON, for instance an errors payload
this._logInvalidJSON(response)
throw new ResponseError(response, "invalid json")
}
}
Expand Down

0 comments on commit 1a8776c

Please sign in to comment.