protobufjs / protobuf.js

Protocol Buffers for JavaScript & TypeScript.
Other
9.87k stars 1.41k forks source link

Starts parsing even if the proto file is not found #866

Open c100k opened 7 years ago

c100k commented 7 years ago

protobuf.js version: 6.8.0 (in a React Native app)

Expected

Throw a meaningful error when the file is not found

Actual behavior

It starts parsing and throws an incomprehensible error

Code snippet

function loadProto() {
    return protobuf.load('thisfiledoesnotexist.proto');
}

Stacktrace

ExceptionsManager.js:71 Error: illegal token 'The' (thisfiledoesnotexist.proto, line 1)
    at illegal (parse.js:87)
    at parse (parse.js:715)
    at process (root.js:107)
    at root.js:184
    at XMLHttpRequest.fetchOnReadyStateChange (index.js:103)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:538)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
    at XMLHttpRequest.js:485
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)

After some investigation, in root.js we have :

util.fetch(filename, function(err, source) {
    console.log(err); // null
    console.log(source); // The file “thisfiledoesnotexist.proto” couldn’t be opened because there is no such file.
    // Some code removed for clarity
    process(filename, source);
});

So maybe it is a bug in fetch that does not consider not finding the file as an error ? Also, what should be the path (absolute or relative) when we load a file in a React Native app ?

dcodeIO commented 7 years ago

Sounds like the invalid source comes from a 404 error page that sends status 200. In this case, that'd rather be a webserver configuration issue.

Reference

asciifaceman commented 7 years ago

914 related/clone

I found absolute paths to work as a work-around for now.

shiyunsheng commented 6 years ago

How do you get the absolute path? @asciifaceman

asciifaceman commented 6 years ago

Typically you would be in control of the location of the file. On an osx terminal or linux CLI you should be able to use pwd in the directory of the file and then append the file name. The absolute path is just the path to the file from the root directory, or / on linux.

shiyunsheng commented 6 years ago

thanks ,such as,protoBuf.load("/Users/sys/shop/data.json"); it's fine but,How do you write when you pack? @asciifaceman

aybb commented 6 years ago

Ok, so absolute path works but its worthless on mobile app. I was lucky enough that my backend developer provided me link to the same file on web site. So https link inside worked, but i doubt that this is the best idea ever

asciifaceman commented 6 years ago

if you have a link then the path is irrelevant. Don't confuse the issue,