Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatRequestError #334

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/network-layer/default/RelayDefaultNetworkLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ function formatRequestErrors(
return errors.map(({locations, message}, ii) => {
var prefix = (ii + 1) + '. ';
var indent = ' '.repeat(prefix.length);
return (
prefix + message + '\n' +
locations.map(({column, line}) => {

//custom errors thrown in graphql-server may not have locations
var locationMessage = locations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style nit, but we prefer the following style for ternaries:

var locationMessage = locations ?
  foo :
  bar;

? ('\n' + locations.map(({column, line}) => {
var queryLine = queryLines[line - 1];
var offset = Math.min(column - 1, CONTEXT_BEFORE);
return [
queryLine.substr(column - 1 - offset, CONTEXT_LENGTH),
' '.repeat(offset) + '^^^'
].map(messageLine => indent + messageLine).join('\n');
}).join('\n')
}).join('\n'))
: '';

return (
prefix + message + locationMessage
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return prefix + message + locationMessage;

}).join('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,33 @@ describe('RelayDefaultNetworkLayer', () => {
].join('\n'));
expect(error.source).toEqual(response);
});

it('handles custom errors', () => {
var response = {
errors: [{
message: 'Something went wrong.'
}]
};

expect(fetch).not.toBeCalled();
networkLayer.sendMutation(request);
expect(fetch).toBeCalled();

fetch.mock.deferreds[0].resolve(genResponse(response));
jest.runAllTimers();

expect(rejectCallback.mock.calls.length).toBe(1);
var error = rejectCallback.mock.calls[0][0];
expect(error instanceof Error).toBe(true);
expect(error.message).toEqual([
'Server request for mutation \`FeedbackLikeMutation\` failed for the ' +
'following reasons:',
'',
'1. Something went wrong.'
].join('\n'));
expect(error.source).toEqual(response);
})

});

describe('sendQueries', () => {
Expand Down