diff --git a/tests/observability_web_test.mjs b/tests/observability_web_test.mjs index 50c16ba..e4932ae 100644 --- a/tests/observability_web_test.mjs +++ b/tests/observability_web_test.mjs @@ -62,4 +62,27 @@ window.dtfObservability.apiError('/board', 503, 'O sistema está reiniciando.'); window.dtfObservability.uiError('O sistema está reiniciando.', 'kanban'); assert.equal(captured, null, 'a skipped restart 503 stays skipped'); +// A refusal the server explains (a 4xx with a sentence) is no bug: on the +// Kanban a warning, on the Site nothing; a field list or a 5xx is an error +// (2026-11-30). +const refused = (app, status, detail) => { + // A fresh copy per app: a page starts Sentry once, for its own app. + vm.runInThisContext(readFileSync('web/observability.js', 'utf8')); + window.dtfObservability.start('https://key@o1.ingest.sentry.io/1', app); + captured = null; + window.dtfObservability.apiError('/orders/1/move', status, 'Puxe o pedido na mesma máquina.', detail); + return captured; +}; +assert.equal(refused('kanban', 409, 'Puxe o pedido na mesma máquina.').options.level, 'warning'); +assert.equal(refused('kanban', 409, 'Puxe o pedido na mesma máquina.').options.tags.recusa, 'sim'); +assert.equal(refused('site', 409, 'Cupom não encontrado.'), null, 'a refusal on the Site is not sent'); +assert.equal(refused('site', 422, [{loc: ['body', 'items'], msg: 'bad'}]).options.level, 'error'); +assert.equal(refused('site', 500, 'Internal Server Error').options.level, 'error'); +assert.equal(refused('kanban', 422, [{loc: ['body'], msg: 'bad'}]).options.level, 'error'); +// Shown in red after it, the refusal is not sent again as a page error. +refused('site', 409, 'Cupom não encontrado.'); +captured = null; +window.dtfObservability.uiError('Puxe o pedido na mesma máquina.', 'site'); +assert.equal(captured, null, 'a refusal shown in red is not sent as an error'); + console.log('observability web checks passed'); diff --git a/web/observability.js b/web/observability.js index e664d2e..07d5fe6 100644 --- a/web/observability.js +++ b/web/observability.js @@ -122,6 +122,14 @@ if (!started || status === 401 || typeof Sentry === 'undefined') return; // A restart's few seconds without the database (app/app.py): not a bug. if (status === 503 && /reiniciando/.test(String(message))) return; + // A refusal the server explains to the person (a rule, a check, a limit: + // a 4xx with a sentence for them) is the system working, not a bug: on + // the Kanban it stays, as a warning, since a refusal the team keeps + // meeting says a screen confuses; on the Site the customer reads it and + // nothing is sent (2026-11-30). A 4xx without a sentence (a list of + // fields: what the page sent was wrong) and a 5xx stay errors. + const refusal = status >= 400 && status < 500 && typeof detail === 'string'; + if (refusal && app !== 'kanban') return; try { // One issue per kind of API error: without this Sentry groups them all // by apiError and DTF-WEB-1 mixes a 422 for 66 files with a 422 for @@ -131,7 +139,7 @@ const extra = {path, status, page: location.pathname}; if (status === 422 && detail) extra.detail = detail; Sentry.captureMessage('API ' + status + ' ' + kindPath + ': ' + kindMessage, - {level: 'error', tags: {app}, extra, + {level: refusal ? 'warning' : 'error', tags: {app, ...(refusal ? {recusa: 'sim'} : {})}, extra, fingerprint: ['api-error', String(status), kindPath, kindMessage]}); } catch (error) { // Reporting an error must never break the page.