How to catch error net::ERR_FILE_NOT_FOUND when performing XMLHttpRequest to local files?

953 views
Skip to first unread message

Maxim Nikolaevich

unread,
Jan 9, 2018, 1:30:43 AM1/9/18
to Chromium-Extensions-Announce
I want to create chrome extension which will be able to read local files. My simplyfied code is:

import url from 'url'

const readFile = (filePath) => {
  return new Promise(function (resolve, reject) {
    const xhr = new XMLHttpRequest()
    xhr.onerror = (error) => {
      reject(error)
    }
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        resolve(xhr.response)
      }
    }
    xhr.ontimeout = function () {
      reject('timeout')
    }
    xhr.open('GET', filePath)
    xhr.send()
    
  })
}

chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
  if (changeInfo.status === 'complete') {
    let text
    try {
      text = await readFile('file:///home/maxim/Documents/text.jsa')
    } catch (e) {
      console.log('Error: ', e)
    }    
    console.log(text);
  }
})

This code successfully works when my filePath is correct. But when it is not Chrome console throws this error:

GET file:///home/maxim/Documents/text.jsa net::ERR_FILE_NOT_FOUND

How can I catch this type of errors?

PhistucK

unread,
Jan 9, 2018, 3:13:13 AM1/9/18
to Maxim Nikolaevich, Chromium-Extensions-Announce
When dealing with file://, I think any time onerror is triggered means the file either does not exist, is inaccessible (bad sectors?) or there is a permission error (all of which may manifest as ERR_FILE_NOT_FOUND as well, I do not know). You cannot get that error using JavaScript, it is an internal error that is just shown in the Developer Tools in order to help (or hurt ;)) debugging.


☆PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/267055ca-13a1-4d47-a9a5-f314815335f5%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Maxim Nikolaevich

unread,
Jan 9, 2018, 6:06:44 AM1/9/18
to Chromium-Extensions-Announce, maxi...@gmail.com
Ok! So may be do you know another way how I can check if path to local file is correct or not?


☆PhistucK

To post to this group, send email to chromium-...@chromium.org.

PhistucK

unread,
Jan 11, 2018, 3:54:53 AM1/11/18
to Maxim Nikolaevich, Chromium-Extensions-Announce
I do not think there is one (other than going native with native messaging).


☆PhistucK



☆PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Jesse Talamantez

unread,
Jan 11, 2018, 9:43:53 PM1/11/18
to PhistucK, Maxim Nikolaevich, Chromium-Extensions-Announce
My experience is that there is a THEY in the playingroundseeminglyearoundecimalsponserdlessessions"'"



☆PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
Reply all
Reply to author
Forward
0 new messages