Skip to content

Commit

Permalink
Error handling of iOS version now working with JS from Android version
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Brody committed Aug 3, 2013
1 parent 125efb8 commit 08a3485
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
32 changes: 13 additions & 19 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -280,34 +280,26 @@ -(void) executeSqlBatch: (CDVInvokedUrlCommand*)command
NSMutableArray *executes = [options objectForKey:@"executes"];

CDVPluginResult* pluginResult;
NSDictionary *error = nil;

@synchronized(self) {
for (NSMutableDictionary *dict in executes) {
CDVPluginResult *result = [self executeSqlWithDict:dict andArgs:dbargs];
if ([result.status intValue] == CDVCommandStatus_ERROR) {
error = [result message];
break;
}

// add result with result.message:
{
/* add error with result.message: */
NSMutableDictionary *r = [NSMutableDictionary dictionaryWithCapacity:0];
[r setObject:[dict objectForKey:@"qid"] forKey:@"qid"];
[r setObject:result.message forKey:@"error"];
[results addObject: r];
} else {
/* add result with result.message: */
NSMutableDictionary *r = [NSMutableDictionary dictionaryWithCapacity:0];
[r setObject:[dict objectForKey:@"qid"] forKey:@"qid"];
[r setObject:@"success" forKey:@"type"];
[r setObject:result.message forKey:@"result"];
[results addObject: r];
}
}

if (!error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:results];
} else {
NSMutableDictionary *resultsAndError = [NSMutableDictionary dictionaryWithCapacity:2];
[resultsAndError setObject:results forKey:@"results"];
[resultsAndError setObject:error forKey:@"error"];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:resultsAndError];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:results];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down Expand Up @@ -507,15 +499,17 @@ +(NSDictionary *)captureSQLiteErrorFromDb:(sqlite3 *)db
{
int code = sqlite3_errcode(db);
int webSQLCode = [SQLitePlugin mapSQLiteErrorCode:code];
#if INCLUDE_SQLITE_ERROR_INFO
int extendedCode = sqlite3_extended_errcode(db);

This comment has been minimized.

Copy link
@brody4hire

brody4hire Aug 20, 2014

See #122

#endif
const char *message = sqlite3_errmsg(db);

NSMutableDictionary *error = [NSMutableDictionary dictionaryWithCapacity:4];

[error setObject:[NSNumber numberWithInt:webSQLCode] forKey:@"code"];
[error setObject:[NSString stringWithUTF8String:message] forKey:@"message"];

#if INCLUDE_SQLITE_ERROR_INFO
int extendedCode = sqlite3_extended_errcode(db);
const char *message = sqlite3_errmsg(db);

[error setObject:[NSNumber numberWithInt:code] forKey:@"sqliteCode"];
[error setObject:[NSNumber numberWithInt:extendedCode] forKey:@"sqliteExtendedCode"];
[error setObject:[NSString stringWithUTF8String:message] forKey:@"sqliteMessage"];
Expand Down
13 changes: 10 additions & 3 deletions www/SQLitePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ if (!window.Cordova) window.Cordova = window.cordova;
});
}

// NOTE: this function is no longer expected to be called:
var error = function (resultsAndError) {
var results = resultsAndError.results;
var nativeError = resultsAndError.error;
Expand Down Expand Up @@ -282,14 +283,20 @@ if (!window.Cordova) window.Cordova = window.cordova;
}
else {
for (var j = 0; j < results.length; ++j) {
var result = results[j].result;
handleFor(j, true, result);
if (!results[j].error) {
var result = results[j].result;
handleFor(j, true, result);
} else {
var error = new Error('Request failed: ' + opts[j].query);
error.code = results[j].error.code;
handleFor(j, false, error);
}
}
}
};
mycommand = this.db.bg ? "backgroundExecuteSqlBatch" : "executeSqlBatch";
var args = {dbargs: { dbname: this.db.dbname }, executes: opts};
exec(mycommand, args, success, error);
exec(mycommand, args, success, /* not expected: */ error);
};

SQLitePluginTransaction.prototype.rollBack = function(txFailure) {
Expand Down

0 comments on commit 08a3485

Please sign in to comment.