2

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

2 Answers 2

2

To get a printable message, you can use

error.toString();

Or to get the actual message,

error.message;

Docs

Sign up to request clarification or add additional context in comments.

1 Comment

I realize the problem is with JSON.stringify(error), as this is what I'm sending back to the client, and client never gets the string representation
0

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();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.