Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add endpoint logging ro server response
Browse files Browse the repository at this point in the history
Need to log endpoint url in case server status is not OK
Also updated response logging to multiline format
  • Loading branch information
Maksym Shykov committed May 17, 2021
1 parent 8825a3b commit 433d1c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/request-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const request = require('then-request');
const log = require('debug')('macpaw:qa-api-services');

module.exports = {
isSuccess(statusCode) {
isStatusCodeOk(statusCode) {
const regExp = new RegExp('^20[0-9]$');

return regExp.test(statusCode);
Expand All @@ -19,24 +19,36 @@ module.exports = {
async sendRequest(method, url, options) {
log(`Executing '${method}: ${url}' with options: %O`, options);
const response = await request(method, url, options);
log(`Server responded with status code: '${response.statusCode}' and body: ${response.body.toString()}'`);

return response;
},

getResponseBody(response) {
this.catchError(response);
const body = response.body.toString();

return body ? JSON.parse(body) : {};
},

catchError(response) {
const {statusCode} = response;

if (!this.isSuccess(statusCode)) {
const body = response.body.toString();
throw new Error(`Oops, the server responded with status code: '${statusCode}' and body: ${body}`);
handleResponse(response) {
const {
url,
statusCode,
body,
} = response;

if (!this.isStatusCodeOk(statusCode)) {
throw new Error(
`Oops!
Endpoint '${url}' responded with
status code: '${statusCode}'
body: ${body.toString()}`,
);
}

log(
`Endpoint '${url}' responded with
status code: '${statusCode}'
and body: %O`, this.getResponseBody(response),
);
},
};
5 changes: 5 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,35 @@ module.exports = {

get(url, options) {
const response = this.browser.call(() => requestHelper.sendRequest('GET', url, options));
requestHelper.handleResponse(response);

return requestHelper.getResponseBody(response);
},

post(url, options) {
const response = this.browser.call(() => requestHelper.sendRequest('POST', url, options));
requestHelper.handleResponse(response);

return requestHelper.getResponseBody(response);
},

patch(url, options) {
const response = this.browser.call(() => requestHelper.sendRequest('PATCH', url, options));
requestHelper.handleResponse(response);

return requestHelper.getResponseBody(response);
},

put(url, options) {
const response = this.browser.call(() => requestHelper.sendRequest('PUT', url, options));
requestHelper.handleResponse(response);

return requestHelper.getResponseBody(response);
},

del(url, options) {
const response = this.browser.call(() => requestHelper.sendRequest('DELETE', url, options));
requestHelper.handleResponse(response);

return requestHelper.getResponseBody(response);
},
Expand Down

0 comments on commit 433d1c8

Please sign in to comment.