Skip to content

Commit

Permalink
feat(debug): implement debug utility
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Sep 23, 2018
1 parent 10007b3 commit 35957b6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
105 changes: 100 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@commitlint/config-angular": "^7.0.1",
"@types/chai": "^4.1.5",
"@types/chai-subset": "^1.3.1",
"@types/debug": "0.0.30",
"@types/mocha": "^5.2.5",
"@types/node": "^8.0.58",
"@types/sinon": "^5.0.2",
Expand Down Expand Up @@ -130,6 +131,7 @@
"@types/node": ">= 8.0.58"
},
"dependencies": {
"debug": "^4.0.1",
"follow-redirects": "^1.5.8",
"getroot": "^1.0.0",
"is-buffer": "^2.0.3",
Expand Down
5 changes: 5 additions & 0 deletions src/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as debug from 'debug';

const getDebug = (identifier: string) => debug(`oxid:${identifier}`);

export { getDebug };
5 changes: 5 additions & 0 deletions src/utils/parseJsonResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { XSSI_PREFIX } from './base';
import { getDebug } from './debug';

const debug = getDebug('parseJsonResponse');

/**
* Parse given body as json object if responseType is configured
Expand All @@ -9,6 +12,7 @@ const parseJsonResponse = (status: number, body: any) => {
// but a successful status code can still result in an error if the user
// asked for JSON data and the body cannot be parsed as such.
const ok = status >= 200 && status < 300;
debug(`Status '${status}' considered as '${ok ? 'ok' : 'not ok'}'`);

// Save the original body, before attempting XSSI prefix stripping.
const originalBody = body;
Expand All @@ -31,6 +35,7 @@ const parseJsonResponse = (status: number, body: any) => {
ret = { error, text: ret };
}

debug(`parse response body failed, returning original body with error`, error);
return { ok: false, body: ret };
}
};
Expand Down

0 comments on commit 35957b6

Please sign in to comment.