最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error object to string - Stack Overflow

programmeradmin0浏览0评论

I want to use regular expression for error message...

try {
  throw new Error("Foo 'bar'");
} catch (err) {
  console.log(getInQuotes(err));
}

... where getInQuotes is a function for string:

var getInQuotes = function(str) {
  var re;
  re = /'([^']+)'/g;
  return str.match(re);
};

... but got error:

Object Error: Foo 'bar' has no method 'match'

Although it works for usual string:

console.log(getInQuotes("Hello 'world'"));

result:

[ '\'world\'' ]

Tried to stringify Error object ...

console.log("stringify: " + JSON.stringify(err));

... but it's empty:

stringify: {}

I want to use regular expression for error message...

try {
  throw new Error("Foo 'bar'");
} catch (err) {
  console.log(getInQuotes(err));
}

... where getInQuotes is a function for string:

var getInQuotes = function(str) {
  var re;
  re = /'([^']+)'/g;
  return str.match(re);
};

... but got error:

Object Error: Foo 'bar' has no method 'match'

Although it works for usual string:

console.log(getInQuotes("Hello 'world'"));

result:

[ '\'world\'' ]

Tried to stringify Error object ...

console.log("stringify: " + JSON.stringify(err));

... but it's empty:

stringify: {}
Share Improve this question asked Jun 28, 2013 at 11:45 Maxim YefremovMaxim Yefremov 14.2k28 gold badges123 silver badges170 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You created an Error object, and that's not a string. But you can simply solve this by calling its toString method, and applying match on the result of that:

function getInQuotes(err) {
  var re;
  re = /'([^']+)'/g;
  return err.toString().match(re);
};

Just try this http://jsfiddle/B6gMS/1/

getInQuotes(err.message)

Basically, here we are trying to get the annotated string. if we go with Regular Expression then we need a right case if there is a real need for this.

if not, below string replace would be easier solution.

// Arrow Function read given string and strip quoted values.
// Basic example
const getInQuotes = (str) => str.replace( /^[^']*'|'.*/g, '' );

In order to keep it more generic. below function helps to keep this annotator (') can be configurable. below code is in latest ES2021 code.

  1. This uses template literals in RegExp Function.
  2. Annotator is configurable
  3. change the method name getInQuotes to getAnnotatedStrings which is meaningful.
  4. method return always array in order to keep it predictable and avoid other errors.
  5. Its good not to pass entire object since its requires only the error message.
function getAnnotatedStrings(errorMessage, annotator = "'") {
  if (!annotator || !errorMessage) return []

  const regex = new RegExp(`${annotator}([^']+)${annotator}`, 'g')
  return errorMessage.toString().match(regex);
}

Actual in ES2021 Code.

try {
  throw new Error("Foo 'bar'");
} catch (e) {
  console.log(getAnnotatedStrings(e?.message)); // outputs - bar
}

Refer: Error Object Intro
Regex Functions and Basics
Template Literals

err is not a string it's an Error object, so it has no .match() function . You Should call the function using Error object's toString() method, just this way:

try {
    throw new Error("Foo 'bar'");
} 
catch (err) {
    console.log(getInQuotes(err.toString())); 
}
发布评论

评论列表(0)

  1. 暂无评论