javascript
1const util = require('util'); 2 3module.exports = function NotFound (type, message) { 4 Error.captureStackTrace(this, this.constructor); 5 this.name = this.constructor.name; 6 this.message = message; 7 this.userMessage = 'ユーザーが見つかりませんでした。'; 8 this.userTitle = 'エラー'; 9 this.type = type; 10 this.status = 400; 11}; 12 13util.inherits(module.exports, Error); 14
このようなコードでエラーオブジェクトを作ることができたのですが、
この場合、1ファイルに複数の関数を用意し下記のようなコードにするとエラーが発生してしまいます。
javascript
1const util = require('util'); 2 3module.exports.NotFound = (type, message) => { 4 Error.captureStackTrace(this, this.constructor); 5 this.name = this.constructor.name; 6 this.message = message; 7 this.userMessage = 'ユーザーが見つかりませんでした。'; 8 this.userTitle = 'エラー'; 9 this.type = type; 10 this.status = 400; 11}; 12 13module.exports.AlreadyRegistered = (type, message) => { 14 Error.captureStackTrace(this, this.constructor); 15 this.name = this.constructor.name; 16 this.message = message; 17 this.userMessage = 'ユーザーは既に登録されています。'; 18 this.userTitle = 'エラー'; 19 this.type = type; 20 this.status = 400; 21}; 22 23util.inherits(module.exports.NotFound, Error); 24util.inherits(module.exports.AlreadyRegistered, Error);
エラー
util.js:982 Object.setPrototypeOf(ctor.prototype, superCtor.prototype); ^ TypeError: Object.setPrototypeOf called on null or undefined at Function.setPrototypeOf (<anonymous>) at Object.inherits (util.js:982:10) at Object.<anonymous> (/Users/hoge/errors/UserNotFoundError.js:23:6) at Module._compile (module.js:624:30) at Object.Module._extensions..js (module.js:635:10) at Module.load (module.js:545:32) at tryModuleLoad (module.js:508:12) at Function.Module._load (module.js:500:3) at Module.require (module.js:568:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/hoge/models/user.js:9:27) at Module._compile (module.js:624:30) at Object.Module._extensions..js (module.js:635:10) at Module.load (module.js:545:32) at tryModuleLoad (module.js:508:12) at Function.Module._load (module.js:500:3)
このような場合、どうしたら良いのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/23 07:51
2017/09/23 07:59