• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.i18n.phonenumbers;
18 
19 /**
20  * Generic exception class for errors encountered when parsing phone numbers.
21  */
22 @SuppressWarnings("serial")
23 public class NumberParseException extends Exception {
24 
25   /**
26    * The reason that a string could not be interpreted as a phone number.
27    */
28   public enum ErrorType {
29     /**
30      * The country code supplied did not belong to a supported country or non-geographical entity.
31      */
32     INVALID_COUNTRY_CODE,
33     /**
34      * This generally indicates the string passed in had less than 3 digits in it. More
35      * specifically, the number failed to match the regular expression VALID_PHONE_NUMBER in
36      * PhoneNumberUtil.java.
37      */
38     NOT_A_NUMBER,
39     /**
40      * This indicates the string started with an international dialing prefix, but after this was
41      * stripped from the number, had less digits than any valid phone number (including country
42      * code) could have.
43      */
44     TOO_SHORT_AFTER_IDD,
45     /**
46      * This indicates the string, after any country code has been stripped, had less digits than any
47      * valid phone number could have.
48      */
49     TOO_SHORT_NSN,
50     /**
51      * This indicates the string had more digits than any valid phone number could have.
52      */
53     TOO_LONG,
54   }
55 
56   private ErrorType errorType;
57   private String message;
58 
NumberParseException(ErrorType errorType, String message)59   public NumberParseException(ErrorType errorType, String message) {
60     super(message);
61     this.message = message;
62     this.errorType = errorType;
63   }
64 
65   /**
66    * Returns the error type of the exception that has been thrown.
67    */
getErrorType()68   public ErrorType getErrorType() {
69     return errorType;
70   }
71 
72   @Override
toString()73   public String toString() {
74     return "Error type: " + errorType + ". " + message;
75   }
76 }
77