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.
31 #ifndef ExceptionMessages_h
32 #define ExceptionMessages_h
34 #include "wtf/MathExtras.h"
35 #include "wtf/text/StringBuilder.h"
36 #include "wtf/text/WTFString.h"
42 class ExceptionMessages {
49 static String argumentNullOrIncorrectType(int argumentIndex, const String& expectedType);
50 static String constructorNotCallableAsFunction(const char* type);
52 static String failedToConstruct(const char* type, const String& detail);
53 static String failedToEnumerate(const char* type, const String& detail);
54 static String failedToExecute(const char* method, const char* type, const String& detail);
55 static String failedToGet(const char* property, const char* type, const String& detail);
56 static String failedToSet(const char* property, const char* type, const String& detail);
57 static String failedToDelete(const char* property, const char* type, const String& detail);
58 static String failedToGetIndexed(const char* type, const String& detail);
59 static String failedToSetIndexed(const char* type, const String& detail);
60 static String failedToDeleteIndexed(const char* type, const String& detail);
62 template <typename NumType>
63 static String formatNumber(NumType number)
65 return formatFiniteNumber(number);
68 static String incorrectPropertyType(const String& property, const String& detail);
70 template <typename NumberType>
71 static String indexExceedsMaximumBound(const char* name, NumberType given, NumberType bound)
73 bool eq = given == bound;
75 result.appendLiteral("The ");
77 result.appendLiteral(" provided (");
78 result.append(formatNumber(given));
79 result.appendLiteral(") is greater than ");
80 result.append(eq ? "or equal to " : "");
81 result.appendLiteral("the maximum bound (");
82 result.append(formatNumber(bound));
83 result.appendLiteral(").");
84 return result.toString();
87 template <typename NumberType>
88 static String indexExceedsMinimumBound(const char* name, NumberType given, NumberType bound)
90 bool eq = given == bound;
92 result.appendLiteral("The ");
94 result.appendLiteral(" provided (");
95 result.append(formatNumber(given));
96 result.appendLiteral(") is less than ");
97 result.append(eq ? "or equal to " : "");
98 result.appendLiteral("the minimum bound (");
99 result.append(formatNumber(bound));
100 result.appendLiteral(").");
101 return result.toString();
104 template <typename NumberType>
105 static String indexOutsideRange(const char* name, NumberType given, NumberType lowerBound, BoundType lowerType, NumberType upperBound, BoundType upperType)
107 StringBuilder result;
108 result.appendLiteral("The ");
110 result.appendLiteral(" provided (");
111 result.append(formatNumber(given));
112 result.appendLiteral(") is outside the range ");
113 result.append(lowerType == ExclusiveBound ? '(' : '[');
114 result.append(formatNumber(lowerBound));
115 result.appendLiteral(", ");
116 result.append(formatNumber(upperBound));
117 result.append(upperType == ExclusiveBound ? ')' : ']');
119 return result.toString();
122 static String invalidArity(const char* expected, unsigned provided);
124 // If > 0, the argument index that failed type check (1-indexed.)
125 // If == 0, a (non-argument) value (e.g., a setter) failed the same check.
126 static String notAnArrayTypeArgumentOrValue(int argumentIndex);
127 static String notASequenceTypeProperty(const String& propertyName);
128 static String notAFiniteNumber(double value, const char* name = "value provided");
129 static String notAFiniteNumber(const Decimal& value, const char* name = "value provided");
131 static String notEnoughArguments(unsigned expected, unsigned provided);
133 static String readOnly(const char* detail = 0);
136 template <typename NumType>
137 static String formatFiniteNumber(NumType number)
139 if (number > 1e20 || number < -1e20)
140 return String::format("%e", 1.0*number);
141 return String::number(number);
144 template <typename NumType>
145 static String formatPotentiallyNonFiniteNumber(NumType number)
147 if (std::isnan(number))
149 if (std::isinf(number))
150 return number > 0 ? "Infinity" : "-Infinity";
151 if (number > 1e20 || number < -1e20)
152 return String::format("%e", number);
153 return String::number(number);
156 static String ordinalNumber(int number);
159 template <> String ExceptionMessages::formatNumber<float>(float number);
160 template <> String ExceptionMessages::formatNumber<double>(double number);
164 #endif // ExceptionMessages_h