Back to index

lightning-sunbird  0.9+nobinonly
nsScriptError.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Mike McCabe <mccabe@netscape.com>
00024  *   John Bandhauer <jband@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * nsIScriptError implementation.  Defined here, lacking a JS-specific
00042  * place to put XPCOM things.
00043  */
00044 
00045 #include "xpcprivate.h"
00046 
00047 NS_IMPL_THREADSAFE_ISUPPORTS2(nsScriptError, nsIConsoleMessage, nsIScriptError)
00048 
00049 nsScriptError::nsScriptError()
00050     :  mMessage(),
00051        mSourceName(),
00052        mLineNumber(0),
00053        mSourceLine(),
00054        mColumnNumber(0),
00055        mFlags(0),
00056        mCategory()
00057 {
00058 }
00059 
00060 nsScriptError::~nsScriptError() {}
00061 
00062 // nsIConsoleMessage methods
00063 NS_IMETHODIMP
00064 nsScriptError::GetMessage(PRUnichar **result) {
00065     nsresult rv;
00066 
00067     nsCAutoString message;
00068     rv = ToString(message);
00069     if (NS_FAILED(rv))
00070         return rv;
00071 
00072     *result = UTF8ToNewUnicode(message);
00073     if (!*result)
00074         return NS_ERROR_OUT_OF_MEMORY;
00075 
00076     return NS_OK;
00077 }
00078 
00079 // nsIScriptError methods
00080 NS_IMETHODIMP
00081 nsScriptError::GetErrorMessage(nsAString& aResult) {
00082     aResult.Assign(mMessage);
00083     return NS_OK;
00084 }
00085 
00086 NS_IMETHODIMP
00087 nsScriptError::GetSourceName(nsAString& aResult) {
00088     aResult.Assign(mSourceName);
00089     return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP
00093 nsScriptError::GetSourceLine(nsAString& aResult) {
00094     aResult.Assign(mSourceLine);
00095     return NS_OK;
00096 }
00097 
00098 NS_IMETHODIMP
00099 nsScriptError::GetLineNumber(PRUint32 *result) {
00100     *result = mLineNumber;
00101     return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 nsScriptError::GetColumnNumber(PRUint32 *result) {
00106     *result = mColumnNumber;
00107     return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP
00111 nsScriptError::GetFlags(PRUint32 *result) {
00112     *result = mFlags;
00113     return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 nsScriptError::GetCategory(char **result) {
00118     *result = ToNewCString(mCategory);
00119     return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 nsScriptError::Init(const PRUnichar *message,
00124                     const PRUnichar *sourceName,
00125                     const PRUnichar *sourceLine,
00126                     PRUint32 lineNumber,
00127                     PRUint32 columnNumber,
00128                     PRUint32 flags,
00129                     const char *category)
00130 {
00131     mMessage.Assign(message);
00132     mSourceName.Assign(sourceName);
00133     mLineNumber = lineNumber;
00134     mSourceLine.Assign(sourceLine);
00135     mColumnNumber = columnNumber;
00136     mFlags = flags;
00137     mCategory.Assign(category);
00138 
00139     return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP
00143 nsScriptError::ToString(nsACString& /*UTF8*/ aResult)
00144 {
00145     static const char format0[] =
00146         "[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]";
00147     static const char format1[] =
00148         "[%s: \"%s\" {file: \"%s\" line: %d}]";
00149     static const char format2[] =
00150         "[%s: \"%s\"]";
00151 
00152     static const char error[]   = "JavaScript Error";
00153     static const char warning[] = "JavaScript Warning";
00154 
00155     const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning;
00156 
00157     char* temp;
00158     char* tempMessage = nsnull;
00159     char* tempSourceName = nsnull;
00160     char* tempSourceLine = nsnull;
00161 
00162     if(!mMessage.IsEmpty())
00163         tempMessage = ToNewUTF8String(mMessage);
00164     if(!mSourceName.IsEmpty())
00165         tempSourceName = ToNewUTF8String(mSourceName);
00166     if(!mSourceLine.IsEmpty())
00167         tempSourceLine = ToNewUTF8String(mSourceLine);
00168 
00169     if(nsnull != tempSourceName && nsnull != tempSourceLine)
00170         temp = JS_smprintf(format0,
00171                            severity,
00172                            tempMessage,
00173                            tempSourceName,
00174                            mLineNumber,
00175                            mColumnNumber,
00176                            tempSourceLine);
00177     else if(!mSourceName.IsEmpty())
00178         temp = JS_smprintf(format1,
00179                            severity,
00180                            tempMessage,
00181                            tempSourceName,
00182                            mLineNumber);
00183     else
00184         temp = JS_smprintf(format2,
00185                            severity,
00186                            tempMessage);
00187 
00188     if(nsnull != tempMessage)
00189         nsMemory::Free(tempMessage);
00190     if(nsnull != tempSourceName)
00191         nsMemory::Free(tempSourceName);
00192     if(nsnull != tempSourceLine)
00193         nsMemory::Free(tempSourceLine);
00194 
00195     if (!temp)
00196         return NS_ERROR_OUT_OF_MEMORY;
00197 
00198     aResult.Assign(temp);
00199     JS_smprintf_free(temp);
00200     return NS_OK;
00201 }