001package org.hl7.fhir.r4.utils.client; 002 003import java.util.ArrayList; 004import java.util.List; 005 006/* 007 Copyright (c) 2011+, HL7, Inc. 008 All rights reserved. 009 010 Redistribution and use in source and binary forms, with or without modification, 011 are permitted provided that the following conditions are met: 012 013 * Redistributions of source code must retain the above copyright notice, this 014 list of conditions and the following disclaimer. 015 * Redistributions in binary form must reproduce the above copyright notice, 016 this list of conditions and the following disclaimer in the documentation 017 and/or other materials provided with the distribution. 018 * Neither the name of HL7 nor the names of its contributors may be used to 019 endorse or promote products derived from this software without specific 020 prior written permission. 021 022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 023 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 024 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 025 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 026 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 027 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 028 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 029 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 030 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 031 POSSIBILITY OF SUCH DAMAGE. 032 033*/ 034 035import org.hl7.fhir.r4.model.OperationOutcome; 036 037/** 038 * FHIR client exception. 039 * 040 * FHIR API exception will be wrapped in FHIR client exceptions. 041 * OperationOutcome errors resulting from the server can be access by calling: 042 * 043 * <pre> 044 * <code> 045 * if(e.hasServerErrors()) { 046 * List<OperationOutcome> errors = e.getServerErrors(); 047 * //process errors... 048 * } 049 * </code> 050 * </pre> 051 * 052 * 053 * 054 * @author Claude Nanjo 055 * 056 */ 057public class EFhirClientException extends RuntimeException { 058 private static final long serialVersionUID = 1L; 059 private List<OperationOutcome> errors = new ArrayList<OperationOutcome>(); 060 061 public EFhirClientException(String message) { 062 super(message); 063 } 064 065 public EFhirClientException(String message, List<OperationOutcome> serverErrors) { 066 super(message); 067 if (serverErrors != null && serverErrors.size() > 0) { 068 errors.addAll(serverErrors); 069 } 070 } 071 072 public EFhirClientException(Exception cause) { 073 super(cause); 074 } 075 076 public EFhirClientException(String message, Exception cause) { 077 super(message, cause); 078 } 079 080 /** 081 * Generate EFhirClientException which include a message indicating the cause of 082 * the exception along with any OperationOutcome server error that may have 083 * resulted. 084 * 085 * @param message 086 * @param serverError 087 */ 088 public EFhirClientException(String message, OperationOutcome serverError) { 089 super(message); 090 if (serverError != null) { 091 errors.add(serverError); 092 } 093 } 094 095 /** 096 * Generate EFhirClientException indicating the cause of the exception along 097 * with any OperationOutcome server error the server may have generated. 098 * 099 * A default message of "One or more server side errors have occurred during 100 * this operation. Refer to e.getServerErrors() for additional details." will be 101 * returned to users. 102 * 103 * @param serverError 104 */ 105 public EFhirClientException(OperationOutcome serverError) { 106 super("Error on the server: " + serverError.getText().getDiv().allText() 107 + ". Refer to e.getServerErrors() for additional details."); 108 if (serverError != null) { 109 errors.add(serverError); 110 } 111 } 112 113 /** 114 * Method returns all OperationOutcome server errors that are associated with 115 * this exception. 116 * 117 * @return 118 */ 119 public List<OperationOutcome> getServerErrors() { 120 return errors; 121 } 122 123 /** 124 * Method returns true if exception contains server OperationOutcome errors in 125 * payload. 126 * 127 * @return 128 */ 129 public boolean hasServerErrors() { 130 return errors.size() > 0; 131 } 132 133}