View Javadoc
1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
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.healthmarketscience.jackcess.util;
18  
19  import java.io.IOException;
20  
21  import com.healthmarketscience.jackcess.Column;
22  import com.healthmarketscience.jackcess.Table;
23  
24  /**
25   * Handler for errors encountered while reading a column of row data from a
26   * Table.  An instance of this class may be configured at the Database, Table,
27   * or Cursor level to customize error handling as desired.  The default
28   * instance used is {@link #DEFAULT}, which just rethrows any exceptions
29   * encountered.
30   *
31   * @author James Ahlborn
32   * @usage _intermediate_class_
33   */
34  @FunctionalInterface
35  public interface ErrorHandler
36  {
37    /**
38     * default error handler used if none provided (just rethrows exception)
39     * @usage _general_field_
40     */
41    public static final ErrorHandleril/ErrorHandler.html#ErrorHandler">ErrorHandler DEFAULT = new ErrorHandler() {
42        @Override
43        public Object handleRowError(Column column, byte[] columnData,
44                                     Location location, Exception error)
45          throws IOException
46        {
47          // really can only be RuntimeException or IOException
48          if(error instanceof IOException) {
49            throw (IOException)error;
50          }
51          throw (RuntimeException)error;
52        }
53      };
54  
55    /**
56     * Handles an error encountered while reading a column of data from a Table
57     * row.  Handler may either throw an exception (which will be propagated
58     * back to the caller) or return a replacement for this row's column value
59     * (in which case the row will continue to be read normally).
60     *
61     * @param column the info for the column being read
62     * @param columnData the actual column data for the column being read (which
63     *                   may be {@code null} depending on when the exception
64     *                   was thrown during the reading process)
65     * @param location the current location of the error
66     * @param error the error that was encountered
67     *
68     * @return replacement for this row's column
69     */
70    public Object handleRowError(Column column,
71                                 byte[] columnData,
72                                 Location location,
73                                 Exception error)
74      throws IOException;
75  
76    /**
77     * Provides location information for an error.
78     */
79    public interface Location
80    {
81      /**
82       * @return the table in which the error occurred
83       */
84      public Table getTable();
85  
86      /**
87       * Contains details about the errored row, useful for debugging.
88       */
89      @Override
90      public String toString();
91    }
92  }