0

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?

user2690527
  • 1,729
  • 1
  • 22
  • 38
  • You're missing a major component here. In JS all objects are based from `Object`. You need to override Object. Read [The Description](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString) `carefully` – Edward J Beckett Dec 28 '14 at 17:49
  • @EddieB: No, the above is perfectly standard and normal JavaScript derivation with constructor fucntions. The problem is it just doesn't work with `Error`. – T.J. Crowder Dec 28 '14 at 17:52
  • @T.J.Crowder: What does "it just doesn't work with `Error`" mean? Do you want to tell me that `Error` does not behave like a normal JS object and therefore cannot be used as a parent object? – user2690527 Dec 28 '14 at 18:05
  • @user2690527 Please See [This Fiddle](http://jsfiddle.net/EdwardBeckett/sut5fvsc/2/) – Edward J Beckett Dec 28 '14 at 18:07
  • @T.J.Crowder what do you mean it doesn't work? It works fine when I run it. – Edward J Beckett Dec 28 '14 at 18:14
  • @EddieB: Sorry, I interpreted your choosing to `alert(HTTPError)` and it showing the code. (WHy would you do that rather than `alert(new HTTPError("We see this message"));`?!?!) Yes, that's the closest you can come, again see the linked question and its answers. It's not inheritance, but it can serve a lot of the same purpose. – T.J. Crowder Dec 28 '14 at 18:19
  • @user2690527 Working example in [JSFiddle](http://jsfiddle.net/EdwardBeckett/sut5fvsc/6/) – Edward J Beckett Dec 28 '14 at 18:20
  • @EddieB: Yes, or ***again*** in the answers on the linked question. No need whatsoever to recover this ground. – T.J. Crowder Dec 28 '14 at 18:21
  • @T.J.Crowder Yes... I see exactly what you mean now. The error is never 'extended' it's always of type 'Error.' – Edward J Beckett Dec 29 '14 at 04:07

0 Answers0