question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

decodeURIComponent can throw at runtime

See original GitHub issue

Hello! First of all, thank for this module, I love it.

I was testing a route and I mistyped the url, for instance instead of write 2 I wrote ", the result was that decodeURIComponent threw an URIError exception at runtime! I can easily fix this by using a try catch and return undefined, but it will deoptimize the function. We can also use a regex, something like:

if (/"/g.test(thisRoute)) return undefined

But actually I don’t know if " is the only bad character! Any suggestion? 😃

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
yoshuawuytscommented, Apr 28, 2017

Looks like introducing try/catch doesn’t introduce a huge performance regression and solves the issue. Could you open a PR for this?

On Fri, Apr 28, 2017, 19:18 Tomas Della Vedova notifications@github.com wrote:

@davidmarkclements https://github.com/davidmarkclements use decodeURIComponent and escape together is not a good solution, since it introduces a wrong behaviour:

decodeURIComponent(escape(‘http://localhost:3030/test/cia%2Fo’))// => ‘http://localhost:3030/test/cia%2Fo’ decodeURIComponent(‘http://localhost:3030/test/cia%2Fo’)// => ‘http://localhost:3030/test/cia/o

Probably the best solution is to use a try/catch

try { decodeURIComponent(‘http://localhost:3030/test/cia%2Fo’) } catch (e) { return undefined }

Here the benchmarks (in node v6.10.0) within and without the try/catch:

decodeURIComponent - no try/catch x 3,999,819 ops/sec ±0.79% (91 runs sampled) decodeURIComponent - try/catch x 3,842,394 ops/sec ±0.70% (93 runs sampled)

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/yoshuawuyts/wayfarer/issues/57#issuecomment-298056108, or mute the thread https://github.com/notifications/unsubscribe-auth/ACWlerT25Idt2PHITxy8EICu98DOx8Xrks5r0h9egaJpZM4M13vC .

1reaction
delvedorcommented, Apr 28, 2017

@davidmarkclements use decodeURIComponent and escape together is not a good solution, since it introduces a wrong behaviour:

decodeURIComponent(escape('http://localhost:3030/test/cia%2Fo'))
// => 'http://localhost:3030/test/cia%2Fo'

decodeURIComponent('http://localhost:3030/test/cia%2Fo')
// => 'http://localhost:3030/test/cia/o'

Probably the best solution is to use a try/catch

try {
  decodeURIComponent('http://localhost:3030/test/cia%2Fo')
} catch (e) {
  return undefined
}

Here the benchmarks (in node v6.10.0) within and without the try/catch:

decodeURIComponent - no try/catch x 3,999,819 ops/sec ±0.79% (91 runs sampled)
decodeURIComponent - try/catch x 3,842,394 ops/sec ±0.70% (93 runs sampled)
Read more comments on GitHub >

github_iconTop Results From Across the Web

URIError: malformed URI sequence - MDN Web Docs - Mozilla
An URIError will be thrown if there is an attempt to encode a surrogate which is not part of a high-low pair, for...
Read more >
encodeURIComponent throws an exception - Stack Overflow
I'd gladly do that if I could figure out which characters encodeURIComponent considers as valid. – K Mehta. Jun 1, 2013 at 2:48....
Read more >
How to Throw Exceptions in Node.js - Rollbar
In this post, we'll take a look at what causes these errors in Node.js, and how to recover from them. What can cause...
Read more >
Exception Handling - TypeScript Deep Dive - Gitbook
JavaScript has an Error class that you can use for exceptions. ... built-in error classes that inherit from Error that the JavaScript runtime...
Read more >
JavaScript policy runtime error troubleshooting | Apigee Edge
The JavaScript policy can throw many different types of ScriptExecutionFailed errors. Some of the more commonly seen errors are listed in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found