Merge branch 'bugs-1112-web' into integration

This commit is contained in:
Cauê Faleiros
2026-11-12 10:09:20 -03:00
5 changed files with 79 additions and 8 deletions

View File

@@ -37,7 +37,7 @@
localStorage.removeItem(key);
}
function clearDraft() { forgetDraft(); actions.replaceChildren(); }
const api = async (path, body) => {
const api = async (path, body, options = {}) => {
const response = await fetch('/api'+path, {
credentials: 'same-origin', headers: {'Content-Type':'application/json'},
...(body === undefined ? {} : {method:'POST', body:JSON.stringify(body)})
@@ -55,7 +55,10 @@
const error=new Error(text);error.status=response.status;
const retry=response.headers.get('Retry-After');
if(retry!=null)error.retryAfter=retry;
window.dtfObservability?.apiError?.(path,response.status,error.message,data.detail);
// A status the caller expects (a resumed upload that expired) is handled
// there, not reported.
if(!(options.expected||[]).includes(response.status))
window.dtfObservability?.apiError?.(path,response.status,error.message,data.detail);
throw error;
}
return data;

View File

@@ -8,6 +8,11 @@
(function () {
const EMAIL = /[^\s@]+@[^\s@]+\.[A-Za-z]{2,}/g;
const DOCUMENT = /(?<!\d)(?:\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}|\d{3}\.?\d{3}\.?\d{3}-?\d{2})(?!\d)/g;
// What changes from one API error to the next of the same kind: the ids of
// the files, quotes and orders, the numbers and the file names.
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
const FILE_NAME = /[^\s"'<>:,;()[\]{}]+\.(?:png|jpe?g|pdf|tiff?|psd|psb|ai|cdr|webp|zip)\b/gi;
const NUMBER = /\d+/g;
const SENSITIVE_PARAM = /token|signature|x-amz/i;
const SENSITIVE_HEADER = /^(cookie|authorization|x-.*token.*)$/i;
const REDACTED = '[removido]';
@@ -104,13 +109,24 @@
}
}
const normalizedPath = value => clean(String(value == null ? '' : value)
.replace(UUID, ':id').replace(NUMBER, ':id'));
const normalizedMessage = value => clean(String(value == null ? '' : value)
.replace(UUID, ':id').replace(FILE_NAME, ':arquivo').replace(NUMBER, ':id'));
function apiError(path, status, message, detail) {
if (!started || status === 401 || typeof Sentry === 'undefined') 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
// 96 DPI. The title names the same kind as the fingerprint.
const kindPath = normalizedPath(path);
const kindMessage = normalizedMessage(message);
const extra = {path, status, page: location.pathname};
if (status === 422 && detail) extra.detail = detail;
Sentry.captureMessage(clean('API ' + status + ' ' + path + ': ' + (message || '')),
{level: 'error', tags: {app}, extra});
Sentry.captureMessage('API ' + status + ' ' + kindPath + ': ' + kindMessage,
{level: 'error', tags: {app}, extra,
fingerprint: ['api-error', String(status), kindPath, kindMessage]});
} catch (error) {
// Reporting an error must never break the page.
}

View File

@@ -36,11 +36,12 @@ async function sendUpload(file, {api, prefix='/uploads', startPath=prefix, progr
};
let id=resume?localStorage.getItem(key):null,state;
if(id){
try{state=await api(prefix+'/'+id);}
try{state=await api(prefix+'/'+id,undefined,{expected:[403,404,410]});}
catch(error){
// The id was started under another session (a sign-in changes the scope):
// the new session does not own it, so this file starts again as a new
// upload. A 403/404/410 must never keep the retry on the old id.
// The id is only a hint from the last visit. When the upload expired or
// was started under another session (a sign-in changes the scope), this
// file starts again as a new upload, silently, and its id is replaced.
// These refusals are expected here and never reach Sentry.
if(![403,404,410].includes(error.status))throw error;
id=null;
if(resume)localStorage.removeItem(key);