# Patterns for error messages

**URL:** <https://www.ruby-forum.com/t/patterns-for-error-messages/130694>\
**Category:** Ruby\
**Created:** [March 1, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694 "2008-03-01T23:41:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 1, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/1 "2008-03-01T23:41:13Z")

</div>

Hello all.

It seems that the question relates to interpreter “internals”, but I’m  
afraid of ask silly questions in ruby-core.

The question is: why error messages of interpreter has inconsistent  
addressing system of file and line?

There are at many slightly different schemes (and all of them can be  
present  
in the same error backtrace):

path/file.rb:10: error text  
path/file.rb:1:in `require': other-path/other-file.rb:31: error text path/file.rb:7:in`method’ error text  
^ from path/file.rb:12

And so on. I can’t invent one regexp for my output-capturing text editor  
(EditPlus on Windows) for catch all cases and automatically extract file  
and  
path for navigate me to this path.

Thanks for the answers.

v.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 2, 2008, 8:37am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/2 "2008-03-02T08:37:41Z")

</div>

Alle Saturday 01 March 2008, Victor ‘Zverok’ Shepelev ha scritto:

> v.

I can’t find the differences. If you want a regexp which gives the name  
of the  
file, I think you can simply use something like:

/^.\*(?=:\d+:)/

Stefano

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 12:08am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/3 "2008-03-03T00:08:43Z")

</div>

> > and path for navigate me to this path.

For 4 of my cases it would find, respectively:

1. path/file.rb:10: - OK
2. path/file.rb:1: - when I wanted “the real error” address -  
“other-path/other-file.rb:31:”
3. path/file.rb:7: - OK
4. nothing, because of there’s no last “:” in the string with “^ from”.  
Even  
if you delete last “:” from regexp, the result will be unsatisfactory: "  
^  
from path/file.rb:12" (There would be even more spaces, but you  
understand  
the idea)

It’s a pain.

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 9:43am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/4 "2008-03-03T09:43:05Z")

</div>

Alle Monday 03 March 2008, Victor ‘Zverok’ Shepelev ha scritto:

> > > and path for navigate me to this path.
> 
> It’s a pain.
> 
> V.

Yes, now I see what you mean. Well, for the second case, it depends on  
how  
your editor works exactly. If it has support for regexp groups, you can  
use  
the following regexp:

/^(?:._:\d+:in\s+`require’:\s+)?(._)(?=:\d+:)/

The name of the file is in the first group (this works also for the  
first  
case)

Regarding the last case, I think (but I may be wrong) that the third and  
last  
one are a single message, split in two lines for some reason. You should  
try  
to understand why this happens and act consequently. If you post the  
full  
error messages, maybe with a piece of code which generates them, I can  
try to  
help you more.

Stefano

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 10:23am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/5 "2008-03-03T10:23:27Z")

</div>

From: Stefano C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Monday, March 03, 2008 10:42 AM

> > > > The question is: why error messages of interpreter has inconsistent  
> > > > And so on. I can’t invent one regexp for my output-capturing text  
> > > > of
> > 
> > 1. nothing, because of there’s no last “:” in the string with “^ from”.  
> > your editor works exactly. If it has support for regexp groups, you can use  
> > the following regexp:
> 
> /^(?:._:\d+:in\s+`require’:\s+)?(._)(?=:\d+:)/
> 
> The name of the file is in the first group (this works also for the first  
> case)

OK, but this regexp works for the second case ONLY (unfortunately, I  
have  
only one “output pattern” setting, so, I am trying to invent the  
universal  
one).

> Regarding the last case, I think (but I may be wrong) that the third and  
> last  
> one are a single message, split in two lines for some reason. You should  
> try  
> to understand why this happens and act consequently. If you post the full  
> error messages, maybe with a piece of code which generates them, I can try  
> to  
> help you more.

Yes, it is like call stack: first line has something like case1-case3,  
other  
lines has something like case4.

For ex:

D:/!work/home/!stuff/ruby-tricks/test.rb:1:in `require’: ./tricks.rb:31:  
syntax error, unexpected ‘]’, expecting $end (SyntaxError)  
p 1,2,3].inject(0){|sum, n| sum + n}  
^ from D:/!work/home/!stuff/ruby-tricks/test.rb:1

And I’d be happy to browse all call-stack by clicking on output strings  
in  
“Script Output” window of my editor.

Most of compilers have “parsing-friendly” output, having the same  
“file:line: error” strings on each line.

Faithfully, I have a proposition to core team for thinking about it.

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 10:51am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/6 "2008-03-03T10:51:18Z")

</div>

From: Stefano C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Monday, March 03, 2008 11:41 AM

> > > help you more.  
> > > ^ from D:/!work/home/!stuff/ruby-tricks/test.rb:1
> 
> This should take care of all the four patterns:
> 
> /^(?:._:\d+:in\s+`require’:\s+|\s+^\s+from\s+)?(._)(?=:\d+)/

But not this one:

“./…/app/models/teachers.rb:7:in `initialize’: undefined local  
variable”

(not saying about that most of regexp engines too dumb for doing ?: and  
?=  
groups).

Maybe somebody from core team can comment on the question phrased as:  
Can  
the error messages form be more consistend? (Hmmm… Even I’m ready for  
trying to contribute, I think). Or this kind of questions should be  
asked in  
ruby-core?

V.

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 11:18am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/7 "2008-03-03T11:18:39Z")

</div>

Alle Monday 03 March 2008, Victor ‘Zverok’ Shepelev ha scritto:

> > This should take care of all the four patterns:
> > 
> > /^(?:._:\d+:in\s+`require’:\s+|\s+^\s+from\s+)?(._)(?=:\d+)/
> 
> But not this one:
> 
> “./…/app/models/teachers.rb:7:in `initialize’: undefined local variable”

I think this is a different matter: here, we’re in the first case (the  
simplest) and the regexp correctly gives:

“./…/app/models/teachers.rb”

This is the path of the file where the error occurs, relative to the  
directory  
where ruby was started. What would you like to get here?

> (not saying about that most of regexp engines too dumb for doing ?: and ?=  
> groups).

Are they? I didn’t try many editors (in particular, kate and vim on  
linux),  
but they all support them. At any rate, I think you can do without them.  
If  
you replace the initial (?: with a simple (, you’ll get the name of the  
file  
in the second group instead of the first. The look-ahead at the end can  
be  
removed by replacing (._) with [^:]_. which matche any character except  
the  
column. This has the side effect of not working with filenames  
containing ‘:’  
(this is the reason I used the look-ahead).

Stefano

---

<div class="post-metadata">

**Author:** ![Victor\_SZverokS\_She](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/46a35a/32.png) [@Victor\_SZverokS\_She](https://www.ruby-forum.com/u/Victor_SZverokS_She)\
**Post date:** [March 3, 2008, 10:43am UTC](https://www.ruby-forum.com/t/patterns-for-error-messages/130694/8 "2008-03-03T10:43:01Z")

</div>

Alle Monday 03 March 2008, Victor ‘Zverok’ Shepelev ha scritto:

> OK, but this regexp works for the second case ONLY (unfortunately, I have  
> only one “output pattern” setting, so, I am trying to invent the universal  
> one).

It should work for all the first three cases (at least, it does in ruby,  
which  
I’m using to test it).

> Yes, it is like call stack: first line has something like case1-case3, other  
> lines has something like case4.
> 
> For ex:
> 
> D:/!work/home/!stuff/ruby-tricks/test.rb:1:in `require’: ./tricks.rb:31:  
> syntax error, unexpected ‘]’, expecting $end (SyntaxError)  
> p 1,2,3].inject(0){|sum, n| sum + n}  
> ^ from D:/!work/home/!stuff/ruby-tricks/test.rb:1

This should take care of all the four patterns:

/^(?:._:\d+:in\s+`require’:\s+|\s+^\s+from\s+)?(._)(?=:\d+)/

Stefano
