Home | History | Annotate | Download | only in phonenumbers
      1 /*
      2  * Copyright (C) 2009 The Libphonenumber Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.i18n.phonenumbers;
     18 
     19 /**
     20  * Generic exception class for errors encountered when parsing phone numbers.
     21  * @author Lara Rennie
     22  */
     23 @SuppressWarnings("serial")
     24 public class NumberParseException extends Exception {
     25 
     26   public enum ErrorType {
     27     INVALID_COUNTRY_CODE,
     28     // This generally indicates the string passed in had less than 3 digits in it. More
     29     // specifically, the number failed to match the regular expression VALID_PHONE_NUMBER in
     30     // PhoneNumberUtil.java.
     31     NOT_A_NUMBER,
     32     // This indicates the string started with an international dialing prefix, but after this was
     33     // stripped from the number, had less digits than any valid phone number (including country
     34     // code) could have.
     35     TOO_SHORT_AFTER_IDD,
     36     // This indicates the string, after any country code has been stripped, had less digits than any
     37     // valid phone number could have.
     38     TOO_SHORT_NSN,
     39     // This indicates the string had more digits than any valid phone number could have.
     40     TOO_LONG,
     41   }
     42 
     43   private ErrorType errorType;
     44   private String message;
     45 
     46   public NumberParseException(ErrorType errorType, String message) {
     47     super(message);
     48     this.message = message;
     49     this.errorType = errorType;
     50   }
     51 
     52   /**
     53    * Returns the error type of the exception that has been thrown.
     54    */
     55   public ErrorType getErrorType() {
     56     return errorType;
     57   }
     58 
     59   @Override
     60   public String toString() {
     61     return "Error type: " + errorType + ". " + message;
     62   }
     63 }
     64