From f2b825b65269a5460e41a463a570e2bc68461078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Mon, 16 Nov 2026 13:40:59 -0300 Subject: [PATCH] feat: every error shown in red reaches Sentry, also the ones the page handled itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Until now only failed API calls and uncaught exceptions reached Sentry. An error the page caught and showed, like the folders' "A última tentativa falhou" Marcus photographed on 16/11, or an upload that failed on the Site, never did. Now every red message in the Kanban, the Site's checkout, the customer's account page and every failed upload is sent, one issue per kind of message; the same text an API error already sent, or a 401 / restart 503 it skips on purpose, is not sent twice. Co-Authored-By: Claude Opus 5.5 (1M context) --- tests/observability_web_test.mjs | 15 +++++++++++++++ web/checkout.js | 8 ++++++-- web/kanban.js | 1 + web/observability.js | 23 ++++++++++++++++++++++- web/portal.js | 3 ++- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/tests/observability_web_test.mjs b/tests/observability_web_test.mjs index 48619a5..50c16ba 100644 --- a/tests/observability_web_test.mjs +++ b/tests/observability_web_test.mjs @@ -47,4 +47,19 @@ assert.notDeepEqual( apiError('/quotes/ba39b432-b0d1-4b1b-8971-09123e067067', 422, expired).options.fingerprint, apiError('/orders/ba39b432-b0d1-4b1b-8971-09123e067067', 422, expired).options.fingerprint); +// Every red message reaches Sentry, also one the page caught itself; the +// same text an API error already sent (or deliberately skipped) is not doubled. +captured = null; +window.dtfObservability.uiError('Pastas automáticas: A última tentativa falhou: DTF-26-item-1.png bloqueado.', 'kanban'); +assert.ok(captured, 'uiError sends a caught error'); +assert.equal(captured.message, 'Aviso kanban: Pastas automáticas: A última tentativa falhou: :arquivo bloqueado.'); +assert.deepEqual(captured.options.fingerprint, ['ui-error', 'kanban', 'Pastas automáticas: A última tentativa falhou: :arquivo bloqueado.']); +apiError('/orders', 409, 'Falta a foto da revisão.'); +captured = null; +window.dtfObservability.uiError('Falta a foto da revisão.', 'kanban'); +assert.equal(captured, null, 'an API error shown in red is sent once'); +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'); + console.log('observability web checks passed'); diff --git a/web/checkout.js b/web/checkout.js index 454d9a0..b06c7b4 100644 --- a/web/checkout.js +++ b/web/checkout.js @@ -152,7 +152,10 @@ // One message line under the steps. Its tone says what it is: a refusal or // a missing field is an error (red), a cart that changed a warning, a // created order a confirmation, the rest information. - function message(text, tone='info') { status.textContent = text; status.dataset.tone = text ? tone : ''; } + function message(text, tone='info') { + status.textContent = text; status.dataset.tone = text ? tone : ''; + if (tone === 'error' && text) window.dtfObservability?.uiError?.(text, 'site'); + } function button(label, handler, into = actions, className = 'pagBtn') { const el = document.createElement('button'); el.type = 'button'; @@ -248,7 +251,8 @@ onBytes:n=>{e.sent=n;pintaEnvio();}}); })(); e.promise.then(()=>{e.done=true;if(window.dtfTimings)T.uploadEnd=performance.now();falhas.delete(k);pintaEnvio();}, - error=>{envios.delete(k);if(error?.name!=='AbortError')falhas.set(k,{file,error});pintaEnvio();}); + error=>{envios.delete(k);if(error?.name!=='AbortError'){falhas.set(k,{file,error}); + window.dtfObservability?.uiError?.('Envio de arquivo falhou: '+(error?.message||'erro no envio'),'site');}pintaEnvio();}); envios.set(k,e); } return e.promise; diff --git a/web/kanban.js b/web/kanban.js index 8cd5ca1..892ad54 100644 --- a/web/kanban.js +++ b/web/kanban.js @@ -99,6 +99,7 @@ function button(text,fn,className='btn'){ let sayTimer=null; function say(text,error=false,ms){ clearTimeout(sayTimer); + if(error&&text)window.dtfObservability?.uiError?.(text,'kanban'); for(const id of ['status','login-status']){$(id).textContent=text||'';$(id).classList.toggle('error',!!error);} if(text)sayTimer=setTimeout(()=>say(''),ms??(error?8000:4000)); } diff --git a/web/observability.js b/web/observability.js index 0ebbb19..e664d2e 100644 --- a/web/observability.js +++ b/web/observability.js @@ -114,7 +114,11 @@ const normalizedMessage = value => clean(String(value == null ? '' : value) .replace(UUID, ':id').replace(FILE_NAME, ':arquivo').replace(NUMBER, ':id')); + // What apiError already sent, so the same text shown in red is not sent twice. + const recent = new Map(); function apiError(path, status, message, detail) { + // Recorded first: a skipped 401 or restart 503 stays out of uiError too. + recent.set(String(message), Date.now()); 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; @@ -134,5 +138,22 @@ } } - window.dtfObservability = {start, apiError, scrub}; + // Every error the page shows the user in red, also the ones it caught and + // handled itself (a folder it could not write, an upload that failed): the + // team sees them in Sentry before anyone sends a screenshot. + function uiError(text, where) { + if (!started || !text || typeof Sentry === 'undefined') return; + const said = recent.get(String(text)); + if (said && Date.now() - said < 15000) return; + try { + const kind = normalizedMessage(text); + Sentry.captureMessage('Aviso ' + (where || app) + ': ' + kind, + {level: 'error', tags: {app, aviso: where || app}, extra: {page: location.pathname}, + fingerprint: ['ui-error', where || app, kind]}); + } catch (error) { + // Reporting an error must never break the page. + } + } + + window.dtfObservability = {start, apiError, uiError, scrub}; })(); diff --git a/web/portal.js b/web/portal.js index f36e2a0..ba7e0a9 100644 --- a/web/portal.js +++ b/web/portal.js @@ -38,7 +38,8 @@ let scope,states={},environment='',conta=null; let filtro={status:'all',code:'',days:0,page:1}; async function api(path,body){const r=await fetch('/api'+path,{headers:{'Content-Type':'application/json'},...(body===undefined?{}:{method:'POST',body:JSON.stringify(body)})});const d=await r.json();if(!r.ok){const error=new Error(typeof d.detail==='string'?d.detail:'Confira os campos informados.');error.status=r.status;throw error;}return d;} -const aviso=(text,erro)=>{$('message').textContent=text||'';$('message').classList.toggle('erro',!!erro);}; +const aviso=(text,erro)=>{$('message').textContent=text||'';$('message').classList.toggle('erro',!!erro); + if(erro&&text)window.dtfObservability?.uiError?.(text,'conta');}; function button(text,fn,className='bSec'){const b=node('button',text,className);b.type='button';b.onclick=async event=>{event?.stopPropagation?.();b.disabled=true;try{await fn();}catch(e){aviso(e.message,true);}finally{b.disabled=false;}};return b;} function forgetCheckout(){for(const k of ['dtf-quote','dtf-request-key','dtf-request-body'])localStorage.removeItem(k);}