Skip to content

lib: validate argument for punycode.encode and decode

Purpose

Unify error wording and make it easy to understand.

encode

Current behavior

punycode.encode()

argument's type result
number ''
string ''
boolean ''
undefined error
null error
object ''
array ''
function ''

punycode.encode is occurred an error when the argument is undefined and null.

Error log
TypeError: Cannot read property 'length' of undefined
    at ucs2decode (punycode.js:104:23)
    at Object.encode (punycode.js:291:10)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)

Modify error's wording like below.

TypeError: Argument must not be "undefined" and "null"
    at Object.encode (punycode.js:292:11)
    at repl:1:10
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:537:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:189:7)
    at REPLServer.Interface._onLine (readline.js:238:10)

decode

Current behavior

punycode.decode()

argument's type result
number error
string ''
boolean error
undefined error
null error
object error
array ''(only empty arrays are accepted)
function error

punycode.decode is occurred an error when the argument isn't string and array. We accpeted only a string.

Error log

undefined, null

TypeError: Cannot read property 'length' of undefined
    at Object.decode (punycode.js:199:27)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)
    at emitOne (events.js:101:20)

number, boolean, object, function

TypeError: input.lastIndexOf is not a function
    at Object.decode (punycode.js:208:20)
    at repl:1:10
    at realRunInThisContextScript (vm.js:22:35)
    at sigintHandlersWrap (vm.js:98:12)
    at ContextifyScript.Script.runInThisContext (vm.js:24:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:544:10)
    at emitOne (events.js:101:20)

Modify error's wording like below.

TypeError: Argument must be a string
    at Object.decode (punycode.js:198:11)
    at repl:1:10
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:537:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:189:7)
    at REPLServer.Interface._onLine (readline.js:238:10)
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

punycode, test

Merge request reports

Loading