Reputation:
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
Upvotes: 2
Views: 2771
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
Reputation: 32127
To get a printable message, you can use
error.toString();
Or to get the actual message,
error.message;
Upvotes: 2