Skip to content

Commit 324a044

Browse files
committed
cleanup feature service error handling
1 parent 3b565ed commit 324a044

File tree

3 files changed

+91
-24
lines changed

3 files changed

+91
-24
lines changed

core/client/app/services/feature.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const {
88
set
99
} = Ember;
1010

11+
const EmberError = Ember.Error;
12+
1113
export function feature(name) {
1214
return computed(`config.${name}`, `labs.${name}`, {
1315
get() {
@@ -22,7 +24,7 @@ export function feature(name) {
2224
});
2325
},
2426
set(key, value) {
25-
this.update(key, value).then((savedValue) => {
27+
return this.update(key, value).then((savedValue) => {
2628
return savedValue;
2729
});
2830
}
@@ -76,8 +78,13 @@ export default Service.extend({
7678
this.set('_settings', savedSettings);
7779
resolve(this._parseLabs(savedSettings).get(key));
7880
}).catch((errors) => {
79-
this.get('notifications').showErrors(errors);
80-
settings.rollbackAttributes();
81+
if (errors) { // model.save errors, show notifications
82+
this.get('notifications').showErrors(errors);
83+
settings.rollbackAttributes();
84+
} else {
85+
settings.rollbackAttributes();
86+
throw new EmberError(`Validation of the feature service settings model failed when updating labs.`);
87+
}
8188
resolve(this._parseLabs(settings)[key]);
8289
});
8390
}).catch(reject);

core/client/tests/integration/services/feature-test.js

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,43 @@ import Pretender from 'pretender';
66
import wait from 'ember-test-helpers/wait';
77
import FeatureService, {feature} from 'ghost/services/feature';
88
import Ember from 'ember';
9+
import { errorOverride, errorReset } from 'ghost/tests/helpers/adapter-error';
910

1011
const {merge, run} = Ember;
12+
const EmberError = Ember.Error;
13+
14+
function stubSettings(server, labs, validSave = true, validSettings = true) {
15+
let settings = [
16+
{
17+
id: '1',
18+
type: 'blog',
19+
key: 'labs',
20+
value: JSON.stringify(labs)
21+
}
22+
];
23+
24+
if (validSettings) {
25+
settings.push({
26+
id: '2',
27+
type: 'blog',
28+
key: 'postsPerPage',
29+
value: 1
30+
});
31+
}
1132

12-
function stubSettings(server, labs) {
1333
server.get('/ghost/api/v0.1/settings/', function () {
14-
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings: [
15-
{
16-
id: '1',
17-
type: 'blog',
18-
key: 'labs',
19-
value: JSON.stringify(labs)
20-
},
21-
// postsPerPage is needed to satisfy the validation
22-
{
23-
id: '2',
24-
type: 'blog',
25-
key: 'postsPerPage',
26-
value: 1
27-
}
28-
]})];
34+
return [200, {'Content-Type': 'application/json'}, JSON.stringify({settings})];
2935
});
3036

3137
server.put('/ghost/api/v0.1/settings/', function (request) {
32-
return [200, {'Content-Type': 'application/json'}, request.requestBody];
38+
let statusCode = (validSave) ? 200 : 400;
39+
let response = (validSave) ? request.requestBody : JSON.stringify({
40+
errors: [{
41+
message: 'Test Error'
42+
}]
43+
});
44+
45+
return [statusCode, {'Content-Type': 'application/json'}, response];
3346
});
3447
}
3548

@@ -182,15 +195,63 @@ describeModule(
182195
return wait().then(() => {
183196
expect(server.handlers[1].numberOfCalls).to.equal(1);
184197

185-
// TODO: failing because service.update only sets values on
186198
service.get('testFlag').then((testFlag) => {
187199
expect(testFlag).to.be.true;
188200
done();
189201
});
190202
});
191203
});
192204

193-
it('notifies for server errors');
194-
it('notifies for validation errors');
205+
it('notifies for server errors', function (done) {
206+
stubSettings(server, {testFlag: false}, false);
207+
addTestFlag();
208+
209+
let service = this.subject();
210+
211+
run(() => {
212+
service.get('testFlag').then((testFlag) => {
213+
expect(testFlag).to.be.false;
214+
});
215+
});
216+
217+
run(() => {
218+
service.set('testFlag', true);
219+
});
220+
221+
return wait().then(() => {
222+
expect(server.handlers[1].numberOfCalls).to.equal(1);
223+
224+
expect(service.get('notifications.notifications').length).to.equal(1);
225+
226+
service.get('testFlag').then((testFlag) => {
227+
expect(testFlag).to.be.false;
228+
done();
229+
});
230+
});
231+
});
232+
233+
it('notifies for validation errors', function (done) {
234+
stubSettings(server, {testFlag: false}, true, false);
235+
addTestFlag();
236+
237+
let service = this.subject();
238+
239+
run(() => {
240+
service.get('testFlag').then((testFlag) => {
241+
expect(testFlag).to.be.false;
242+
});
243+
});
244+
245+
run(() => {
246+
expect(() => {
247+
service.set('testFlag', true);
248+
}, EmberError, 'Threw validation error');
249+
});
250+
251+
service.get('testFlag').then((testFlag) => {
252+
expect(testFlag).to.be.false;
253+
done();
254+
});
255+
});
195256
}
196257
);

core/client/tests/unit/validators/tag-settings-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ describe('Unit: Validator: tag-settings', function () {
112112
nameErrors = tag.get('errors').errorsFor('name').get(0);
113113
expect(nameErrors.attribute, 'errors.name.attribute').to.equal('name');
114114
expect(nameErrors.message, 'errors.name.message').to.equal('Tag names can\'t start with commas.');
115-
expect(tag.get('errors.length')).to.equal(1);
116115

117116
expect(passed, 'passed').to.be.false;
118117
expect(tag.get('hasValidated'), 'hasValidated').to.include('name');

0 commit comments

Comments
 (0)