• 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 indicates the string passed is not a valid number. Either the string had less than 3
35      * digits in it or had an invalid phone-context parameter. More specifically, the number failed
36      * to match the regular expression VALID_PHONE_NUMBER, RFC3966_GLOBAL_NUMBER_DIGITS, or
37      * RFC3966_DOMAINNAME in PhoneNumberUtil.java.
38      */
39     NOT_A_NUMBER,
40     /**
41      * This indicates the string started with an international dialing prefix, but after this was
42      * stripped from the number, had less digits than any valid phone number (including country
43      * code) could have.
44      */
45     TOO_SHORT_AFTER_IDD,
46     /**
47      * This indicates the string, after any country code has been stripped, had less digits than any
48      * valid phone number could have.
49      */
50     TOO_SHORT_NSN,
51     /**
52      * This indicates the string had more digits than any valid phone number could have.
53      */
54     TOO_LONG,
55   }
56 
57   private ErrorType errorType;
58   private String message;
59 
NumberParseException(ErrorType errorType, String message)60   public NumberParseException(ErrorType errorType, String message) {
61     super(message);
62     this.message = message;
63     this.errorType = errorType;
64   }
65 
66   /**
67    * Returns the error type of the exception that has been thrown.
68    */
getErrorType()69   public ErrorType getErrorType() {
70     return errorType;
71   }
72 
73   @Override
toString()74   public String toString() {
75     return "Error type: " + errorType + ". " + message;
76   }
77 }
78