TheCocoaProject / cordova-plugin-nativestorage

Cordova plugin: Native storage of variables in Android, iOS and Windows
http://thecocoaproject.github.io/
Apache License 2.0
292 stars 106 forks source link

NativeStorageError is not a real Error, you can test with instanceof Error #83

Closed dotnetCarpenter closed 7 years ago

dotnetCarpenter commented 7 years ago

My use-case is that I need to test if the argument to the callbacks is a NativeStorageError.

function readFile(fileName, callback) {
  fileSystem.getItem(
    fileName,
    normalizeCallback(callback, fileName, actionEnum.read), // success
    normalizeCallback(callback, fileName, actionEnum.read)) // failure
}

function normalizeCallback(callback, fileName, action) {
  return function(e) {
    var err
    if(e instanceof Error) {
      err = {
        name: fileName,
        action: getName(actionEnum, action),
        toString: function toString() { return getName(errorEnum, e.code) }
      }
      callback(err, null)
    } else {
      callback(null, fileFactory(fileName, e == null ? '' : e.data))
    }
  }
}

PR with a fix will come soon