1 /* 2 * Copyright 2019-2022 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.exception; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import org.bremersee.exception.model.RestApiException; 22 import org.springframework.http.HttpHeaders; 23 import org.springframework.http.HttpStatusCode; 24 import org.springframework.util.FileCopyUtils; 25 26 /** 27 * A http response parser that creates a {@link RestApiException}. 28 * 29 * @author Christian Bremer 30 */ 31 public interface RestApiExceptionParser { 32 33 /** 34 * Parse exception. 35 * 36 * @param response the response 37 * @param httpStatus the http status 38 * @param headers the headers 39 * @return the parsed exception 40 */ 41 RestApiException parseException( 42 String response, 43 HttpStatusCode httpStatus, 44 HttpHeaders headers); 45 46 /** 47 * Parse exception. 48 * 49 * @param response the response 50 * @param httpStatus the http status 51 * @param headers the headers 52 * @return the parsed exception 53 */ 54 RestApiException parseException( 55 byte[] response, 56 HttpStatusCode httpStatus, 57 HttpHeaders headers); 58 59 /** 60 * Parse exception. 61 * 62 * @param response the response 63 * @param httpStatus the http status 64 * @param headers the headers 65 * @return the rest api exception 66 * @throws IOException the io exception 67 */ 68 default RestApiException parseException( 69 InputStream response, 70 HttpStatusCode httpStatus, 71 HttpHeaders headers) throws IOException { 72 73 return parseException(FileCopyUtils.copyToByteArray(response), httpStatus, headers); 74 } 75 76 }