001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.core.exception.service.remote;
025    
026    import microsoft.exchange.webservices.data.core.response.ServiceResponse;
027    import microsoft.exchange.webservices.data.core.enumeration.misc.error.ServiceError;
028    
029    /**
030     * Represents a remote service exception that has a single response.
031     */
032    public class ServiceResponseException extends ServiceRemoteException {
033    
034      /**
035       * Constant serialized ID used for compatibility.
036       */
037      private static final long serialVersionUID = 1L;
038    
039      /**
040       * Error details Value keys.
041       */
042      private static final String ExceptionClassKey = "ExceptionClass";
043    
044      /**
045       * The Exception message key.
046       */
047      private static final String ExceptionMessageKey = "ExceptionMessage";
048    
049      /**
050       * The Stack trace key.
051       */
052      private static final String StackTraceKey = "StackTrace";
053    
054      /**
055       * ServiceResponse when service operation failed remotely.
056       */
057      private ServiceResponse response;
058    
059      /**
060       * Initializes a new instance.
061       *
062       * @param response the response
063       */
064      public ServiceResponseException(ServiceResponse response) {
065        this.response = response;
066      }
067    
068      /**
069       * Gets the ServiceResponse for the exception.
070       *
071       * @return the response
072       */
073      public ServiceResponse getResponse() {
074        return response;
075      }
076    
077      /**
078       * Gets the service error code.
079       *
080       * @return the error code
081       */
082      public ServiceError getErrorCode() {
083        return this.response.getErrorCode();
084      }
085    
086      /**
087       * Gets a message that describes the current exception.
088       *
089       * @return The error message that explains the reason for the exception.
090       */
091    
092      public String getMessage() {
093    
094        // Bug E14:134792 -- Special case for Internal Server Error. If the
095        // server returned
096        // stack trace information, include it in the exception message.
097        if (this.response.getErrorCode() == ServiceError.ErrorInternalServerError) {
098          String exceptionClass;
099          String exceptionMessage;
100          String stackTrace;
101    
102          if (this.response.getErrorDetails().containsKey(ExceptionClassKey) &&
103              this.response.getErrorDetails().containsKey(
104                  ExceptionMessageKey) &&
105              this.response.getErrorDetails().containsKey(
106                  StackTraceKey)) {
107            exceptionClass = this.response.getErrorDetails().get(
108                ExceptionClassKey);
109            exceptionMessage = this.response.getErrorDetails().get(
110                ExceptionMessageKey);
111            stackTrace = this.response.getErrorDetails().get(StackTraceKey);
112    
113            // return
114            return String.format("%s -- Server Error: %s: %s %s", this.response
115                    .getErrorMessage(), exceptionClass,
116                exceptionMessage, stackTrace);
117          }
118        }
119    
120        return this.response.getErrorMessage();
121      }
122    }