Prefer throwing a new Error object instead of just a string expression

This commit is contained in:
kleinfreund 2018-06-05 16:43:45 +02:00 committed by Sam
parent 0997eb6486
commit 4ce1d230c7
19 changed files with 35 additions and 29 deletions

View File

@ -127,7 +127,7 @@ export default Ember.Service.extend({
if (result.deleted) { if (result.deleted) {
resolve(); resolve();
} else { } else {
throw 'failed to delete'; throw new Error('failed to delete');
} }
}).catch(() => { }).catch(() => {
bootbox.alert(I18n.t("admin.user.delete_failed")); bootbox.alert(I18n.t("admin.user.delete_failed"));

View File

@ -20,7 +20,7 @@ export function Result(payload, responseJson) {
// We use this to make sure 404s are caught // We use this to make sure 404s are caught
function rethrow(error) { function rethrow(error) {
if (error.status === 404) { if (error.status === 404) {
throw "404: " + error.responseText; throw new Error("404: " + error.responseText);
} }
throw(error); throw(error);
} }

View File

@ -138,7 +138,7 @@ class Toolbar {
addButton(button) { addButton(button) {
const g = this.groups.findBy('group', button.group); const g = this.groups.findBy('group', button.group);
if (!g) { if (!g) {
throw `Couldn't find toolbar group ${button.group}`; throw new Error(`Couldn't find toolbar group ${button.group}`);
} }
const createdButton = { const createdButton = {

View File

@ -590,7 +590,7 @@ export default Ember.Controller.extend({
if (!opts.draftKey) { if (!opts.draftKey) {
alert("composer was opened without a draft key"); alert("composer was opened without a draft key");
throw "composer opened without a proper draft key"; throw new Error("composer opened without a proper draft key");
} }
const self = this; const self = this;
let composerModel = this.get('model'); let composerModel = this.get('model');

View File

@ -77,8 +77,8 @@ export default Ember.Controller.extend(PasswordValidation, UsernameValidation, N
this.set('errorMessage', result.message); this.set('errorMessage', result.message);
} }
} }
}).catch(response => { }).catch(error => {
throw response; throw new Error(error);
}); });
} }
} }

View File

@ -67,8 +67,8 @@ export default Ember.Controller.extend(PasswordValidation, {
this.set('errorMessage', result.message); this.set('errorMessage', result.message);
} }
} }
}).catch(response => { }).catch(error => {
throw response; throw new Error(error);
}); });
}, },

View File

@ -22,7 +22,7 @@ export function resetCustomPostMessageCallbacks() {
export function registerCustomPostMessageCallback(type, callback) { export function registerCustomPostMessageCallback(type, callback) {
if (customPostMessageCallbacks[type]) { if (customPostMessageCallbacks[type]) {
throw `Error ${type} is an already registered post message!`; throw new Error(`Error ${type} is an already registered post message!`);
} }
customPostMessageCallbacks[type] = callback; customPostMessageCallbacks[type] = callback;

View File

@ -82,7 +82,7 @@ export default function loadScript(url, opts) {
// option. // option.
if (opts.scriptTag) { if (opts.scriptTag) {
if (Ember.testing) { if (Ember.testing) {
throw `In test mode scripts cannot be loaded async ${cdnUrl}`; throw new Error(`In test mode scripts cannot be loaded async ${cdnUrl}`);
} }
loadWithTag(cdnUrl, cb); loadWithTag(cdnUrl, cb);
} else { } else {

View File

@ -61,7 +61,7 @@ const Badge = RestModel.extend({
self.updateFromJson(json); self.updateFromJson(json);
return self; return self;
}).catch(function(error) { }).catch(function(error) {
throw error; throw new Error(error);
}); });
}, },

View File

@ -506,8 +506,8 @@ const Composer = RestModel.extend({
} }
if (opts.action === REPLY && isEdit(this.get('action'))) this.set('reply', ''); if (opts.action === REPLY && isEdit(this.get('action'))) this.set('reply', '');
if (!opts.draftKey) throw 'draft key is required'; if (!opts.draftKey) throw new Error('draft key is required');
if (opts.draftSequence === null) throw 'draft sequence is required'; if (opts.draftSequence === null) throw new Error('draft sequence is required');
this.setProperties({ this.setProperties({
draftKey: opts.draftKey, draftKey: opts.draftKey,
@ -668,7 +668,7 @@ const Composer = RestModel.extend({
this.clearState(); this.clearState();
return result; return result;
}).catch(error => { }).catch(error => {
throw error; throw new Error(error);
}); });
}).catch(rollback); }).catch(rollback);
}, },

View File

@ -217,7 +217,7 @@ export default RestModel.extend({
this.setProperties({ loadingFilter: false, timelineLookup: json.timeline_lookup, loaded: true }); this.setProperties({ loadingFilter: false, timelineLookup: json.timeline_lookup, loaded: true });
}).catch(result => { }).catch(result => {
this.errorLoading(result); this.errorLoading(result);
throw result; throw new Error(result);
}).finally(() => { }).finally(() => {
this.set('loadingNearPost', null); this.set('loadingNearPost', null);
}); });

View File

@ -273,10 +273,10 @@ const Post = RestModel.extend({
.then(function(result){ .then(function(result){
self.set("topic.bookmarked", result.topic_bookmarked); self.set("topic.bookmarked", result.topic_bookmarked);
}) })
.catch(function(e) { .catch(function(error) {
self.toggleProperty("bookmarked"); self.toggleProperty("bookmarked");
if (bookmarkedTopic) {self.set("topic.bookmarked", false); } if (bookmarkedTopic) {self.set("topic.bookmarked", false); }
throw e; throw new Error(error);
}); });
}, },

View File

@ -44,7 +44,9 @@ const RestModel = Ember.Object.extend({
const self = this; const self = this;
self.set('isSaving', true); self.set('isSaving', true);
return adapter.createRecord(store, type, props).then(function(res) { return adapter.createRecord(store, type, props).then(function(res) {
if (!res) { throw "Received no data back from createRecord"; } if (!res) {
throw new Error("Received no data back from createRecord");
}
// We can get a response back without properties, for example // We can get a response back without properties, for example
// when a post is queued. // when a post is queued.
@ -59,7 +61,7 @@ const RestModel = Ember.Object.extend({
}, },
createProperties() { createProperties() {
throw "You must overwrite `createProperties()` before saving a record"; throw new Error("You must overwrite `createProperties()` before saving a record");
}, },
save(props) { save(props) {

View File

@ -302,10 +302,14 @@ export default Ember.Object.extend({
}, },
_hydrate(type, obj, root) { _hydrate(type, obj, root) {
if (!obj) { throw "Can't hydrate " + type + " of `null`"; } if (!obj) {
throw new Error("Can't hydrate " + type + " of `null`");
}
const id = obj.id; const id = obj.id;
if (!id) { throw "Can't hydrate " + type + " without an `id`"; } if (!id) {
throw new Error("Can't hydrate " + type + " without an `id`");
}
root = root || obj; root = root || obj;

View File

@ -652,7 +652,7 @@ function moveResult(result) {
flushMap(); flushMap();
return result; return result;
} }
throw "error moving posts topic"; throw new Error("error moving posts topic");
} }
export function movePosts(topicId, data) { export function movePosts(topicId, data) {

View File

@ -179,9 +179,9 @@ export default class Widget {
if (Discourse.Environment === "development" || Ember.testing) { if (Discourse.Environment === "development" || Ember.testing) {
const ds = this.defaultState(attrs); const ds = this.defaultState(attrs);
if (typeof ds !== "object") { if (typeof ds !== "object") {
throw `defaultState must return an object`; throw new Error(`defaultState must return an object`);
} else if (Object.keys(ds).length > 0 && !this.key) { } else if (Object.keys(ds).length > 0 && !this.key) {
throw `you need a key when using state in ${this.name}`; throw new Error(`you need a key when using state in ${this.name}`);
} }
} }
@ -279,7 +279,7 @@ export default class Widget {
result.dirtyKeys = this.dirtyKeys; result.dirtyKeys = this.dirtyKeys;
return result; return result;
} else { } else {
throw `Couldn't find ${widgetName} factory`; throw new Error(`Couldn't find ${widgetName} factory`);
} }
} }

View File

@ -91,7 +91,7 @@ self.addEventListener('fetch', function(event) {
if (!navigator.onLine) { if (!navigator.onLine) {
return caches.match(OFFLINE_URL); return caches.match(OFFLINE_URL);
} else { } else {
throw error; throw new Error(error);
} }
}) })
); );

View File

@ -49,7 +49,7 @@ export default Ember.Object.extend(ValidState, {
data: { fields } data: { fields }
}).catch(response => { }).catch(response => {
response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description)); response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
throw response; throw new Error(response);
}); });
} }
}); });

View File

@ -1,12 +1,12 @@
function checkSelectKitIsNotExpanded(selector) { function checkSelectKitIsNotExpanded(selector) {
if (find(selector).hasClass('is-expanded')) { if (find(selector).hasClass('is-expanded')) {
throw 'You expected select-kit to be collapsed but it is expanded.'; throw new Error('You expected select-kit to be collapsed but it is expanded.');
} }
} }
function checkSelectKitIsNotCollapsed(selector) { function checkSelectKitIsNotCollapsed(selector) {
if (!find(selector).hasClass('is-expanded')) { if (!find(selector).hasClass('is-expanded')) {
throw 'You expected select-kit to be expanded but it is collapsed.'; throw new Error('You expected select-kit to be expanded but it is collapsed.');
} }
} }