I try to write my own HTTPError
object in JS. My code looks as follows
"use strict";
HTTPError.prototype = Object.create( Error.prototype );
HTTPError.prototype.constructor = HTTPError;
function HTTPError( msg, httpStatus, httpStatusText, fileName, lineNumber ) {
var _httpStatus = httpStatus;
var _httpStatusText = httpStatusText;
// Public and privileged methods
this.httpStatus = function() {
return _httpStatus;
}
this.httpStatusText = function() {
return _httpStatusText;
}
// C'tor starts here
Error.call( this, msg, fileName, lineNumber );
this.name = "HTTPError";
}
HTTPError.prototype.toString = function() {
var name = this.name;
name = (typeof name !== "string") ? 'HTTPError' : name;
var msg = this.message;
console.log( "This is msg: " + msg );
msg = (typeof msg !== "string") ? '' : msg;
var status = '(Status: ' + this.httpStatus() + ' - ' + this.httpStatusText() + ')';
if (name === '') {
return msg + ' ' + status;
}
if (msg === '') {
return name + ' ' + status;
}
return name + ': ' + msg + ' ' + status;
}
The method toString
was inspired by MDN Error.toString reference. The signature of the Error
-constructor is taken from MDN Error reference.
The problem is that the toString()
method never prints the error message. The line of code console.log( "This is msg: " + msg )
solely exists for debugging purposes. This line reveals that msg
is undefined, hence it is set to ''
. But why is it undefined? For some reason the message
property does not seem to be exist but I called the parent Error
constructor. What I am doing wrong?