PhoneGap and constants

This post is more than 2 years old.

One of the issues I've run into with PhoneGap is dealing with constants. For example, today I'm doing my first work with the file system. I ran into an issue using one of the methods. My error handler was called with the helpful value of:

code:1

Oh. Of course. Error code 1. Well, everyone knows what that is, right? If you check the docs, you see this for FileError:

Ok, that's not helpful. You can clearly see the types of errors, but nothing here indicates what is what.

Luckily I ran into this before (with the SQL stuff I believe) and I remembered that if you simply open up phonegap.js (the JavaScript file you include in every PhoneGap project), you can quickly search for your error object (in my case, FileError), and come across the code:

// File error codes // Found in DOMException FileError.NOT_FOUND_ERR = 1; FileError.SECURITY_ERR = 2; FileError.ABORT_ERR = 3;

// Added by this specification FileError.NOT_READABLE_ERR = 4; FileError.ENCODING_ERR = 5; FileError.NO_MODIFICATION_ALLOWED_ERR = 6; FileError.INVALID_STATE_ERR = 7; FileError.SYNTAX_ERR = 8; FileError.INVALID_MODIFICATION_ERR = 9; FileError.QUOTA_EXCEEDED_ERR = 10; FileError.TYPE_MISMATCH_ERR = 11; FileError.PATH_EXISTS_ERR = 12;

If there is an automatic way to translate that error w/o enumerating over FileError, I don't know. In my case, I just needed this documented.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Patryk posted on 5/21/2012 at 7:53 AM

Well, you can just make a comparison without referencing the error code's value:

if (fail.code == FileError.NOT_FOUND_ERR) {
// ...
}

Or

switch(fail.code) {
case FileError.NOT_FOUND_ERR:
// ...

Comment 2 by Raymond Camden posted on 5/21/2012 at 3:14 PM

For me, it wasn't a "using" issue, but a documentation issue. What you recommend is definiltey how I'd use it in the code, but I needed help figuring out how even to get that code setup (if that distinction makes sense).

Comment 3 by Rei Di Lorenzo posted on 3/21/2014 at 5:44 PM

Hi All.. i'm developer. recenltly upgrade corvova 3.4.0 . I try getfile and receive err.code 1000 ... what's this?? :-(

FileManager.prototype.ReadAsTextFromFile = function (fileName, readDataCallBack) {
var that = this;
try {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(fileName, {create: false},
function (fileEntry) {
fileEntry.file(
function (file){
var reader = new FileReader();
// il risultato della onloadend ovvero evt.target.result viene
// assegnato alla callback, ora vai su relief_detail.js > getReliefDetail
reader.onloadend = readDataCallBack;
reader.readAsText(file);
}
, function(err){alert('ReadFile' + " fail: " + err.code);});
}
, function(err){alert('GetFile' + " fail: " + err.code);});
}, function(err){alert('FileSystem' + " fail: " + err.code);});
} catch (e) {
logError(e);
}
}

Comment 4 by Raymond Camden posted on 3/21/2014 at 6:12 PM

The last version of PhoneGap had a pretty big File IO change: http://www.raymondcamden.co...

Comment 5 by Rei Di Lorenzo posted on 3/21/2014 at 6:39 PM

Thanks Raymond, but I can not.. always error.code 1000 :-(

https://github.com/apache/c...

i try : cdvfile://localhost/persistent/" + my_file_path (android) (/scard/pippo.txt)

Comment 6 by Raymond Camden posted on 3/21/2014 at 7:00 PM

According to the source (https://github.com/apache/c... this is an unknown error. Not sure what to suggest. I haven't had a chance yet to try File stuff in the new builds.

Comment 7 by Rei Di Lorenzo posted on 3/21/2014 at 7:14 PM

I understand, Can you recommend a third-party plugins to manage file IO?

Comment 8 by Raymond Camden posted on 3/21/2014 at 7:24 PM

Well, the FileSystem plugin *does* work - I'd just keep digging. Or consider the PhoneGap Google group.