feat: update backend error handling

This commit is contained in:
Eric Maciel 2021-01-12 09:33:10 -05:00
parent d4b089a480
commit c29df0d8e7

View File

@ -8,13 +8,6 @@ import { getClients } from './utils/index'
import { debugCollabBackend } from './utils/debug' import { debugCollabBackend } from './utils/debug'
import AutomergeBackend from './AutomergeBackend' import AutomergeBackend from './AutomergeBackend'
interface ErrorData {
user: any
docId: string
serializedData: string
opData?: string
}
export interface IAutomergeCollaborationOptions { export interface IAutomergeCollaborationOptions {
entry: Server entry: Server
connectOpts?: SocketIO.ServerOptions connectOpts?: SocketIO.ServerOptions
@ -28,7 +21,7 @@ export interface IAutomergeCollaborationOptions {
user: any user: any
) => Promise<void> | void ) => Promise<void> | void
onDisconnect?: (docId: string, user: any) => Promise<void> | void onDisconnect?: (docId: string, user: any) => Promise<void> | void
onError?: (error: Error, data: ErrorData) => Promise<void> | void onError?: (error: Error, data: any) => Promise<void> | void
} }
export default class AutomergeCollaboration { export default class AutomergeCollaboration {
@ -78,7 +71,7 @@ export default class AutomergeCollaboration {
/** /**
* Construct error data and call onError callback * Construct error data and call onError callback
*/ */
private handleError(socket: SocketIO.Socket, err: Error, opData?: string) { private handleError(socket: SocketIO.Socket, err: Error, data: any = {}) {
const { id } = socket const { id } = socket
const { name: docId } = socket.nsp const { name: docId } = socket.nsp
@ -86,9 +79,9 @@ export default class AutomergeCollaboration {
const document = this.backend.getDocument(docId) const document = this.backend.getDocument(docId)
this.options.onError(err, { this.options.onError(err, {
user: this.userMap[id], user: this.userMap[id],
docId: docId, docId,
serializedData: document ? Automerge.save(document) : 'No document', automergeDocument: document ? Automerge.save(document) : null,
opData ...data
}) })
} }
} }
@ -221,8 +214,8 @@ export default class AutomergeCollaboration {
this.autoSaveDoc(socket, docId) this.autoSaveDoc(socket, docId)
this.garbageCursors(socket) this.garbageCursors(socket)
} catch (e) { } catch (err) {
this.handleError(socket, e, JSON.stringify(data)) this.handleError(socket, err, { onMessageData: data })
} }
} }
} }
@ -250,8 +243,8 @@ export default class AutomergeCollaboration {
if (onDocumentSave && user) { if (onDocumentSave && user) {
await onDocumentSave(docId, toJS(doc.children), user) await onDocumentSave(docId, toJS(doc.children), user)
} }
} catch (e) { } catch (err) {
this.handleError(socket, e) this.handleError(socket, err)
} }
} }