View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http;
29  
30  /**
31   * Signals a protocol exception due to failure to parse a message element.
32   *
33   * @since 4.0
34   */
35  public class ParseException extends ProtocolException {
36  
37      private static final long serialVersionUID = -7288819855864183578L;
38  
39      private final int errorOffset;
40  
41      /**
42       * Creates a {@link ParseException} without details.
43       */
44      public ParseException() {
45          super();
46          this.errorOffset = -1;
47      }
48  
49      /**
50       * Creates a {@link ParseException} with a detail message.
51       *
52       * @param message the exception detail message, or {@code null}
53       */
54      public ParseException(final String message) {
55          super(message);
56          this.errorOffset = -1;
57      }
58  
59      /**
60       * Creates a {@link ParseException} with parsing context details.
61       *
62       * @since 5.0
63       */
64      public ParseException(final String description, final CharSequence text, final int off, final int len, final int errorOffset) {
65          super(description +
66                  (errorOffset >= 0 ? "; error at offset " + errorOffset : "") +
67                  (text != null && len < 1024 ? ": <" + text.subSequence(off, off + len) + ">" : ""));
68          this.errorOffset = errorOffset;
69      }
70  
71      /**
72       * Creates a {@link ParseException} with parsing context details.
73       *
74       * @since 5.0
75       */
76      public ParseException(final String description, final CharSequence text, final int off, final int len) {
77          this(description, text, off, len, -1);
78      }
79  
80      /**
81       * @since 5.0
82       */
83      public int getErrorOffset() {
84          return errorOffset;
85      }
86  
87  }