user3610227
user3610227

Reputation:

how to display error details in nodejs

Sample console output from nodejs, I could access,

error.code

error.errno

error.sqlState

error.index

but how to access this string where it says "Column 'name' cannot be null

enter image description here

Upvotes: 2

Views: 2771

Answers (2)

years_of_no_light
years_of_no_light

Reputation: 958

error.message would do the trick

If your error.message = "ER_BAD_NULL_ERROR: Column 'name' cannot be null" and you are only interested in "Column 'name' cannot be null" you can create your own custom error class (for this particular sql errors) and make it return the later part of colon only. The logic would be somet hink like

if(error.sqlState !== undefined){
  // only do it for sql error
  throw new CustomSqlError(error);
}

and something like

function CustomSqlError(err){
  if(err && (err.sqlState!== undefined)){
    this.err = err;
  }
}

util.inherits(CustomSqlError, Error); // needs require("util");

CustomSqlError.prototype.getMsgWithutSQlCode = function(){
  if(typeof this.message == "string"){
    return (this.message.split(":"))[1].trim();
  }
}

Upvotes: 0

Ben Fortune
Ben Fortune

Reputation: 32127

To get a printable message, you can use

error.toString();

Or to get the actual message,

error.message;

Docs

Upvotes: 2

Related Questions