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) {
resolve();
} else {
throw 'failed to delete';
throw new Error('failed to delete');
}
}).catch(() => {
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
function rethrow(error) {
if (error.status === 404) {
throw "404: " + error.responseText;
throw new Error("404: " + error.responseText);
}
throw(error);
}

View File

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

View File

@ -590,7 +590,7 @@ export default Ember.Controller.extend({
if (!opts.draftKey) {
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;
let composerModel = this.get('model');

View File

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

View File

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

View File

@ -22,7 +22,7 @@ export function resetCustomPostMessageCallbacks() {
export function registerCustomPostMessageCallback(type, callback) {
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;

View File

@ -82,7 +82,7 @@ export default function loadScript(url, opts) {
// option.
if (opts.scriptTag) {
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);
} else {

View File

@ -61,7 +61,7 @@ const Badge = RestModel.extend({
self.updateFromJson(json);
return self;
}).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.draftKey) throw 'draft key is required';
if (opts.draftSequence === null) throw 'draft sequence is required';
if (!opts.draftKey) throw new Error('draft key is required');
if (opts.draftSequence === null) throw new Error('draft sequence is required');
this.setProperties({
draftKey: opts.draftKey,
@ -668,7 +668,7 @@ const Composer = RestModel.extend({
this.clearState();
return result;
}).catch(error => {
throw error;
throw new Error(error);
});
}).catch(rollback);
},

View File

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

View File

@ -273,10 +273,10 @@ const Post = RestModel.extend({
.then(function(result){
self.set("topic.bookmarked", result.topic_bookmarked);
})
.catch(function(e) {
.catch(function(error) {
self.toggleProperty("bookmarked");
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;
self.set('isSaving', true);
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
// when a post is queued.
@ -59,7 +61,7 @@ const RestModel = Ember.Object.extend({
},
createProperties() {
throw "You must overwrite `createProperties()` before saving a record";
throw new Error("You must overwrite `createProperties()` before saving a record");
},
save(props) {

View File

@ -302,10 +302,14 @@ export default Ember.Object.extend({
},
_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;
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;

View File

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

View File

@ -179,9 +179,9 @@ export default class Widget {
if (Discourse.Environment === "development" || Ember.testing) {
const ds = this.defaultState(attrs);
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) {
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;
return result;
} 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) {
return caches.match(OFFLINE_URL);
} else {
throw error;
throw new Error(error);
}
})
);

View File

@ -49,7 +49,7 @@ export default Ember.Object.extend(ValidState, {
data: { fields }
}).catch(response => {
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) {
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) {
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.');
}
}