taku910/mecab

Failure initializing Tagger has no error message

polm opened this issue · 0 comments

polm commented

If you try to intialize a Tagger object with an output format that doesn't exist (like -Oasdf) then initialization will fail. On the command line this prints a reasonable error message, but if you call MeCab as a library (like in a wrapper from another language), getGlobalError returns an empty string. This can be confusing if the issue is a typo in the name of an output format or other minor issue.

The root cause of this issue is that when an invalid output format is used the global error message is set twice. First it's set here, in ModelImpl::open:

setGlobalError(error.c_str());

At this point everything is correct. But this function was called from createTagger, and when that sees that creating the Model failed it sets the error message again, this time to an empty string:

setGlobalError(tagger->what());

I first became aware of this issue due to trouble in mecab-python3.

An easy fix is to not to set the global error in createTagger. I'm not sure that's always correct, so it might be better to not set the global error if the new error string is empty - a check like that is in one version of setGlobalError already, but not the version normally used.