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, List<OperationOutcome> serverErrors) {
067    super(message);
068    this.code = code;
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    this.code = code;
081  }
082
083  /**
084   * Generate EFhirClientException which include a message indicating the cause of
085   * the exception along with any OperationOutcome server error that may have
086   * resulted.
087   * 
088   * @param message
089   * @param serverError
090   */
091  public EFhirClientException(int code, String message, OperationOutcome serverError) {
092    super(message);
093    this.code = code;
094    if (serverError != null) {
095      errors.add(serverError);
096    }
097  }
098
099  /**
100   * Generate EFhirClientException indicating the cause of the exception along
101   * 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
104   * this operation. Refer to e.getServerErrors() for additional details." will be
105   * returned to users.
106   * 
107   * @param serverError
108   */
109  public EFhirClientException(int code, OperationOutcome serverError) {
110    super("Error on the server: " + serverError.getText().getDiv().allText()
111        + ". Refer to e.getServerErrors() for additional details.");
112    this.code = code;
113    if (serverError != null) {
114      errors.add(serverError);
115    }
116  }
117
118  /**
119   * Method returns all OperationOutcome server errors that are associated with
120   * this exception.
121   * 
122   * @return
123   */
124  public List<OperationOutcome> getServerErrors() {
125    return errors;
126  }
127
128  /**
129   * Method returns true if exception contains server OperationOutcome errors in
130   * payload.
131   * 
132   * @return
133   */
134  public boolean hasServerErrors() {
135    return errors.size() > 0;
136  }
137
138  public int getCode() {
139    return code;
140  }
141
142}