Every failed API call landed in one issue titled Object.apiError; each now has its own fingerprint and title from status, path and message, with ids and file names normalized. The 410 of a recovered cart's expired upload was already handled (the file goes up again from the stored copy); the probe no longer reports it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
137 lines
5.6 KiB
JavaScript
137 lines
5.6 KiB
JavaScript
/* Sentry for the Site and the Kanban, used only when the DSN arrives from the
|
|
API. It loads after web/vendor/sentry.min.js and never fetches anything
|
|
itself; DSNs are public by design, so an empty one leaves everything off.
|
|
|
|
beforeSend follows the server (app/observability.py): no cookies, credential
|
|
headers, request bodies or signed/token query strings, and CPF/CNPJ and
|
|
e-mails replaced by [removido]. */
|
|
(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]';
|
|
let started = false;
|
|
let app = '';
|
|
|
|
const clean = value => String(value == null ? '' : value)
|
|
.replace(EMAIL, REDACTED).replace(DOCUMENT, REDACTED);
|
|
|
|
function scrubQuery(query) {
|
|
if (!query) return query;
|
|
return String(query).split('&').filter(part => {
|
|
let key = part.split('=')[0] || '';
|
|
try { key = decodeURIComponent(key.replace(/\+/g, ' ')); } catch (error) { /* keep the raw key */ }
|
|
return !SENSITIVE_PARAM.test(key);
|
|
}).join('&');
|
|
}
|
|
|
|
function scrubText(value) {
|
|
if (typeof value !== 'string') return value;
|
|
value = clean(value);
|
|
const at = value.indexOf('?');
|
|
return at < 0 ? value : value.slice(0, at + 1) + scrubQuery(value.slice(at + 1));
|
|
}
|
|
|
|
function scrubUrl(url) {
|
|
if (typeof url !== 'string') return url;
|
|
const at = url.indexOf('?');
|
|
return at < 0 ? url : url.slice(0, at + 1) + scrubQuery(url.slice(at + 1));
|
|
}
|
|
|
|
function scrubValue(value) {
|
|
if (typeof value === 'string') return scrubText(value);
|
|
if (Array.isArray(value)) return value.map(scrubValue);
|
|
if (value && typeof value === 'object') {
|
|
const out = {};
|
|
for (const [key, item] of Object.entries(value)) out[key] = scrubValue(item);
|
|
return out;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function scrubRequest(request) {
|
|
if (!request || typeof request !== 'object') return;
|
|
delete request.cookies;
|
|
delete request.data;
|
|
delete request.body;
|
|
if (request.headers && typeof request.headers === 'object') {
|
|
for (const key of Object.keys(request.headers)) {
|
|
if (SENSITIVE_HEADER.test(key)) delete request.headers[key];
|
|
}
|
|
}
|
|
if (request.query_string) request.query_string = scrubQuery(request.query_string);
|
|
if (request.url) request.url = scrubUrl(request.url);
|
|
}
|
|
|
|
function scrub(event) {
|
|
if (!event || typeof event !== 'object') return event;
|
|
scrubRequest(event.request);
|
|
if (typeof event.message === 'string') event.message = scrubText(event.message);
|
|
if (event.logentry && typeof event.logentry.message === 'string')
|
|
event.logentry.message = scrubText(event.logentry.message);
|
|
if (event.exception && Array.isArray(event.exception.values)) {
|
|
for (const value of event.exception.values) {
|
|
if (value && typeof value.value === 'string') value.value = scrubText(value.value);
|
|
}
|
|
}
|
|
let breadcrumbs = event.breadcrumbs;
|
|
if (breadcrumbs && !Array.isArray(breadcrumbs)) breadcrumbs = breadcrumbs.values;
|
|
if (Array.isArray(breadcrumbs)) {
|
|
for (const crumb of breadcrumbs) {
|
|
if (!crumb || typeof crumb !== 'object') continue;
|
|
if (typeof crumb.message === 'string') crumb.message = scrubText(crumb.message);
|
|
if (crumb.data) crumb.data = scrubValue(crumb.data);
|
|
}
|
|
}
|
|
if (event.extra) event.extra = scrubValue(event.extra);
|
|
if (event.user) event.user = scrubValue(event.user);
|
|
return event;
|
|
}
|
|
|
|
function start(dsn, component) {
|
|
if (started || !dsn || typeof Sentry === 'undefined') return;
|
|
started = true;
|
|
app = component || '';
|
|
const options = {dsn, sendDefaultPii: false, beforeSend: scrub};
|
|
if (window.dtfEnvironment) options.environment = window.dtfEnvironment;
|
|
if (window.dtfRelease) options.release = window.dtfRelease;
|
|
try {
|
|
Sentry.init(options);
|
|
Sentry.setTag('app', app);
|
|
} catch (error) {
|
|
started = false;
|
|
}
|
|
}
|
|
|
|
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('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.
|
|
}
|
|
}
|
|
|
|
window.dtfObservability = {start, apiError, scrub};
|
|
})();
|