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