最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript FileReader onerror event - how to find out what the error was? - Stack Overflow

programmeradmin2浏览0评论

the Javascript FileReader object supports an onerror handler that gets called when there has been an error, but the event passed in is a ProgressEvent and doesn't contain any details of the error.

How can I find out what actual error occurred?

the Javascript FileReader object supports an onerror handler that gets called when there has been an error, but the event passed in is a ProgressEvent and doesn't contain any details of the error.

How can I find out what actual error occurred?

Share Improve this question asked Apr 13, 2022 at 9:40 AndyAndy 11.5k16 gold badges77 silver badges100 bronze badges 2
  • Could you please add the snippet of your code for us to better understand the problem? – Lokendra Soni Commented Apr 13, 2022 at 9:43
  • I think this link might help you: File and FileReader – Ariel Rotem Commented Apr 13, 2022 at 9:43
Add a ment  | 

1 Answer 1

Reset to default 9

After RTFMing a little more closely in response to @Ariel's ment (https://developer.mozilla/en-US/docs/Web/API/FileReader/error), I see that FileReader has an error property as well as an error event. When the error handler is called, the the ProgressEvent passed to it is next to useless and you need to look at the error property instead.

Therefore instead of:

reader.onerror = x => { throw x }

a more appropriate response is:

reader.onerror = () => { throw reader.error }`

or if you're wrapping inside a promise (as per Javascript Promises with FileReader()), instead of:

reader.onerror = reject:

you're better off using

reader.onerror = () => reject(reader.error);

Having said that, the error property is of type DOMException which does not inherit from Error and, in my case, contained very little useful information, but it seems to be the best there is and, as far as I could tell, throwing a DOMException seems to be considered acceptable practice even though it isn't an Error

发布评论

评论列表(0)

  1. 暂无评论