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 int code;
061        private List<OperationOutcome> errors = new ArrayList<OperationOutcome>();
062         
063        public EFhirClientException(String message) {
064                super(message);
065        }
066        
067        public EFhirClientException(int code, String message, List<OperationOutcome> serverErrors) {
068                super(message);
069                if(serverErrors != null && serverErrors.size() > 0) {
070                        errors.addAll(serverErrors);
071                }
072        }
073
074        public EFhirClientException(Exception cause) {
075                super(cause);
076        }
077        
078        public EFhirClientException(int code, String message, Exception cause) {
079                super(message, cause);
080        }
081        
082        /**
083         * Generate EFhirClientException which include a message indicating the cause of the exception
084         * along with any OperationOutcome server error that may have resulted.
085         * 
086         * @param message
087         * @param serverError
088         */
089        public EFhirClientException(int code, String message, OperationOutcome serverError) {
090                super(message);
091                if(serverError != null) {
092                        errors.add(serverError);
093                }
094        }
095        
096        /**
097         * Generate EFhirClientException indicating the cause of the exception
098         * along with any OperationOutcome server error the server may have generated.
099         * 
100         * A default message of "One or more server side errors have occurred during this operation. Refer to e.getServerErrors() for additional details."
101         * will be returned to users.
102         * 
103         * @param serverError
104         */
105        public EFhirClientException(OperationOutcome serverError) {
106                super("Error on the server: "+serverError.getText().getDiv().allText()+". Refer to e.getServerErrors() for additional details.");
107                if(serverError != null) {
108                        errors.add(serverError);
109                }
110        }
111        
112        /**
113         * Method returns all OperationOutcome server errors that are 
114         * associated with this exception.
115         * 
116         * @return
117         */
118        public List<OperationOutcome> getServerErrors() {
119                return errors;
120        }
121        
122        /**
123         * Method returns true if exception contains server OperationOutcome errors in payload.
124         * 
125         * @return
126         */
127        public boolean hasServerErrors() {
128                return errors.size() > 0;
129        }
130
131  public int getCode() {
132    return code;
133  }
134
135}