Skip to content

Commit 06601bc

Browse files
committed
Merge pull request #39 from pixelhandler/fetch-error
FetchError passes original error or default message
2 parents 9761299 + 2b2a6fe commit 06601bc

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

addon/adapters/application.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ export default Ember.Object.extend(Ember.Evented, {
222222
});
223223
}
224224
}).catch(function(error) {
225-
reject(new FetchError('Unable to Fetch resource(s)', error));
225+
let msg = (error && error.message) ? error.message : 'Unable to Fetch resource(s)';
226+
reject(new FetchError(msg, error));
226227
});
227228
});
228229
},
@@ -324,25 +325,29 @@ export default Ember.Object.extend(Ember.Evented, {
324325
});
325326

326327
function ServerError(message = 'Server Error', response = null) {
327-
this.name = 'Server Error';
328+
this.name = 'ServerError';
328329
this.message = message;
329330
this.response = response;
331+
this.errors = response.errors || null;
330332
}
331333
ServerError.prototype = Object.create(Error.prototype);
332334
ServerError.prototype.constructor = ServerError;
333335

334-
function ClientError(message = 'API Error', response = null) {
335-
this.name = 'API Error';
336+
function ClientError(message = 'Client Error', response = null) {
337+
this.name = 'ClientError';
336338
this.message = message;
337339
this.response = response;
338-
this.errors = response.errors;
340+
this.errors = response.errors || null;
341+
this.errors = (response) ? response.errors || null : null;
339342
}
340343
ClientError.prototype = Object.create(Error.prototype);
341344
ClientError.prototype.constructor = ClientError;
342345

343346
function FetchError(message = 'Fetch Error', error = null, response = null) {
344-
this.name = 'Fetch Error';
347+
this.name = 'FetchError';
345348
this.message = message;
349+
this.stack = (error) ? error.stack || null : null;
350+
346351
this.error = error;
347352
this.response = response;
348353
}

tests/unit/adapters/application-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ test('serializer#deserializeIncluded called after successful fetch', function(as
310310
});
311311

312312

313-
test('#fetch handles 5xx (Server Error) response status', function(assert) {
313+
test('#fetch handles 5xx (ServerError) response status', function(assert) {
314314
assert.expect(2);
315315
const done = assert.async();
316316
const adapter = this.subject({type: 'posts', url: '/posts'});
@@ -320,7 +320,7 @@ test('#fetch handles 5xx (Server Error) response status', function(assert) {
320320
let promise = adapter.fetch('/posts', { method: 'POST', body: 'json string here' });
321321
assert.ok(typeof promise.then === 'function', 'returns a thenable');
322322
promise.catch(function(error) {
323-
assert.equal(error.name, 'Server Error', '5xx response throws aa custom error');
323+
assert.equal(error.name, 'ServerError', '5xx response throws aa custom error');
324324
done();
325325
});
326326
});

0 commit comments

Comments
 (0)