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