[JavaScript] Quick solution for URI malformed / malformed URI sequence errors

Tadashi Shigeoka ·  Thu, March 8, 2018

I’ll introduce a quick solution for URI malformed / malformed URI sequence errors in JavaScript.

JavaScript

Overview of URI malformed / malformed URI sequence

Message

URIError: malformed URI sequence (Firefox)
URIError: URI malformed (Chrome)

Error Type

URIError

What went wrong?

URI encoding or decoding was not successful. The decodeURI or encodeURI, encodeURIComponent, decodeURIComponent functions had invalid arguments, so the functions could not properly encode or decode.

Quick solution for URI malformed / malformed URI sequence errors

Handle it roughly with try…catch statements.

// Decode encoded URL parameters
decodeURIParams(encodedURIParams) {
  try {
    return decodeURIComponent(encodedURIParams);
  } catch (err) {
    return ''; 
  }
}

That’s all from the Gemba where I occasionally encounter URI malformed errors.