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