001package org.hl7.fhir.dstu3.utils.client;
002
003
004
005
006
007import java.util.ArrayList;
008import java.util.List;
009
010/*
011  Copyright (c) 2011+, HL7, Inc.
012  All rights reserved.
013  
014  Redistribution and use in source and binary forms, with or without modification, 
015  are permitted provided that the following conditions are met:
016  
017   * Redistributions of source code must retain the above copyright notice, this 
018     list of conditions and the following disclaimer.
019   * Redistributions in binary form must reproduce the above copyright notice, 
020     this list of conditions and the following disclaimer in the documentation 
021     and/or other materials provided with the distribution.
022   * Neither the name of HL7 nor the names of its contributors may be used to 
023     endorse or promote products derived from this software without specific 
024     prior written permission.
025  
026  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
027  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
028  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
029  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
030  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
031  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
032  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
033  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
035  POSSIBILITY OF SUCH DAMAGE.
036  
037*/
038
039import org.hl7.fhir.dstu3.model.OperationOutcome;
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}