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

Include request/response info with Abort error #5639

Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion addon/adapters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ function ajaxError(adapter, payload, requestData, responseData) {
} else if (responseData.textStatus === 'timeout') {
error = new TimeoutError();
} else if (responseData.textStatus === 'abort' || responseData.status === 0) {
error = new AbortError();
error = handleAbort(requestData, responseData);
pixelhandler marked this conversation as resolved.
Show resolved Hide resolved
} else {
try {
error = adapter.handleResponse(
Expand All @@ -1245,6 +1245,15 @@ function ajaxError(adapter, payload, requestData, responseData) {
return error;
}

// Adapter abort error to include any relevent info, e.g. request/response:
function handleAbort(requestData, responseData) {
let { method, url, errorThrown } = requestData;
let { status } = responseData;
let msg = `Request failed: ${method} ${url} ${errorThrown || ''}`;
let errors = [{ title: 'Adapter Error', detail: msg.trim(), status }];
return new AbortError(errors);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having this error with the url, and error response if available would be much better.

Currently we only use new AbortError();

}

//From http://stackoverflow.com/questions/280634/endswith-in-javascript
function endsWith(string, suffix) {
if (typeof String.prototype.endsWith !== 'function') {
Expand Down
17 changes: 16 additions & 1 deletion tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2551,7 +2551,7 @@ test('gracefully handles exceptions in handleResponse where the ajax request err
});

test('treats status code 0 as an abort', function(assert) {
assert.expect(1);
assert.expect(3);

adapter._ajaxRequest = function(hash) {
hash.error({
Expand All @@ -2568,6 +2568,21 @@ test('treats status code 0 as an abort', function(assert) {
return run(() => {
return store.findRecord('post', '1').catch(err => {
assert.ok(err instanceof DS.AbortError, 'reason should be an instance of DS.AbortError');
assert.equal(
err.errors.length,
1,
'AbortError includes errors with request/response details'
);
let expectedError = {
title: 'Adapter Error',
detail: 'Request failed: GET /posts/1',
status: 0,
};
assert.deepEqual(
err.errors[0],
expectedError,
'method, url and, status are captured as details'
);
});
});
});
Expand Down