我想創建自己的異常。 所以我可以根據nodejs服務器運行的環境來格式化錯誤。當錯誤被拋出時,這是未定義的。自定義異常。這個undefined
(function() {
'use strict';
var states = require('../states');
var env = states.config.env;
var _ = require('underscore');
/**
* This is a error that is going to be thrown on server errors.
* The application format the message for the specific environment
* @param error The error
*/
var unrecoverableError = function (error) {
this.name = 'unrecoverableError';
this.message = _.isEqual(env, 'production') ? 'There was a server error. Please contact server admin' : error.toString();
this.code = 500;
};
unrecoverableError.prototype = Object.create(Error.prototype);
unrecoverableError.prototype.constructor = unrecoverableError;
module.exports = unrecoverableError;
}());
我也使用sequelize作爲ORM。
organisation.findOne({
where: {
name: organisationName
}
})
.then(function (organisation) {
if (_.isEmpty(organisation)) {
throw new modelNotFoundException();
} else {
resolve(organisation);
}
})
.catch(function (error) {
if (error instanceof modelNotFoundException) {
reject(error);
} else {
throw new unrecoverableError(error);
}
})
.catch(function (error) {
reject(error);
});
然後在我的控制檯中出現錯誤。
[類型錯誤:無法設置的未定義的屬性「名」]
我想不出我做錯了什麼。它在瀏覽器中工作。 這是一個工作小提琴的例子。 https://jsfiddle.net/y3gk0hos/
在此先感謝
我打電話給新的權利?或者我理解你錯了。 「拋出新的不可恢復的錯誤(錯誤);」 –
該解決方案的工作原理,但我不知道這是否是正確的解決方案。 –