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);}