From 2bcaa0bb21603c13faf11c76ebd46915e09abf28 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 7 Apr 2018 20:36:33 +0800 Subject: [PATCH] src: rename ERR_STRING_TOO_LARGE to ERR_STRING_TOO_LONG The old error name and message were trying to be consistent with ERR_BUFFER_TOO_LARGE but they were not really accurate. The kStringMaxLength was measured in number of characters, not number of bytes. The name ERR_STRING_TOO_LARGE also seems a bit awkward. This patch tries to correct them before they get released to users. --- doc/api/errors.md | 4 ++-- src/node_errors.h | 8 ++++---- src/string_bytes.cc | 8 ++++---- .../test-stringbytes-external-exceed-max-by-1-ascii.js | 5 +++-- ...test-stringbytes-external-exceed-max-by-1-base64.js | 5 +++-- ...test-stringbytes-external-exceed-max-by-1-binary.js | 5 +++-- .../test-stringbytes-external-exceed-max-by-1-hex.js | 5 +++-- .../test-stringbytes-external-exceed-max-by-1-utf8.js | 10 ++++++---- .../test-stringbytes-external-exceed-max.js | 5 +++-- test/sequential/test-fs-readfile-tostring-fail.js | 6 +++--- 10 files changed, 34 insertions(+), 27 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index e96848b2960d40..5d4748f0b97103 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1474,8 +1474,8 @@ additional details. A stream method was called that cannot complete because the stream was destroyed using `stream.destroy()`. - -### ERR_STRING_TOO_LARGE + +### ERR_STRING_TOO_LONG An attempt has been made to create a string larger than the maximum allowed size. diff --git a/src/node_errors.h b/src/node_errors.h index 8e328ac2f30644..f34beb6fbc94cd 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -18,7 +18,7 @@ namespace node { #define ERRORS_WITH_CODE(V) \ V(ERR_MEMORY_ALLOCATION_FAILED, Error) \ - V(ERR_STRING_TOO_LARGE, Error) \ + V(ERR_STRING_TOO_LONG, Error) \ V(ERR_BUFFER_TOO_LARGE, Error) #define V(code, type) \ @@ -58,12 +58,12 @@ inline v8::Local ERR_BUFFER_TOO_LARGE(v8::Isolate *isolate) { return ERR_BUFFER_TOO_LARGE(isolate, message); } -inline v8::Local ERR_STRING_TOO_LARGE(v8::Isolate *isolate) { +inline v8::Local ERR_STRING_TOO_LONG(v8::Isolate *isolate) { char message[128]; snprintf(message, sizeof(message), - "Cannot create a string larger than 0x%x bytes", + "Cannot create a string longer than 0x%x characters", v8::String::kMaxLength); - return ERR_STRING_TOO_LARGE(isolate, message); + return ERR_STRING_TOO_LONG(isolate, message); } } // namespace node diff --git a/src/string_bytes.cc b/src/string_bytes.cc index bbd381d33a5cf7..0bc2ec044ebb55 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -113,7 +113,7 @@ class ExternString: public ResourceType { if (str.IsEmpty()) { delete h_str; - *error = node::ERR_STRING_TOO_LARGE(isolate); + *error = node::ERR_STRING_TOO_LONG(isolate); return MaybeLocal(); } @@ -170,7 +170,7 @@ MaybeLocal ExternOneByteString::NewSimpleFromCopy(Isolate* isolate, v8::NewStringType::kNormal, length); if (str.IsEmpty()) { - *error = node::ERR_STRING_TOO_LARGE(isolate); + *error = node::ERR_STRING_TOO_LONG(isolate); return MaybeLocal(); } return str.ToLocalChecked(); @@ -188,7 +188,7 @@ MaybeLocal ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate, v8::NewStringType::kNormal, length); if (str.IsEmpty()) { - *error = node::ERR_STRING_TOO_LARGE(isolate); + *error = node::ERR_STRING_TOO_LONG(isolate); return MaybeLocal(); } return str.ToLocalChecked(); @@ -657,7 +657,7 @@ MaybeLocal StringBytes::Encode(Isolate* isolate, v8::NewStringType::kNormal, buflen); if (val.IsEmpty()) { - *error = node::ERR_STRING_TOO_LARGE(isolate); + *error = node::ERR_STRING_TOO_LONG(isolate); return MaybeLocal(); } return val.ToLocalChecked(); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js index 96a3273254d8d7..c7cf1319e7c57c 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-ascii.js @@ -28,7 +28,8 @@ const stringLengthHex = kStringMaxLength.toString(16); common.expectsError(function() { buf.toString('ascii'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js index 90e13ce788efcc..920124e897de32 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-base64.js @@ -28,7 +28,8 @@ const stringLengthHex = kStringMaxLength.toString(16); common.expectsError(function() { buf.toString('base64'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js index 0ffd1eb4166989..f8aed7c0b2e09b 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-binary.js @@ -29,8 +29,9 @@ const stringLengthHex = kStringMaxLength.toString(16); common.expectsError(function() { buf.toString('latin1'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js index bc64ef396dc2d3..18fb46e0df95b8 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-hex.js @@ -28,7 +28,8 @@ const stringLengthHex = kStringMaxLength.toString(16); common.expectsError(function() { buf.toString('hex'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js index f6c9f21e4b06e2..5f7221b591e8ac 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max-by-1-utf8.js @@ -32,8 +32,9 @@ assert.throws(function() { }, function(e) { if (e.message !== 'Array buffer allocation failed') { common.expectsError({ - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error })(e); return true; @@ -45,7 +46,8 @@ assert.throws(function() { common.expectsError(function() { buf.toString('utf8'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js index e4b66d8f30bae3..e46b5f24cce028 100644 --- a/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js +++ b/test/addons/stringbytes-external-exceed-max/test-stringbytes-external-exceed-max.js @@ -29,7 +29,8 @@ const stringLengthHex = kStringMaxLength.toString(16); common.expectsError(function() { buf.toString('utf16le'); }, { - message: `Cannot create a string larger than 0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: `Cannot create a string longer than 0x${stringLengthHex} ` + + 'characters', + code: 'ERR_STRING_TOO_LONG', type: Error }); diff --git a/test/sequential/test-fs-readfile-tostring-fail.js b/test/sequential/test-fs-readfile-tostring-fail.js index f8b0c666a01b23..c5ed8559103d77 100644 --- a/test/sequential/test-fs-readfile-tostring-fail.js +++ b/test/sequential/test-fs-readfile-tostring-fail.js @@ -35,9 +35,9 @@ stream.on('finish', common.mustCall(function() { if (err.message !== 'Array buffer allocation failed') { const stringLengthHex = kStringMaxLength.toString(16); common.expectsError({ - message: 'Cannot create a string larger than ' + - `0x${stringLengthHex} bytes`, - code: 'ERR_STRING_TOO_LARGE', + message: 'Cannot create a string longer than ' + + `0x${stringLengthHex} characters`, + code: 'ERR_STRING_TOO_LONG', type: Error })(err); }