All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.difi.vefa.peppol.common.util.ExceptionUtil Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
/*
 * Copyright 2015-2017 Direktoratet for forvaltning og IKT
 *
 * This source code is subject to dual licensing:
 *
 *
 * Licensed under the EUPL, Version 1.1 or – as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 *
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

package no.difi.vefa.peppol.common.util;

import no.difi.vefa.peppol.common.api.PerformAction;
import no.difi.vefa.peppol.common.api.PerformResult;
import no.difi.vefa.peppol.common.lang.PeppolRuntimeException;

public class ExceptionUtil {

    public static  void perform(Class cls, PerformAction action) throws E {
        try {
            action.action();
        } catch (Exception e) {
            throw prepareException(cls, e.getMessage(), e);
        }
    }

    public static  T perform(Class cls, PerformResult action) throws E {
        try {
            return action.action();
        } catch (Exception e) {
            throw prepareException(cls, e.getMessage(), e);
        }
    }

    public static  T perform(Class cls, String message, PerformResult action) throws E {
        try {
            return action.action();
        } catch (Exception e) {
            throw prepareException(cls, message, e);
        }
    }

    private static  E prepareException(Class cls, String message, Throwable throwable)
            throws E {
        try {
            return cls.getConstructor(String.class, Throwable.class).newInstance(message, throwable);
        } catch (Exception e) {
            throw new PeppolRuntimeException(String.format("Unable to initiate exception '%s'.", cls), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy