fix(database): Ensure errors reach the renderer-side by sending them as JSON strings, reinflating as Errors

This commit is contained in:
Ben Gotow 2015-07-02 19:15:31 +02:00
parent c3a90e4f5d
commit ef960b21c8
3 changed files with 30 additions and 4 deletions

View file

@ -66,12 +66,14 @@ class DatabaseManager
db = @_databases[databasePath]
if not db
err = new Error("Database not prepared"); result = null
event.sender.send('database-result', {queryKey, err, result})
result = null
errJSONString = JSON.stringify(new Error("Database not prepared"))
event.sender.send('database-result', {queryKey, errJSONString, result})
return
@_query db, query, values, (err, result) ->
event.sender.send('database-result', {queryKey, err, result})
errJSONString = JSON.stringify(err)
event.sender.send('database-result', {queryKey, errJSONString, result})
# Resolves when a new database has been created and the initial setup
# migration has run successfuly.

View file

@ -22,6 +22,21 @@ if (process.type === 'renderer') {
}
var logpath = path.join(tmpPath, 'edgehill-' + logpid + '.log');
// globally define Error.toJSON. This allows us to pass errors via IPC
// and through the Action Bridge. Note:they are not re-inflated into
// Error objects automatically.
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true
});
module.exports = ErrorReporter = (function() {

View file

@ -79,12 +79,21 @@ class DatabaseConnection
databasePath = @_databasePath
ipc.send('database-query', {databasePath, queryKey, query, values})
_onDatabaseResult: ({queryKey, err, result}) =>
_onDatabaseResult: ({queryKey, errJSONString, result}) =>
record = @_queryRecords[queryKey]
return unless record
{query, start, values, reject, resolve, options} = record
if errJSONString
# Note: Error objects turn into JSON when went through the IPC bridge.
# In case downstream code checks instanceof Error, convert back into
# a real error objet.
errJSON = JSON.parse(errJSONString)
err = new Error()
for key, val of errJSON
err[key] = val
@_logQuery(query, start, result)
if options.evaluateImmediately