Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.api; 16 : 17 : import com.google.common.base.Throwables; 18 : import com.google.gerrit.extensions.restapi.RestApiException; 19 : 20 : /** Static utilities for API implementations. */ 21 : public class ApiUtil { 22 : /** 23 : * Convert an exception encountered during API execution to a {@link RestApiException}. 24 : * 25 : * @param msg message to be used in the case where a new {@code RestApiException} is wrapped 26 : * around {@code e}. 27 : * @param e exception being handled. 28 : * @return {@code e} if it is already a {@code RestApiException}, otherwise a new {@code 29 : * RestApiException} wrapped around {@code e}. 30 : * @throws RuntimeException if {@code e} is a runtime exception, it is rethrown as-is. 31 : */ 32 : public static RestApiException asRestApiException(String msg, Exception e) 33 : throws RuntimeException { 34 69 : Throwables.throwIfUnchecked(e); 35 69 : return e instanceof RestApiException ? (RestApiException) e : RestApiException.wrap(msg, e); 36 : } 37 : 38 : private ApiUtil() {} 39 : }