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