[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) {
database.write(() => {
results.users.forEach((user) => {
try {
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');
let { emojis } = result;
emojis = emojis.filter(emoji => !lastMessage || emoji._updatedAt > lastMessage);
if (emojis.length === 0) {
return;
}
emojis = this._prepareEmojis(emojis);
InteractionManager.runAfterInteractions(() => database.write(() => {
emojis.forEach(emoji => database.create('customEmojis', emoji, true));
}));
InteractionManager.runAfterInteractions(() => {
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)));
} catch (e) {
log('getCustomEmojis', e);

View File

@ -8,7 +8,11 @@ import settings from '../../constants/settings';
function updateServer(param) {
database.databases.serversDB.write(() => {
try {
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(
() => database.write(
() => filteredSettings.forEach((setting) => {
try {
database.create('settings', { ...setting, _updatedAt: new Date() }, true);
} catch (e) {
log('create settings', e);
}
if (setting._id === 'Site_Name') {
updateServer.call(this, { name: setting.valueAsString });

View File

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

View File

@ -33,19 +33,29 @@ export default function loadMissedMessages(...args) {
const { updated } = data;
updated.forEach(buildMessage);
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);
});
}
if (data.deleted && data.deleted.length) {
const { deleted } = data;
InteractionManager.runAfterInteractions(() => {
try {
database.write(() => {
deleted.forEach((m) => {
const message = database.objects('messages').filtered('_id = $0', m._id);
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 database from '../realm';
import log from '../../utils/log';
const promises = {};
@ -47,7 +48,11 @@ export async function sendFileMessage(rid, fileInfo) {
fileInfo.rid = rid;
database.write(() => {
try {
database.create('uploads', fileInfo, true);
} catch (e) {
log('sendFileMessage -> create uploads 1', e);
}
});
const result = await _ufsCreate.call(this, fileInfo);
@ -60,7 +65,11 @@ export async function sendFileMessage(rid, fileInfo) {
promises[fileInfo.path].uploadProgress((loaded, total) => {
database.write(() => {
fileInfo.progress = Math.floor((loaded / total) * 100);
try {
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(() => {
const upload = database.objects('uploads').filtered('path = $0', fileInfo.path);
try {
database.delete(upload);
} catch (e) {
log('sendFileMessage -> delete uploads', e);
}
});
} catch (e) {
database.write(() => {
fileInfo.error = true;
try {
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) {
messages = database.objects('messages').filtered('rid == $0', subscription.rid);
}
try {
database.write(() => {
database.delete(messages);
database.delete(subscription);
});
} catch (e) {
log('handleStreamMessageReceived -> subscriptions removed', e);
}
} else {
const rooms = database.objects('rooms').filtered('_id == $0', data.rid);
const tpm = merge(data, rooms[0]);
try {
database.write(() => {
database.create('subscriptions', tpm, true);
database.delete(rooms);
});
} catch (e) {
log('handleStreamMessageReceived -> subscriptions updated', e);
}
}
}
if (/rooms/.test(ev)) {
if (type === 'updated') {
const [sub] = database.objects('subscriptions').filtered('rid == $0', data._id);
try {
database.write(() => {
const tmp = merge(sub, data);
database.create('subscriptions', tmp, true);
});
} catch (e) {
log('handleStreamMessageReceived -> rooms updated', e);
}
} else if (type === 'inserted') {
try {
database.write(() => {
database.create('rooms', data, true);
});
} catch (e) {
log('handleStreamMessageReceived -> rooms inserted', e);
}
}
}
if (/message/.test(ev)) {
@ -94,9 +110,15 @@ export default async function subscribeRooms() {
username: 'rocket.cat'
}
};
requestAnimationFrame(() => database.write(() => {
requestAnimationFrame(() => {
try {
database.write(() => {
database.create('messages', message, true);
}));
});
} catch (e) {
log('handleStreamMessageReceived -> message', e);
}
});
}
});

View File

@ -246,7 +246,11 @@ const RocketChat = {
database.write(() => {
foreach(this.roles, (description, _id) => {
try {
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)));
} catch (error) {
try {
database.write(() => {
message.status = messagesStatus.ERROR;
database.create('messages', message, true);
});
} catch (e) {
log('resendMessage error', e);
}
}
},