2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/ExceptionState.h"
34 #include "bindings/v8/ExceptionMessages.h"
35 #include "bindings/v8/V8ThrowException.h"
36 #include "core/dom/ExceptionCode.h"
40 void ExceptionState::clearException()
46 void ExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
50 ASSERT(!m_creationContext.IsEmpty());
52 // SecurityError is thrown via ::throwSecurityError, and _careful_ consideration must be given to the data exposed to JavaScript via the 'sanitizedMessage'.
53 ASSERT(ec != SecurityError);
56 String processedMessage = addExceptionContext(message);
57 m_message = processedMessage;
58 setException(V8ThrowException::createDOMException(ec, processedMessage, m_creationContext, m_isolate));
61 void ExceptionState::throwSecurityError(const String& sanitizedMessage, const String& unsanitizedMessage)
64 ASSERT(!m_creationContext.IsEmpty());
65 m_code = SecurityError;
66 String finalSanitized = addExceptionContext(sanitizedMessage);
67 m_message = finalSanitized;
68 String finalUnsanitized = addExceptionContext(unsanitizedMessage);
70 setException(V8ThrowException::createDOMException(SecurityError, finalSanitized, finalUnsanitized, m_creationContext, m_isolate));
73 void ExceptionState::setException(v8::Handle<v8::Value> exception)
75 // FIXME: Assert that exception is not empty?
76 if (exception.IsEmpty()) {
81 m_exception.set(m_isolate, exception);
84 void ExceptionState::throwException()
86 ASSERT(!m_exception.isEmpty());
87 V8ThrowException::throwError(m_exception.newLocal(m_isolate), m_isolate);
90 void ExceptionState::throwTypeError(const String& message)
95 setException(V8ThrowException::createTypeError(addExceptionContext(message), m_isolate));
98 void NonThrowableExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
100 ASSERT_NOT_REACHED();
105 void NonThrowableExceptionState::throwTypeError(const String& message)
107 ASSERT_NOT_REACHED();
112 void NonThrowableExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
114 ASSERT_NOT_REACHED();
115 m_code = SecurityError;
116 m_message = sanitizedMessage;
119 void TrackExceptionState::throwDOMException(const ExceptionCode& ec, const String& message)
125 void TrackExceptionState::throwTypeError(const String& message)
131 void TrackExceptionState::throwSecurityError(const String& sanitizedMessage, const String&)
133 m_code = SecurityError;
134 m_message = sanitizedMessage;
137 String ExceptionState::addExceptionContext(const String& message) const
139 if (message.isEmpty())
142 String processedMessage = message;
143 if (propertyName() && interfaceName() && m_context != UnknownContext) {
144 if (m_context == DeletionContext)
145 processedMessage = ExceptionMessages::failedToDelete(propertyName(), interfaceName(), message);
146 else if (m_context == ExecutionContext)
147 processedMessage = ExceptionMessages::failedToExecute(propertyName(), interfaceName(), message);
148 else if (m_context == GetterContext)
149 processedMessage = ExceptionMessages::failedToGet(propertyName(), interfaceName(), message);
150 else if (m_context == SetterContext)
151 processedMessage = ExceptionMessages::failedToSet(propertyName(), interfaceName(), message);
152 } else if (!propertyName() && interfaceName()) {
153 if (m_context == ConstructionContext)
154 processedMessage = ExceptionMessages::failedToConstruct(interfaceName(), message);
155 else if (m_context == EnumerationContext)
156 processedMessage = ExceptionMessages::failedToEnumerate(interfaceName(), message);
157 else if (m_context == IndexedDeletionContext)
158 processedMessage = ExceptionMessages::failedToDeleteIndexed(interfaceName(), message);
159 else if (m_context == IndexedGetterContext)
160 processedMessage = ExceptionMessages::failedToGetIndexed(interfaceName(), message);
161 else if (m_context == IndexedSetterContext)
162 processedMessage = ExceptionMessages::failedToSetIndexed(interfaceName(), message);
164 return processedMessage;
167 } // namespace WebCore