haimeili / language-detection

Automatically exported from code.google.com/p/language-detection
0 stars 0 forks source link

ErrorCode is not visible #49

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The enumeration (enum) ErrorCode, specified in the 
com.cybozu.labs.langdetect.LangDetectException.java file is not visible 
externally. Therefore, even if the exception has a getCode() method, this is 
practically useless, because you can not check against a well-read value. 

For instance, you can not write

package mypackage;

import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;

public class TestEnum {

     public static void main( String[] args ){

          try{
               DetectorFactory.loadProfile( "./profiles" );

               Detector detector = null;
               String text = "";
               detector = DetectorFactory.create();
               detector.append( text );
               String lang = detector.detect();
               System.out.println( "Language is: " + lang );
          }catch( LangDetectException lde ){
               if ( lde.getCode() == ErrorCode.CantDetectError ){
                    // ignore
               }
          }
     }
}

because this throw give a compilation error.

You can still check using .ordinal() on getCode(), i.e. 
(lde.getCode().ordinal()), however, using a switch (or multiple if) without 
knowing what each value represents (e.g. is value 1 a "CantDetectError" or a 
"InitParamError") is really a *bad* idea.

By the way, the ErrorCode "NoTextError" seems not to be used anywhere.

Suggested solution: Unless additional exceptions will be provided (say, one for 
each ErrorCode), so as to discriminate between them, I'd suggest declaring the 
ErrorCode in a separate file, as a public Enum.

Original issue reported on code.google.com by lebiat...@gmail.com on 23 Jan 2013 at 6:19