Back to index

lightning-sunbird  0.9+nobinonly
nsSOAPException.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsSOAPException.h"
00039 #include "nsReadableUtils.h"
00040 #include "nsIXPConnect.h"
00041 
00042 nsSOAPException::nsSOAPException(nsresult aStatus, const nsAString & aName, 
00043                                  const nsAString & aMessage, nsIException* aInner) :
00044                                  mStatus(aStatus),mName(aName),mMessage(aMessage),
00045                                  mInner(aInner)
00046 {
00047   nsresult rc;
00048   nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rc));
00049   if(NS_SUCCEEDED(rc)) {
00050     xpc->GetCurrentJSStack(getter_AddRefs(mFrame));
00051   }
00052 }
00053 
00054 nsSOAPException::~nsSOAPException()
00055 {
00056 }
00057 
00058 NS_IMPL_ISUPPORTS1_CI(nsSOAPException, nsIException)
00059 
00060 /* readonly attribute string message; */
00061 NS_IMETHODIMP 
00062 nsSOAPException::GetMessage(char * *aMessage)
00063 {
00064   NS_ENSURE_ARG_POINTER(aMessage);
00065 
00066   *aMessage = ToNewUTF8String(mMessage);
00067   return NS_OK;
00068 }
00069 
00070 /* readonly attribute nsresult result; */
00071 NS_IMETHODIMP 
00072 nsSOAPException::GetResult(nsresult *aResult)
00073 {
00074   NS_ENSURE_ARG_POINTER(aResult);
00075 
00076   *aResult = mStatus;
00077   return NS_OK;
00078 }
00079 
00080 /* readonly attribute string name; */
00081 NS_IMETHODIMP 
00082 nsSOAPException::GetName(char * *aName)
00083 {
00084   NS_ENSURE_ARG_POINTER(aName);
00085 
00086   *aName = ToNewUTF8String(mName);
00087   return NS_OK;
00088 }
00089 
00090 /* readonly attribute string filename; */
00091 NS_IMETHODIMP 
00092 nsSOAPException::GetFilename(char * *aFilename)
00093 {
00094   NS_ENSURE_ARG_POINTER(aFilename);
00095   if (mFrame) {
00096     return mFrame->GetFilename(aFilename);
00097   }
00098 
00099   *aFilename = nsnull;
00100   return NS_OK;
00101 }
00102 
00103 /* readonly attribute PRUint32 lineNumber; */
00104 NS_IMETHODIMP 
00105 nsSOAPException::GetLineNumber(PRUint32 *aLineNumber)
00106 {
00107   NS_ENSURE_ARG_POINTER(aLineNumber);
00108   if (mFrame) {
00109     PRInt32 l = 0;
00110     mFrame->GetLineNumber(&l);
00111     *aLineNumber = (PRUint32)l;
00112     return NS_OK;
00113   }
00114 
00115   *aLineNumber = 0;
00116   return NS_OK;
00117 }
00118 
00119 /* readonly attribute PRUint32 columnNumber; */
00120 NS_IMETHODIMP 
00121 nsSOAPException::GetColumnNumber(PRUint32 *aColumnNumber)
00122 {
00123   NS_ENSURE_ARG_POINTER(aColumnNumber);
00124 
00125   *aColumnNumber = 0;
00126   return NS_OK;
00127 }
00128 
00129 /* readonly attribute nsIStackFrame location; */
00130 NS_IMETHODIMP 
00131 nsSOAPException::GetLocation(nsIStackFrame * *aLocation)
00132 {
00133   NS_ENSURE_ARG_POINTER(aLocation);
00134 
00135   *aLocation = mFrame;
00136   NS_IF_ADDREF(*aLocation);
00137   return NS_OK;
00138 }
00139 
00140 /* readonly attribute nsIException inner; */
00141 NS_IMETHODIMP 
00142 nsSOAPException::GetInner(nsIException * *aInner)
00143 {
00144   NS_ENSURE_ARG_POINTER(aInner);
00145 
00146   *aInner = mInner;
00147   NS_IF_ADDREF(*aInner);
00148   return NS_OK;
00149 }
00150 
00151 /* readonly attribute nsISupports data; */
00152 NS_IMETHODIMP 
00153 nsSOAPException::GetData(nsISupports * *aData)
00154 {
00155   NS_ENSURE_ARG_POINTER(aData);
00156 
00157   *aData = nsnull;
00158   return NS_OK;
00159 }
00160 
00161 /* string toString (); */
00162 NS_IMETHODIMP 
00163 nsSOAPException::ToString(char **_retval)
00164 {
00165   NS_ENSURE_ARG_POINTER(_retval);
00166   nsAutoString s;
00167   s.Append(mName);
00168   s.AppendLiteral(": ");
00169   s.Append(mMessage);
00170   if (mFrame) {
00171     char* str = nsnull;
00172     mFrame->ToString(&str);
00173     if (str) {
00174       s.AppendLiteral(", called by ");
00175       nsAutoString i;
00176       CopyASCIItoUCS2(nsDependentCString(str),i);
00177       nsMemory::Free(str);
00178       s.Append(i);
00179     }
00180   }
00181   if (mInner) {
00182     char* str = nsnull;
00183     mInner->ToString(&str);
00184     if (str) {
00185       nsAutoString i;
00186       CopyASCIItoUCS2(nsDependentCString(str),i);
00187       nsMemory::Free(str);
00188       s.AppendLiteral(", caused by ");
00189       s.Append(i);
00190     }
00191   }
00192 
00193   *_retval = ToNewUTF8String(s);
00194   return NS_OK;
00195 }
00196 
00197 nsresult nsSOAPException::AddException(nsresult aStatus, const nsAString & aName,
00198   const nsAString & aMessage,PRBool aClear)
00199 {
00200   nsCOMPtr<nsIExceptionService> xs =
00201     do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
00202   if (xs) {
00203     nsCOMPtr<nsIExceptionManager> xm;
00204     xs->GetCurrentExceptionManager(getter_AddRefs(xm));
00205     if (xm) {
00206       nsCOMPtr<nsIException> old;
00207       if (!aClear)
00208         xs->GetCurrentException(getter_AddRefs(old));
00209       nsCOMPtr<nsIException> exception = new nsSOAPException(aStatus, aName, 
00210         aMessage, old);
00211       if (exception) {
00212         xm->SetCurrentException(exception);
00213       }
00214     }
00215   }
00216   return aStatus;
00217 }