1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30
31#if ENABLE(INDEXED_DATABASE)
32
33#include "IDBDatabaseException.h"
34
35namespace WebCore {
36
37static const struct IDBDatabaseExceptionNameDescription {
38    const char* const name;
39    const char* const description;
40    const ExceptionCode code;
41} idbDatabaseExceptions[] = {
42    // These are IDB-specific errors from the spec.
43    { "UnknownError", "An unknown error occurred within Indexed Database.", 0 },
44    { "ConstraintError", "A mutation operation in the transaction failed because a constraint was not satisfied.", 0 },
45    { "DataError", "The data provided does not meet requirements.", 0 },
46    { "TransactionInactiveError", "A request was placed against a transaction which is either currently not active, or which is finished.", 0 },
47    { "ReadOnlyError", "A write operation was attempted in a read-only transaction.", 0 },
48    { "VersionError", "An attempt was made to open a database using a lower version than the existing version.", 0 },
49
50    // These are IDB-specific descriptions of generic DOM Exceptions when they are thrown from IDB APIs
51    { "NotFoundError", "An operation failed because the requested database object could not be found.", NOT_FOUND_ERR },
52    { "InvalidStateError", "An operation was called on an object on which it is not allowed or at a time when it is not allowed.", INVALID_STATE_ERR },
53    { "InvalidAccessError", "An invalid operation was performed on an object.", INVALID_ACCESS_ERR },
54    { "AbortError", "The transaction was aborted, so the request cannot be fulfilled.", ABORT_ERR },
55    { "TimeoutError", "A lock for the transaction could not be obtained in a reasonable time.", TIMEOUT_ERR }, // FIXME: This isn't used yet.
56    { "QuotaExceededError", "The operation failed because there was not enough remaining storage space, or the storage quota was reached and the user declined to give more space to the database.", QUOTA_EXCEEDED_ERR },
57    { "SyntaxError", "The keypath argument contains an invalid key path.", SYNTAX_ERR },
58    { "DataCloneError", "The data being stored could not be cloned by the internal structured cloning algorithm.", DATA_CLONE_ERR },
59};
60
61static const IDBDatabaseExceptionNameDescription* getErrorEntry(ExceptionCode ec)
62{
63    if (ec < IDBDatabaseException::IDBDatabaseExceptionOffset || ec > IDBDatabaseException::IDBDatabaseExceptionMax)
64        return 0;
65
66    size_t tableSize = WTF_ARRAY_LENGTH(idbDatabaseExceptions);
67    size_t tableIndex = ec - IDBDatabaseException::UnknownError;
68
69    return tableIndex < tableSize ? &idbDatabaseExceptions[tableIndex] : 0;
70}
71
72bool IDBDatabaseException::initializeDescription(ExceptionCode ec, ExceptionCodeDescription* description)
73{
74    const IDBDatabaseExceptionNameDescription* entry = getErrorEntry(ec);
75    if (!entry)
76        return false;
77
78    description->typeName = "DOM IDBDatabase";
79    description->code = entry->code;
80    description->type = DOMCoreExceptionType;
81
82    description->name = entry ? entry->name : 0;
83    description->description = entry ? entry->description : 0;
84
85    return true;
86}
87
88String IDBDatabaseException::getErrorName(ExceptionCode ec)
89{
90    const IDBDatabaseExceptionNameDescription* entry = getErrorEntry(ec);
91    ASSERT(entry);
92    if (!entry)
93        return "UnknownError";
94
95    return entry->name;
96}
97
98String IDBDatabaseException::getErrorDescription(ExceptionCode ec)
99{
100    const IDBDatabaseExceptionNameDescription* entry = getErrorEntry(ec);
101    ASSERT(entry);
102    if (!entry)
103        return "Unknown error.";
104
105    return entry->description;
106}
107
108ExceptionCode IDBDatabaseException::getLegacyErrorCode(ExceptionCode ec)
109{
110    const IDBDatabaseExceptionNameDescription* entry = getErrorEntry(ec);
111    ASSERT(entry);
112
113    return (entry && entry->code) ? entry->code : ec - IDBDatabaseExceptionOffset;
114}
115
116} // namespace WebCore
117
118#endif // ENABLE(INDEXED_DATABASE)
119