Skip to content

Commit

Permalink
Merge pull request #9 from edx/douglashall/ARCH-775
Browse files Browse the repository at this point in the history
fix: log network errors as info
  • Loading branch information
Douglas Hall authored May 15, 2019
2 parents 30934f8 + 4e90687 commit 574d3ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/NewRelicLoggingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function fixErrorLength(error) {
}

class NewRelicLoggingService {
static logInfo(message) {
static logInfo(message, customAttributes = {}) {
if (process.env.NODE_ENV === 'development') {
console.log(message); // eslint-disable-line
}
if (window && typeof window.newrelic !== 'undefined') {
window.newrelic.addPageAction('INFO', { message });
window.newrelic.addPageAction('INFO', Object.assign({}, { message }, customAttributes));
}
}

Expand Down Expand Up @@ -54,6 +54,8 @@ class NewRelicLoggingService {
};
updatedCustomAttributes = Object.assign({}, responseAttributes, customAttributes);
processedError = new Error(`API request failed: ${error.response.status} ${responseAttributes.errorUrl} ${data}`);

this.logError(processedError, updatedCustomAttributes);
} else if (error.request) {
const { config, request } = error;
const errorMessage = request.responseText || error.message;
Expand All @@ -67,10 +69,12 @@ class NewRelicLoggingService {
errorData: errorMessage,
};
updatedCustomAttributes = Object.assign({}, requestAttributes, customAttributes);
processedError = new Error(`API request failed: ${request.status} ${requestMethod} ${requestUrl} ${errorMessage}`);
}

this.logError(processedError, updatedCustomAttributes);
this.logInfo(
`API request failed: ${request.status} ${requestMethod} ${requestUrl} ${errorMessage}`,
updatedCustomAttributes,
);
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/NewRelicLoggingService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ describe('logError', () => {
describe('logAPIErrorResponse', () => {
beforeEach(() => {
global.newrelic.noticeError.mockReset();
global.newrelic.addPageAction.mockReset();
});

it('calls New Relic client to log error when error has request object', () => {
it('calls New Relic client to log info when error has request object', () => {
const error = {
request: {
status: 400,
Expand All @@ -71,16 +72,16 @@ describe('logAPIErrorResponse', () => {
},
};
const message = `${error.request.status} ${error.config.method} ${error.request.responseURL} ${error.request.responseText}`;
const expectedError = new Error(`API request failed: ${message}`);
const expectedAttributes = {
message: `API request failed: ${message}`,
errorType: 'api-request-error',
errorStatus: error.request.status,
errorMethod: error.config.method,
errorUrl: error.request.responseURL,
errorData: error.request.responseText,
};
NewRelicLoggingService.logAPIErrorResponse(error);
expect(global.newrelic.noticeError).toHaveBeenCalledWith(expectedError, expectedAttributes);
expect(global.newrelic.addPageAction).toHaveBeenCalledWith('INFO', expectedAttributes);
});

it('calls New Relic client to log error when error has response object', () => {
Expand Down

0 comments on commit 574d3ee

Please sign in to comment.