001package org.hl7.fhir.dstu2.utils.client; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030*/ 031 032import java.util.ArrayList; 033import java.util.List; 034 035import org.hl7.fhir.dstu2.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 message 104 * @param serverError 105 */ 106 public EFhirClientException(OperationOutcome serverError) { 107 super("Error on the server: " + serverError.getText().getDiv().allText() 108 + ". Refer to e.getServerErrors() for additional details."); 109 if (serverError != null) { 110 errors.add(serverError); 111 } 112 } 113 114 /** 115 * Method returns all OperationOutcome server errors that are associated with 116 * this exception. 117 * 118 * @return 119 */ 120 public List<OperationOutcome> getServerErrors() { 121 return errors; 122 } 123 124 /** 125 * Method returns true if exception contains server OperationOutcome errors in 126 * payload. 127 * 128 * @return 129 */ 130 public boolean hasServerErrors() { 131 return errors.size() > 0; 132 } 133 134}