Add functionality to suppress errors to avoid spam in grids in client apps

This commit is contained in:
Andrew Kingston 2023-06-19 18:21:14 +01:00
parent 9942956a06
commit 757d2d11ef
4 changed files with 24 additions and 7 deletions

View File

@ -35,7 +35,8 @@ export const API = createAPIClient({
// We could also log these to sentry.
// Or we could check error.status and redirect to login on a 403 etc.
onError: error => {
const { status, method, url, message, handled } = error || {}
const { status, method, url, message, handled, suppressErrors } =
error || {}
const ignoreErrorUrls = [
"bbtel",
"/api/global/self",
@ -49,7 +50,7 @@ export const API = createAPIClient({
}
// Notify all errors
if (message) {
if (message && !suppressErrors) {
// Don't notify if the URL contains the word analytics as it may be
// blocked by browser extensions
let ignore = false

View File

@ -75,7 +75,11 @@ export const createAPIClient = config => {
let cache = {}
// Generates an error object from an API response
const makeErrorFromResponse = async (response, method) => {
const makeErrorFromResponse = async (
response,
method,
suppressErrors = false
) => {
// Try to read a message from the error
let message = response.statusText
let json = null
@ -96,6 +100,7 @@ export const createAPIClient = config => {
url: response.url,
method,
handled: true,
suppressErrors,
}
}
@ -119,6 +124,7 @@ export const createAPIClient = config => {
json = true,
external = false,
parseResponse,
suppressErrors = false,
}) => {
// Ensure we don't do JSON processing if sending a GET request
json = json && method !== "GET"
@ -174,7 +180,7 @@ export const createAPIClient = config => {
}
} else {
delete cache[url]
throw await makeErrorFromResponse(response, method)
throw await makeErrorFromResponse(response, method, suppressErrors)
}
}

View File

@ -16,14 +16,16 @@ export const buildRowEndpoints = API => ({
/**
* Creates or updates a row in a table.
* @param row the row to save
* @param suppressErrors whether or not to suppress error notifications
*/
saveRow: async row => {
saveRow: async (row, suppressErrors = false) => {
if (!row?.tableId) {
return
}
return await API.post({
url: `/api/${row.tableId}/rows`,
body: row,
suppressErrors,
})
},

View File

@ -4,6 +4,8 @@ import { notifications } from "@budibase/bbui"
import { NewRowID, RowPageSize } from "../lib/constants"
import { tick } from "svelte"
const SuppressErrors = true
export const createStores = () => {
const rows = writable([])
const table = writable(null)
@ -209,7 +211,10 @@ export const deriveStores = context => {
const addRow = async (row, idx, bubble = false) => {
try {
// Create row
const newRow = await API.saveRow({ ...row, tableId: get(tableId) })
const newRow = await API.saveRow(
{ ...row, tableId: get(tableId) },
SuppressErrors
)
// Update state
if (idx != null) {
@ -336,7 +341,10 @@ export const deriveStores = context => {
...state,
[rowId]: true,
}))
const saved = await API.saveRow({ ...row, ...get(rowChangeCache)[rowId] })
const saved = await API.saveRow(
{ ...row, ...get(rowChangeCache)[rowId] },
SuppressErrors
)
// Update state after a successful change
if (saved?._id) {