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