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/v8/ExceptionMessages.h"
33
34 #include "wtf/MathExtras.h"
35
36 namespace WebCore {
37
failedToConstruct(const String & type,const String & detail)38 String ExceptionMessages::failedToConstruct(const String& type, const String& detail)
39 {
40 return "Failed to construct '" + type + (!detail.isEmpty() ? String("': " + detail) : String("'"));
41 }
42
failedToExecute(const String & method,const String & type,const String & detail)43 String ExceptionMessages::failedToExecute(const String& method, const String& type, const String& detail)
44 {
45 return "Failed to execute '" + method + "' on '" + type + (!detail.isEmpty() ? String("': " + detail) : String("'"));
46 }
47
failedToGet(const String & property,const String & type,const String & detail)48 String ExceptionMessages::failedToGet(const String& property, const String& type, const String& detail)
49 {
50 return "Failed to read the '" + property + "' property from '" + type + "': " + detail;
51 }
52
failedToSet(const String & property,const String & type,const String & detail)53 String ExceptionMessages::failedToSet(const String& property, const String& type, const String& detail)
54 {
55 return "Failed to set the '" + property + "' property on '" + type + "': " + detail;
56 }
57
failedToDelete(const String & property,const String & type,const String & detail)58 String ExceptionMessages::failedToDelete(const String& property, const String& type, const String& detail)
59 {
60 return "Failed to delete the '" + property + "' property from '" + type + "': " + detail;
61 }
62
incorrectPropertyType(const String & property,const String & detail)63 String ExceptionMessages::incorrectPropertyType(const String& property, const String& detail)
64 {
65 return "The '" + property + "' property " + detail;
66 }
67
incorrectArgumentType(int argumentIndex,const String & detail)68 String ExceptionMessages::incorrectArgumentType(int argumentIndex, const String& detail)
69 {
70 return "The " + ordinalNumber(argumentIndex) + " argument " + detail;
71 }
72
notAnArrayTypeArgumentOrValue(int argumentIndex)73 String ExceptionMessages::notAnArrayTypeArgumentOrValue(int argumentIndex)
74 {
75 String kind;
76 if (argumentIndex) // method argument
77 kind = ordinalNumber(argumentIndex) + " argument";
78 else // value, e.g. attribute setter
79 kind = "value provided";
80 return "The " + kind + " is neither an array, nor does it have indexed properties.";
81 }
82
notASequenceTypeProperty(const String & propertyName)83 String ExceptionMessages::notASequenceTypeProperty(const String& propertyName)
84 {
85 return "'" + propertyName + "' property is neither an array, nor does it have indexed properties.";
86 }
87
notEnoughArguments(unsigned expected,unsigned provided)88 String ExceptionMessages::notEnoughArguments(unsigned expected, unsigned provided)
89 {
90 return String::number(expected) + " argument" + (expected > 1 ? "s" : "") + " required, but only " + String::number(provided) + " present.";
91 }
92
notAFiniteNumber(double value)93 String ExceptionMessages::notAFiniteNumber(double value)
94 {
95 ASSERT(!std::isfinite(value));
96 return std::isinf(value) ? "The value provided is infinite." : "The value provided is not a number.";
97 }
98
ordinalNumber(int number)99 String ExceptionMessages::ordinalNumber(int number)
100 {
101 String suffix("th");
102 switch (number % 10) {
103 case 1:
104 if (number % 100 != 11)
105 suffix = "st";
106 break;
107 case 2:
108 if (number % 100 != 12)
109 suffix = "nd";
110 break;
111 case 3:
112 if (number % 100 != 13)
113 suffix = "rd";
114 break;
115 }
116 return String::number(number) + suffix;
117 }
118
119 } // namespace WebCore
120