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.impl.ByteUtil;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Implementation of ErrorHandler which is useful for generating debug
28   * information about bad row data (great for bug reports!).  After logging a
29   * debug entry for the failed column, it will return some sort of replacement
30   * value, see {@link ReplacementErrorHandler}.
31   * 
32   * @author James Ahlborn
33   * @usage _general_class_
34   */
35  public class DebugErrorHandler extends ReplacementErrorHandler
36  {
37    private static final Log LOG = LogFactory.getLog(DebugErrorHandler.class); 
38  
39    /**
40     * Constructs a DebugErrorHandler which replaces all errored values with
41     * {@code null}.
42     */
43    public DebugErrorHandler() {
44    }
45  
46    /**
47     * Constructs a DebugErrorHandler which replaces all errored values with the
48     * given Object.
49     */
50    public DebugErrorHandler(Object replacement) {
51      super(replacement);
52    }
53  
54    @Override
55    public Object handleRowError(Column column, byte[] columnData, 
56                                 Location location, Exception error)
57      throws IOException
58    {
59      if(LOG.isDebugEnabled()) {
60        LOG.debug("Failed reading column " + column + ", row " +
61                  location + ", bytes " +
62                  ((columnData != null) ?
63                   ByteUtil.toHexString(columnData) : "null"),
64                  error);
65      }
66  
67      return super.handleRowError(column, columnData, location, error);
68    }
69  
70  }