15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
3521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * modification, are permitted provided that the following conditions are
681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * met:
7521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) *
881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * in the documentation and/or other materials provided with the
1381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * distribution.
1481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
1681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * this software without specific prior written permission.
17521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles) *
1881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2481a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
31e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#ifndef ExceptionMessages_h
32e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#define ExceptionMessages_h
330019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "wtf/MathExtras.h"
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "wtf/text/StringBuilder.h"
36e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include "wtf/text/WTFString.h"
370019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch
38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
39e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
4009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class Decimal;
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
42e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)class ExceptionMessages {
43e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)public:
4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    enum BoundType {
4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        InclusiveBound,
4609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        ExclusiveBound,
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    };
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String argumentNullOrIncorrectType(int argumentIndex, const String& expectedType);
5007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String constructorNotCallableAsFunction(const char* type);
5107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
5207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToConstruct(const char* type, const String& detail);
5307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToEnumerate(const char* type, const String& detail);
5407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToExecute(const char* method, const char* type, const String& detail);
5507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToGet(const char* property, const char* type, const String& detail);
5607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToSet(const char* property, const char* type, const String& detail);
5707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToDelete(const char* property, const char* type, const String& detail);
5807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToGetIndexed(const char* type, const String& detail);
5907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToSetIndexed(const char* type, const String& detail);
6007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String failedToDeleteIndexed(const char* type, const String& detail);
61d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
6207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    template <typename NumType>
6307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String formatNumber(NumType number)
6407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    {
6507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        return formatFiniteNumber(number);
6607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    }
67f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
6807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String incorrectPropertyType(const String& property, const String& detail);
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
70d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <typename NumberType>
71d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String indexExceedsMaximumBound(const char* name, NumberType given, NumberType bound)
72d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
73d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        bool eq = given == bound;
74d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        StringBuilder result;
759e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral("The ");
76d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(name);
779e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(" provided (");
78d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(given));
799e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(") is greater than ");
80d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(eq ? "or equal to " : "");
819e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral("the maximum bound (");
82d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(bound));
839e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(").");
84d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return result.toString();
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <typename NumberType>
88d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String indexExceedsMinimumBound(const char* name, NumberType given, NumberType bound)
89d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
90d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        bool eq = given == bound;
91d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        StringBuilder result;
929e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral("The ");
93d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(name);
949e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(" provided (");
95d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(given));
969e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(") is less than ");
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(eq ? "or equal to " : "");
989e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral("the minimum bound (");
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(bound));
1009e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(").");
101d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return result.toString();
102d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
103d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
104d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <typename NumberType>
105d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String indexOutsideRange(const char* name, NumberType given, NumberType lowerBound, BoundType lowerType, NumberType upperBound, BoundType upperType)
106d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
107d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        StringBuilder result;
1089e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral("The ");
109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(name);
1109e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(" provided (");
111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(given));
1129e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(") is outside the range ");
113d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(lowerType == ExclusiveBound ? '(' : '[');
114d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(lowerBound));
1159e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        result.appendLiteral(", ");
116d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(formatNumber(upperBound));
117d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append(upperType == ExclusiveBound ? ')' : ']');
118d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        result.append('.');
119d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return result.toString();
120d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
121d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
122d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    static String invalidArity(const char* expected, unsigned provided);
123d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
12407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // If  > 0, the argument index that failed type check (1-indexed.)
12507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // If == 0, a (non-argument) value (e.g., a setter) failed the same check.
12607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String notAnArrayTypeArgumentOrValue(int argumentIndex);
12707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String notASequenceTypeProperty(const String& propertyName);
12807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String notAFiniteNumber(double value, const char* name = "value provided");
12907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String notAFiniteNumber(const Decimal& value, const char* name = "value provided");
13009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
131e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    static String notEnoughArguments(unsigned expected, unsigned provided);
132d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
13307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String readOnly(const char* detail = 0);
134d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
13507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochprivate:
136d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <typename NumType>
137d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String formatFiniteNumber(NumType number)
138d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
139d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (number > 1e20 || number < -1e20)
140d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return String::format("%e", 1.0*number);
141d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return String::number(number);
142d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
143d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
144d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    template <typename NumType>
145d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static String formatPotentiallyNonFiniteNumber(NumType number)
146d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    {
147d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (std::isnan(number))
148d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return "NaN";
149d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (std::isinf(number))
150d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return number > 0 ? "Infinity" : "-Infinity";
151d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (number > 1e20 || number < -1e20)
152d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return String::format("%e", number);
153d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return String::number(number);
154d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
15507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
15607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    static String ordinalNumber(int number);
157521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)};
158e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
159d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)template <> String ExceptionMessages::formatNumber<float>(float number);
160d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)template <> String ExceptionMessages::formatNumber<double>(double number);
161d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
162c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
163e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
164e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#endif // ExceptionMessages_h
165