質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

解決済

1回答

3620閲覧

Javascriptで独自エラーオブジェクトを作りたい

rera

総合スコア109

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

0グッド

1クリップ

投稿2017/09/23 07:38

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)

このような場合、どうしたら良いのでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

アロー関数は、コンストラクタとして使うことはできません(MDN)。

ES6ならclass NotFound extends Errorのように、class構文で継承ができます。

投稿2017/09/23 07:44

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rera

2017/09/23 07:51

そうだったんですね...初めて知りました! ちなみに`class NotFound extends Error`を引数で受けれる形にするにはどうしたら良いのでしょうか...??
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問