Remove NSError** out parameters from file-system-access.ios.

This commit is contained in:
Jason Zhekov
2015-07-13 16:11:38 +03:00
parent e07561332f
commit 8a1b4b7bce

View File

@ -15,7 +15,7 @@ export class FileSystemAccess {
public getLastModified(path: string): Date {
var fileManager = NSFileManager.defaultManager();
var attributes = fileManager.attributesOfItemAtPathError(path, null);
var attributes = fileManager.attributesOfItemAtPathError(path);
if (attributes) {
return attributes.objectForKey(this.keyModificationTime);
@ -84,10 +84,12 @@ export class FileSystemAccess {
var exists = this.folderExists(path);
if (!exists) {
if (!fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, true, null, null)) {
// error
try {
fileManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, true, null)
}
catch (ex) {
if (onError) {
onError(new Error("Failed to create folder at path '" + path + "'"));
onError(new Error("Failed to create folder at path '" + path + "': " + ex));
}
return undefined;
@ -189,9 +191,12 @@ export class FileSystemAccess {
var filesEnum = function (files: Array<{ path: string; name: string; extension: string }>) {
var i;
for (i = 0; i < files.length; i++) {
if (!fileManager.removeItemAtPathError(files[i].path, null)) {
try {
fileManager.removeItemAtPathError(files[i].path);
}
catch (ex) {
if (onError) {
onError(new Error("Failed to empty folder '" + path + "'"));
onError(new Error("Failed to empty folder '" + path + "': " + ex));
}
return;
@ -208,11 +213,19 @@ export class FileSystemAccess {
public rename(path: string, newPath: string, onSuccess?: () => any, onError?: (error: any) => any) {
var fileManager = NSFileManager.defaultManager();
if (!fileManager.moveItemAtPathToPathError(path, newPath, null)) {
if (onError) {
onError(new Error("Failed to rename '" + path + "' to '" + newPath + "'"));
try {
fileManager.moveItemAtPathToPathError(path, newPath);
}
} else if (onSuccess) {
catch (ex) {
if (onError) {
onError(new Error("Failed to rename '" + path + "' to '" + newPath + "': " + ex));
}
return;
}
if (onSuccess) {
onSuccess();
}
}
@ -231,12 +244,18 @@ export class FileSystemAccess {
actualEncoding = textModule.encoding.UTF_8;
}
var nsString = NSString.stringWithContentsOfFileEncodingError(path, actualEncoding, null);
if (!nsString) {
if (onError) {
onError(new Error("Failed to read file at path '" + path + "'"));
try {
var nsString = NSString.stringWithContentsOfFileEncodingError(path, actualEncoding);
}
} else if (onSuccess) {
catch (ex) {
if (onError) {
onError(new Error("Failed to read file at path '" + path + "': " + ex));
}
return;
}
if (onSuccess) {
onSuccess(nsString.toString());
}
}
@ -250,11 +269,18 @@ export class FileSystemAccess {
}
// TODO: verify the useAuxiliaryFile parameter should be false
if (!nsString.writeToFileAtomicallyEncodingError(path, false, actualEncoding, null)) {
if (onError) {
onError(new Error("Failed to write to file '" + path + "'"));
try {
nsString.writeToFileAtomicallyEncodingError(path, false, actualEncoding);
}
} else if (onSuccess) {
catch (ex) {
if (onError) {
onError(new Error("Failed to write to file '" + path + "': " + ex));
}
return;
}
if (onSuccess) {
onSuccess();
}
}
@ -289,25 +315,29 @@ export class FileSystemAccess {
private deleteEntity(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
var fileManager = NSFileManager.defaultManager();
if (!fileManager.removeItemAtPathError(path, null)) {
if (onError) {
onError(new Error("Failed to delete file at path '" + path + "'"));
try {
fileManager.removeItemAtPathError(path);
}
} else {
catch (ex) {
if (onError) {
onError(new Error("Failed to delete file at path '" + path + "': " + ex));
}
}
if (onSuccess) {
onSuccess();
}
}
}
private enumEntities(path: string, callback: (entity: { path: string; name: string; extension: string }) => boolean, onError?: (error) => any) {
try {
var fileManager = NSFileManager.defaultManager();
var files = fileManager.contentsOfDirectoryAtPathError(path, null);
if (!files) {
try {
var files = fileManager.contentsOfDirectoryAtPathError(path);
}
catch (ex) {
if (onError) {
onError(new Error("Failed to enum files for forlder '" + path + "'"));
onError(new Error("Failed to enum files for folder '" + path + "': " + ex));
}
return;