[FIX] Handle database errors properly (#650)

This commit is contained in:
Diego Mello 2019-02-25 13:23:17 -03:00 committed by GitHub
parent bf212bfc4d
commit df78a6831c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 124 additions and 40 deletions

View File

@ -353,7 +353,11 @@ export default class MessageBox extends Component {
if (results.users && results.users.length) { if (results.users && results.users.length) {
database.write(() => { database.write(() => {
results.users.forEach((user) => { results.users.forEach((user) => {
try {
database.create('users', user, true); database.create('users', user, true);
} catch (e) {
log('create users', e);
}
}); });
}); });
} }

View File

@ -18,10 +18,21 @@ export default async function() {
const result = await this.sdk.get('emoji-custom'); const result = await this.sdk.get('emoji-custom');
let { emojis } = result; let { emojis } = result;
emojis = emojis.filter(emoji => !lastMessage || emoji._updatedAt > lastMessage); emojis = emojis.filter(emoji => !lastMessage || emoji._updatedAt > lastMessage);
if (emojis.length === 0) {
return;
}
emojis = this._prepareEmojis(emojis); emojis = this._prepareEmojis(emojis);
InteractionManager.runAfterInteractions(() => database.write(() => { InteractionManager.runAfterInteractions(() => {
emojis.forEach(emoji => database.create('customEmojis', emoji, true)); database.write(() => {
})); emojis.forEach((emoji) => {
try {
database.create('customEmojis', emoji, true);
} catch (e) {
log('create custom emojis', e);
}
});
});
});
reduxStore.dispatch(actions.setCustomEmojis(this.parseEmojis(emojis))); reduxStore.dispatch(actions.setCustomEmojis(this.parseEmojis(emojis)));
} catch (e) { } catch (e) {
log('getCustomEmojis', e); log('getCustomEmojis', e);

View File

@ -8,7 +8,11 @@ import settings from '../../constants/settings';
function updateServer(param) { function updateServer(param) {
database.databases.serversDB.write(() => { database.databases.serversDB.write(() => {
try {
database.databases.serversDB.create('servers', { id: reduxStore.getState().server.server, ...param }, true); database.databases.serversDB.create('servers', { id: reduxStore.getState().server.server, ...param }, true);
} catch (e) {
log('updateServer', e);
}
}); });
} }
@ -27,7 +31,11 @@ export default async function() {
InteractionManager.runAfterInteractions( InteractionManager.runAfterInteractions(
() => database.write( () => database.write(
() => filteredSettings.forEach((setting) => { () => filteredSettings.forEach((setting) => {
try {
database.create('settings', { ...setting, _updatedAt: new Date() }, true); database.create('settings', { ...setting, _updatedAt: new Date() }, true);
} catch (e) {
log('create settings', e);
}
if (setting._id === 'Site_Name') { if (setting._id === 'Site_Name') {
updateServer.call(this, { name: setting.valueAsString }); updateServer.call(this, { name: setting.valueAsString });

View File

@ -39,7 +39,11 @@ export default function loadMessagesForRoom(...args) {
if (data && data.length) { if (data && data.length) {
InteractionManager.runAfterInteractions(() => { InteractionManager.runAfterInteractions(() => {
database.write(() => data.forEach((message) => { database.write(() => data.forEach((message) => {
try {
database.create('messages', buildMessage(message), true); database.create('messages', buildMessage(message), true);
} catch (e) {
log('loadMessagesForRoom -> create messages', e);
}
})); }));
return resolve(data); return resolve(data);
}); });

View File

@ -33,19 +33,29 @@ export default function loadMissedMessages(...args) {
const { updated } = data; const { updated } = data;
updated.forEach(buildMessage); updated.forEach(buildMessage);
InteractionManager.runAfterInteractions(() => { InteractionManager.runAfterInteractions(() => {
database.write(() => updated.forEach(message => database.create('messages', message, true))); database.write(() => updated.forEach((message) => {
try {
database.create('messages', message, true);
} catch (e) {
log('loadMissedMessages -> create messages', e);
}
}));
resolve(updated); resolve(updated);
}); });
} }
if (data.deleted && data.deleted.length) { if (data.deleted && data.deleted.length) {
const { deleted } = data; const { deleted } = data;
InteractionManager.runAfterInteractions(() => { InteractionManager.runAfterInteractions(() => {
try {
database.write(() => { database.write(() => {
deleted.forEach((m) => { deleted.forEach((m) => {
const message = database.objects('messages').filtered('_id = $0', m._id); const message = database.objects('messages').filtered('_id = $0', m._id);
database.delete(message); database.delete(message);
}); });
}); });
} catch (e) {
log('loadMissedMessages -> delete message', e);
}
}); });
} }
} }

View File

@ -2,6 +2,7 @@ import RNFetchBlob from 'rn-fetch-blob';
import reduxStore from '../createStore'; import reduxStore from '../createStore';
import database from '../realm'; import database from '../realm';
import log from '../../utils/log';
const promises = {}; const promises = {};
@ -47,7 +48,11 @@ export async function sendFileMessage(rid, fileInfo) {
fileInfo.rid = rid; fileInfo.rid = rid;
database.write(() => { database.write(() => {
try {
database.create('uploads', fileInfo, true); database.create('uploads', fileInfo, true);
} catch (e) {
log('sendFileMessage -> create uploads 1', e);
}
}); });
const result = await _ufsCreate.call(this, fileInfo); const result = await _ufsCreate.call(this, fileInfo);
@ -60,7 +65,11 @@ export async function sendFileMessage(rid, fileInfo) {
promises[fileInfo.path].uploadProgress((loaded, total) => { promises[fileInfo.path].uploadProgress((loaded, total) => {
database.write(() => { database.write(() => {
fileInfo.progress = Math.floor((loaded / total) * 100); fileInfo.progress = Math.floor((loaded / total) * 100);
try {
database.create('uploads', fileInfo, true); database.create('uploads', fileInfo, true);
} catch (e) {
log('sendFileMessage -> create uploads 2', e);
}
}); });
}); });
}); });
@ -79,12 +88,20 @@ export async function sendFileMessage(rid, fileInfo) {
database.write(() => { database.write(() => {
const upload = database.objects('uploads').filtered('path = $0', fileInfo.path); const upload = database.objects('uploads').filtered('path = $0', fileInfo.path);
try {
database.delete(upload); database.delete(upload);
} catch (e) {
log('sendFileMessage -> delete uploads', e);
}
}); });
} catch (e) { } catch (e) {
database.write(() => { database.write(() => {
fileInfo.error = true; fileInfo.error = true;
try {
database.create('uploads', fileInfo, true); database.create('uploads', fileInfo, true);
} catch (err) {
log('sendFileMessage -> create uploads 3', err);
}
}); });
} }
} }

View File

@ -53,30 +53,46 @@ export default async function subscribeRooms() {
if (subscription) { if (subscription) {
messages = database.objects('messages').filtered('rid == $0', subscription.rid); messages = database.objects('messages').filtered('rid == $0', subscription.rid);
} }
try {
database.write(() => { database.write(() => {
database.delete(messages); database.delete(messages);
database.delete(subscription); database.delete(subscription);
}); });
} catch (e) {
log('handleStreamMessageReceived -> subscriptions removed', e);
}
} else { } else {
const rooms = database.objects('rooms').filtered('_id == $0', data.rid); const rooms = database.objects('rooms').filtered('_id == $0', data.rid);
const tpm = merge(data, rooms[0]); const tpm = merge(data, rooms[0]);
try {
database.write(() => { database.write(() => {
database.create('subscriptions', tpm, true); database.create('subscriptions', tpm, true);
database.delete(rooms); database.delete(rooms);
}); });
} catch (e) {
log('handleStreamMessageReceived -> subscriptions updated', e);
}
} }
} }
if (/rooms/.test(ev)) { if (/rooms/.test(ev)) {
if (type === 'updated') { if (type === 'updated') {
const [sub] = database.objects('subscriptions').filtered('rid == $0', data._id); const [sub] = database.objects('subscriptions').filtered('rid == $0', data._id);
try {
database.write(() => { database.write(() => {
const tmp = merge(sub, data); const tmp = merge(sub, data);
database.create('subscriptions', tmp, true); database.create('subscriptions', tmp, true);
}); });
} catch (e) {
log('handleStreamMessageReceived -> rooms updated', e);
}
} else if (type === 'inserted') { } else if (type === 'inserted') {
try {
database.write(() => { database.write(() => {
database.create('rooms', data, true); database.create('rooms', data, true);
}); });
} catch (e) {
log('handleStreamMessageReceived -> rooms inserted', e);
}
} }
} }
if (/message/.test(ev)) { if (/message/.test(ev)) {
@ -94,9 +110,15 @@ export default async function subscribeRooms() {
username: 'rocket.cat' username: 'rocket.cat'
} }
}; };
requestAnimationFrame(() => database.write(() => { requestAnimationFrame(() => {
try {
database.write(() => {
database.create('messages', message, true); database.create('messages', message, true);
})); });
} catch (e) {
log('handleStreamMessageReceived -> message', e);
}
});
} }
}); });

View File

@ -246,7 +246,11 @@ const RocketChat = {
database.write(() => { database.write(() => {
foreach(this.roles, (description, _id) => { foreach(this.roles, (description, _id) => {
try {
database.create('roles', { _id, description }, true); database.create('roles', { _id, description }, true);
} catch (e) {
log('create roles', e);
}
}); });
}); });
@ -408,10 +412,14 @@ const RocketChat = {
}); });
await sendMessageCall.call(this, JSON.parse(JSON.stringify(message))); await sendMessageCall.call(this, JSON.parse(JSON.stringify(message)));
} catch (error) { } catch (error) {
try {
database.write(() => { database.write(() => {
message.status = messagesStatus.ERROR; message.status = messagesStatus.ERROR;
database.create('messages', message, true); database.create('messages', message, true);
}); });
} catch (e) {
log('resendMessage error', e);
}
} }
}, },