001/*******************************************************************************
002 * Copyright (c) 2017, 2018 Red Hat Inc
003 * All rights reserved. This program and the accompanying materials
004 * are made available under the terms of the Eclipse Public License v1.0
005 * which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v10.html
007 *
008 * Contributors:
009 *     Jens Reimann - initial API and implementation
010 *******************************************************************************/
011package de.dentrassi.varlink.spi;
012
013import java.util.function.BiFunction;
014
015public final class Errors {
016
017    private Errors() {
018    }
019
020    public static void check(final CallResponse response) {
021        if (!response.isError()) {
022            return;
023        }
024
025        throw new CallErrorException(response);
026    }
027
028    public static void checkErrors(final CallResponse response,
029            final BiFunction<String, CallResponse, RuntimeException> errorMapper) {
030
031        if (!response.isError()) {
032            return;
033        }
034
035        final String errorName = response.getError();
036        if (errorName != null && errorMapper != null) {
037            final RuntimeException error = errorMapper.apply(errorName, response);
038            if (error != null) {
039                throw error;
040            }
041        }
042
043        throw new CallErrorException(response);
044    }
045
046}