From 76bce0be1af739e098ace4e6a9f90e2c705be716 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 3 Jul 2023 15:28:42 +0100 Subject: [PATCH 1/4] Removing the alert log of failure, this isn't really an error scenario, as it'll automatically get updated as part of app syncs and if the sync is reran. --- packages/server/src/sdk/app/applications/sync.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index d0a1d78428..2806afc858 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -105,12 +105,7 @@ export async function syncUsersToAllApps(userIds: string[]) { promises.push(syncUsersToApp(appId, finalUsers, groups)) } } - const resp = await Promise.allSettled(promises) - const failed = resp.filter(promise => promise.status === "rejected") - if (failed.length > 0) { - const reasons = failed.map(fail => (fail as PromiseRejectedResult).reason) - logging.logAlert("Failed to sync users to apps", reasons) - } + return await Promise.allSettled(promises) } export async function syncApp( From 4e560a1f3240fc0a966d77f02c3e3c14a82a6886 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 3 Jul 2023 16:00:33 +0100 Subject: [PATCH 2/4] Adding warning back in for non-409 errors of user sync. --- packages/backend-core/src/db/errors.ts | 9 +++++++++ packages/backend-core/src/db/index.ts | 1 + .../server/src/sdk/app/applications/sync.ts | 18 +++++++++--------- 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 packages/backend-core/src/db/errors.ts diff --git a/packages/backend-core/src/db/errors.ts b/packages/backend-core/src/db/errors.ts new file mode 100644 index 0000000000..db536502b2 --- /dev/null +++ b/packages/backend-core/src/db/errors.ts @@ -0,0 +1,9 @@ +export function is409Error(error: any) { + if (typeof error === "object") { + return error.status === 409 || error.message?.includes("409") + } else if (typeof error === "number") { + return error === 409 + } else if (typeof error === "string") { + return error.includes("409") + } +} diff --git a/packages/backend-core/src/db/index.ts b/packages/backend-core/src/db/index.ts index cf71199ecf..c47d1793dc 100644 --- a/packages/backend-core/src/db/index.ts +++ b/packages/backend-core/src/db/index.ts @@ -9,3 +9,4 @@ export * from "../constants/db" export { getGlobalDBName, baseGlobalDBName } from "../context" export * from "./lucene" export * as searchIndexes from "./searchIndexes" +export * from "./errors" diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index 2806afc858..559b0733da 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -1,12 +1,5 @@ import env from "../../../environment" -import { - db as dbCore, - context, - docUpdates, - constants, - logging, - roles, -} from "@budibase/backend-core" +import { db as dbCore, context, logging, roles } from "@budibase/backend-core" import { User, ContextUser, UserGroup } from "@budibase/types" import { sdk as proSdk } from "@budibase/pro" import sdk from "../../" @@ -105,7 +98,14 @@ export async function syncUsersToAllApps(userIds: string[]) { promises.push(syncUsersToApp(appId, finalUsers, groups)) } } - return await Promise.allSettled(promises) + const resp = await Promise.allSettled(promises) + const failed = resp.filter(promise => promise.status === "rejected") + const reasons = failed + .map(fail => (fail as PromiseRejectedResult).reason) + .filter(reason => !dbCore.is409Error(reason)) + if (reasons.length > 0) { + logging.logWarn("Failed to sync users to apps", reasons) + } } export async function syncApp( From deca999183bffa6fd72a4dfb04a9557b7a26a5b9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 3 Jul 2023 16:01:54 +0100 Subject: [PATCH 3/4] Allowing logWarn to take an object for warning. --- packages/backend-core/src/logging/alerts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend-core/src/logging/alerts.ts b/packages/backend-core/src/logging/alerts.ts index 625ecfcf15..b7173bc436 100644 --- a/packages/backend-core/src/logging/alerts.ts +++ b/packages/backend-core/src/logging/alerts.ts @@ -21,6 +21,6 @@ export function logAlertWithInfo( logAlert(message, error) } -export function logWarn(message: string) { - console.warn(`bb-warn: ${message}`) +export function logWarn(message: string, e?: any) { + console.warn(`bb-warn: ${message}`, e) } From c1e9bc3c81b70325928f5bbae60c518c3d28e872 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 4 Jul 2023 11:59:56 +0100 Subject: [PATCH 4/4] PR comments. --- packages/backend-core/src/db/errors.ts | 13 +++++++++---- packages/server/src/sdk/app/applications/sync.ts | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/db/errors.ts b/packages/backend-core/src/db/errors.ts index db536502b2..17f76c667b 100644 --- a/packages/backend-core/src/db/errors.ts +++ b/packages/backend-core/src/db/errors.ts @@ -1,9 +1,14 @@ -export function is409Error(error: any) { +export function checkErrorCode(error: any, code: number) { + const stringCode = code.toString() if (typeof error === "object") { - return error.status === 409 || error.message?.includes("409") + return error.status === code || error.message?.includes(stringCode) } else if (typeof error === "number") { - return error === 409 + return error === code } else if (typeof error === "string") { - return error.includes("409") + return error.includes(stringCode) } } + +export function isDocumentConflictError(error: any) { + return checkErrorCode(error, 409) +} diff --git a/packages/server/src/sdk/app/applications/sync.ts b/packages/server/src/sdk/app/applications/sync.ts index 559b0733da..d8807251a1 100644 --- a/packages/server/src/sdk/app/applications/sync.ts +++ b/packages/server/src/sdk/app/applications/sync.ts @@ -102,7 +102,7 @@ export async function syncUsersToAllApps(userIds: string[]) { const failed = resp.filter(promise => promise.status === "rejected") const reasons = failed .map(fail => (fail as PromiseRejectedResult).reason) - .filter(reason => !dbCore.is409Error(reason)) + .filter(reason => !dbCore.isDocumentConflictError(reason)) if (reasons.length > 0) { logging.logWarn("Failed to sync users to apps", reasons) }