Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: parse & throw json errors from signalr errors

thecodrr7b57b0b91618
diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js
index 40c2c4492..4d93f8716 100644
--- a/packages/core/api/sync/index.js
+++ b/packages/core/api/sync/index.js
@@ -16,6 +16,7 @@ import { SyncQueue } from "./syncqueue";
import { AutoSync } from "./auto-sync";
import { toChunks } from "../../utils/array";
import { MessagePackHubProtocol } from "@microsoft/signalr-protocol-msgpack";
+import { errorTransformer, RequestError } from "../../utils/http";
const ITEM_TYPE_MAP = {
attachments: "attachment",
@@ -60,7 +61,25 @@ export default class SyncManager {
if (this.syncMutex.isLocked()) return false;
return this.syncMutex.runExclusive(async () => {
await this.sync.autoSync.start();
- await this.sync.start(full, force);
+ try {
+ await this.sync.start(full, force);
+ } catch (e) {
+ // TODO: a very hacky way to parse out a JSON error
+ // from the errors given by SignalR
+ let jsonError = /\{.*\}/gm.exec(e.message);
+ if (jsonError) {
+ jsonError = jsonError[0];
+ let parsed = null;
+ try {
+ parsed = errorTransformer(JSON.parse(jsonError));
+ } catch {
+ throw e;
+ }
+
+ if (parsed) throw new RequestError(parsed);
+ }
+ throw e;
+ }
});
}
diff --git a/packages/core/utils/http.js b/packages/core/utils/http.js
index d8fa14d7d..15ae31033 100644
--- a/packages/core/utils/http.js
+++ b/packages/core/utils/http.js
@@ -104,7 +104,7 @@ function getAuthorizationHeader(token) {
return token ? { Authorization: "Bearer " + token } : {};
}
-function errorTransformer(errorJson) {
+export function errorTransformer(errorJson) {
let errorMessage = "Unknown error.";
let errorCode = "unknown";