View Javadoc
1   /*
2   * Copyright 2019-2026 the original author or 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 org.bremersee.ldaptive;
18  
19  import java.util.Optional;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.ldaptive.LdapException;
23  
24  /**
25   * The abstract ldaptive error handler.
26   *
27   * @author Christian Bremer
28   */
29  public abstract class AbstractLdaptiveErrorHandler implements LdaptiveErrorHandler {
30  
31    private static final Log log = LogFactory.getLog(AbstractLdaptiveErrorHandler.class);
32  
33    /**
34     * Instantiates a new abstract ldaptive error handler.
35     */
36    protected AbstractLdaptiveErrorHandler() {
37      super();
38    }
39  
40    @Override
41    public void handleError(Throwable t) {
42      final LdaptiveException ldaptiveException;
43      if (t instanceof LdaptiveException le) {
44        ldaptiveException = le;
45      } else if (t instanceof LdapException le) {
46        ldaptiveException = map(le);
47      } else {
48        ldaptiveException = LdaptiveException.builder()
49            .reason(Optional.ofNullable(t)
50                .filter(Exception.class::isInstance)
51                .map(Throwable::getMessage)
52                .orElse("Unknown"))
53            .cause(t)
54            .build();
55      }
56      log.error("LDAP operation failed.", ldaptiveException);
57      throw ldaptiveException;
58    }
59  
60  }