From 2b2a6fea3fe1f3231906c1a63a353777aba09038 Mon Sep 17 00:00:00 2001 From: Bill Heaton Date: Sun, 23 Aug 2015 21:21:46 -0700 Subject: [PATCH] FetchError passes original error or default message --- addon/adapters/application.js | 17 +++++++++++------ tests/unit/adapters/application-test.js | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/addon/adapters/application.js b/addon/adapters/application.js index 2562809..3e5c638 100644 --- a/addon/adapters/application.js +++ b/addon/adapters/application.js @@ -222,7 +222,8 @@ export default Ember.Object.extend(Ember.Evented, { }); } }).catch(function(error) { - reject(new FetchError('Unable to Fetch resource(s)', error)); + let msg = (error && error.message) ? error.message : 'Unable to Fetch resource(s)'; + reject(new FetchError(msg, error)); }); }); }, @@ -324,25 +325,29 @@ export default Ember.Object.extend(Ember.Evented, { }); function ServerError(message = 'Server Error', response = null) { - this.name = 'Server Error'; + this.name = 'ServerError'; this.message = message; this.response = response; + this.errors = response.errors || null; } ServerError.prototype = Object.create(Error.prototype); ServerError.prototype.constructor = ServerError; -function ClientError(message = 'API Error', response = null) { - this.name = 'API Error'; +function ClientError(message = 'Client Error', response = null) { + this.name = 'ClientError'; this.message = message; this.response = response; - this.errors = response.errors; + this.errors = response.errors || null; + this.errors = (response) ? response.errors || null : null; } ClientError.prototype = Object.create(Error.prototype); ClientError.prototype.constructor = ClientError; function FetchError(message = 'Fetch Error', error = null, response = null) { - this.name = 'Fetch Error'; + this.name = 'FetchError'; this.message = message; + this.stack = (error) ? error.stack || null : null; + this.error = error; this.response = response; } diff --git a/tests/unit/adapters/application-test.js b/tests/unit/adapters/application-test.js index d7ad5de..97b1b2b 100644 --- a/tests/unit/adapters/application-test.js +++ b/tests/unit/adapters/application-test.js @@ -310,7 +310,7 @@ test('serializer#deserializeIncluded called after successful fetch', function(as }); -test('#fetch handles 5xx (Server Error) response status', function(assert) { +test('#fetch handles 5xx (ServerError) response status', function(assert) { assert.expect(2); const done = assert.async(); const adapter = this.subject({type: 'posts', url: '/posts'}); @@ -320,7 +320,7 @@ test('#fetch handles 5xx (Server Error) response status', function(assert) { let promise = adapter.fetch('/posts', { method: 'POST', body: 'json string here' }); assert.ok(typeof promise.then === 'function', 'returns a thenable'); promise.catch(function(error) { - assert.equal(error.name, 'Server Error', '5xx response throws aa custom error'); + assert.equal(error.name, 'ServerError', '5xx response throws aa custom error'); done(); }); });