1 /*
2 * Copyright (C) 2013 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 are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32 #include "bindings/core/v8/ExceptionMessages.h"
33
34 #include "platform/Decimal.h"
35 #include "wtf/MathExtras.h"
36
37 namespace blink {
38
failedToConstruct(const char * type,const String & detail)39 String ExceptionMessages::failedToConstruct(const char* type, const String& detail)
40 {
41 return "Failed to construct '" + String(type) + (!detail.isEmpty() ? String("': " + detail) : String("'"));
42 }
43
failedToEnumerate(const char * type,const String & detail)44 String ExceptionMessages::failedToEnumerate(const char* type, const String& detail)
45 {
46 return "Failed to enumerate the properties of '" + String(type) + (!detail.isEmpty() ? String("': " + detail) : String("'"));
47 }
48
failedToExecute(const char * method,const char * type,const String & detail)49 String ExceptionMessages::failedToExecute(const char* method, const char* type, const String& detail)
50 {
51 return "Failed to execute '" + String(method) + "' on '" + String(type) + (!detail.isEmpty() ? String("': " + detail) : String("'"));
52 }
53
failedToGet(const char * property,const char * type,const String & detail)54 String ExceptionMessages::failedToGet(const char* property, const char* type, const String& detail)
55 {
56 return "Failed to read the '" + String(property) + "' property from '" + String(type) + "': " + detail;
57 }
58
failedToSet(const char * property,const char * type,const String & detail)59 String ExceptionMessages::failedToSet(const char* property, const char* type, const String& detail)
60 {
61 return "Failed to set the '" + String(property) + "' property on '" + String(type) + "': " + detail;
62 }
63
failedToDelete(const char * property,const char * type,const String & detail)64 String ExceptionMessages::failedToDelete(const char* property, const char* type, const String& detail)
65 {
66 return "Failed to delete the '" + String(property) + "' property from '" + String(type) + "': " + detail;
67 }
68
failedToGetIndexed(const char * type,const String & detail)69 String ExceptionMessages::failedToGetIndexed(const char* type, const String& detail)
70 {
71 return "Failed to read an indexed property from '" + String(type) + "': " + detail;
72 }
73
failedToSetIndexed(const char * type,const String & detail)74 String ExceptionMessages::failedToSetIndexed(const char* type, const String& detail)
75 {
76 return "Failed to set an indexed property on '" + String(type) + "': " + detail;
77 }
78
failedToDeleteIndexed(const char * type,const String & detail)79 String ExceptionMessages::failedToDeleteIndexed(const char* type, const String& detail)
80 {
81 return "Failed to delete an indexed property from '" + String(type) + "': " + detail;
82 }
83
constructorNotCallableAsFunction(const char * type)84 String ExceptionMessages::constructorNotCallableAsFunction(const char* type)
85 {
86 return failedToConstruct(type, "Please use the 'new' operator, this DOM object constructor cannot be called as a function.");
87 }
88
incorrectPropertyType(const String & property,const String & detail)89 String ExceptionMessages::incorrectPropertyType(const String& property, const String& detail)
90 {
91 return "The '" + property + "' property " + detail;
92 }
93
invalidArity(const char * expected,unsigned provided)94 String ExceptionMessages::invalidArity(const char* expected, unsigned provided)
95 {
96 return "Valid arities are: " + String(expected) + ", but " + String::number(provided) + " arguments provided.";
97 }
98
argumentNullOrIncorrectType(int argumentIndex,const String & expectedType)99 String ExceptionMessages::argumentNullOrIncorrectType(int argumentIndex, const String& expectedType)
100 {
101 return "The " + ordinalNumber(argumentIndex) + " argument provided is either null, or an invalid " + expectedType + " object.";
102 }
103
notAnArrayTypeArgumentOrValue(int argumentIndex)104 String ExceptionMessages::notAnArrayTypeArgumentOrValue(int argumentIndex)
105 {
106 String kind;
107 if (argumentIndex) // method argument
108 kind = ordinalNumber(argumentIndex) + " argument";
109 else // value, e.g. attribute setter
110 kind = "value provided";
111 return "The " + kind + " is neither an array, nor does it have indexed properties.";
112 }
113
notASequenceTypeProperty(const String & propertyName)114 String ExceptionMessages::notASequenceTypeProperty(const String& propertyName)
115 {
116 return "'" + propertyName + "' property is neither an array, nor does it have indexed properties.";
117 }
118
notEnoughArguments(unsigned expected,unsigned provided)119 String ExceptionMessages::notEnoughArguments(unsigned expected, unsigned provided)
120 {
121 return String::number(expected) + " argument" + (expected > 1 ? "s" : "") + " required, but only " + String::number(provided) + " present.";
122 }
123
notAFiniteNumber(double value,const char * name)124 String ExceptionMessages::notAFiniteNumber(double value, const char* name)
125 {
126 ASSERT(!std::isfinite(value));
127 return String::format("The %s is %s.", name, std::isinf(value) ? "infinite" : "not a number");
128 }
129
notAFiniteNumber(const Decimal & value,const char * name)130 String ExceptionMessages::notAFiniteNumber(const Decimal& value, const char* name)
131 {
132 ASSERT(!value.isFinite());
133 return String::format("The %s is %s.", name, value.isInfinity() ? "infinite" : "not a number");
134 }
135
ordinalNumber(int number)136 String ExceptionMessages::ordinalNumber(int number)
137 {
138 String suffix("th");
139 switch (number % 10) {
140 case 1:
141 if (number % 100 != 11)
142 suffix = "st";
143 break;
144 case 2:
145 if (number % 100 != 12)
146 suffix = "nd";
147 break;
148 case 3:
149 if (number % 100 != 13)
150 suffix = "rd";
151 break;
152 }
153 return String::number(number) + suffix;
154 }
155
readOnly(const char * detail)156 String ExceptionMessages::readOnly(const char* detail)
157 {
158 DEFINE_STATIC_LOCAL(String, readOnly, ("This object is read-only."));
159 return detail ? String::format("This object is read-only, because %s.", detail) : readOnly;
160 }
161
162 template <>
formatNumber(float number)163 String ExceptionMessages::formatNumber<float>(float number)
164 {
165 return formatPotentiallyNonFiniteNumber(number);
166 }
167
168 template <>
formatNumber(double number)169 String ExceptionMessages::formatNumber<double>(double number)
170 {
171 return formatPotentiallyNonFiniteNumber(number);
172 }
173
174 } // namespace blink
175